@@ -12,6 +12,7 @@ public class SchemaSeeder {
1212
1313 private static final String INDEX_NAME = "ux_plant_item_one_active_per_pot" ;
1414 private static final String MIGRATION_FILE = "src/main/resources/db/cleanup_duplicate_active_plant_items.sql" ;
15+ private static final String BACKFILL_SCRIPT = "src/main/resources/db/backfill_plant_collection.sql" ;
1516
1617 private final JdbcTemplate jdbcTemplate ;
1718
@@ -49,4 +50,24 @@ public void ensureUniqueActivePlantPerPot() {
4950 "파일: {}" , MIGRATION_FILE , e );
5051 }
5152 }
53+
54+ /**
55+ * PR #169 이전에 pot을 생성한 기존 계정의 plant_collection 백필.
56+ * plant_item 이력 기반으로 유저별 해금 씨앗을 INSERT IGNORE.
57+ * 이미 존재하는 레코드는 unique constraint(user_id, plant_id)로 자동 스킵.
58+ * 참조 스크립트: backfill_plant_collection.sql
59+ */
60+ public void backfillPlantCollection () {
61+ int insertedRows = jdbcTemplate .update (
62+ "INSERT IGNORE INTO plant_collection (user_id, plant_id, created_at)" +
63+ " SELECT DISTINCT pi.user_id, pi.plant_id, NOW()" +
64+ " FROM plant_item pi" +
65+ " INNER JOIN plant p ON p.id = pi.plant_id AND p.growth_stage = 'Seed'"
66+ );
67+ if (insertedRows > 0 ) {
68+ log .info ("plant_collection 백필 완료 — {}건 삽입. 참조: {}" , insertedRows , BACKFILL_SCRIPT );
69+ } else {
70+ log .debug ("plant_collection 백필 불필요 — 모든 유저 레코드 정상." );
71+ }
72+ }
5273}
0 commit comments