Skip to content

Commit be9f064

Browse files
authored
Merge pull request #173 from prgrms-aibe-devcourse/fix/171-plant-collection-backfill
[Bug] 기존 계정 plant_collection 백필 마이그레이션
2 parents 41f48fc + 47534e3 commit be9f064

3 files changed

Lines changed: 48 additions & 0 deletions

File tree

src/main/java/com/Rootin/global/config/seeder/DatabaseSeeder.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ public void seed() {
2525
log.info("=== DB 시드 데이터 검사 시작 ===");
2626
schemaSeeder.ensureUniqueActivePlantPerPot();
2727
plantMasterSeeder.seed();
28+
schemaSeeder.backfillPlantCollection();
2829
userPotSeeder.seed().ifPresent(tilSeeder::seed);
2930
log.info("=== DB 시드 데이터 완료 ===");
3031
}

src/main/java/com/Rootin/global/config/seeder/SchemaSeeder.java

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
-- ============================================================
2+
-- backfill_plant_collection.sql
3+
--
4+
-- 대상: PR #169(씨앗 배정 정책) 이전에 pot을 생성한 기존 계정
5+
-- 증상: plant_collection 레코드 없음 → 수확 시 "해금된 씨앗이 없습니다." 오류
6+
-- 처리: plant_item 이력 기반으로 유저별 해금 씨앗을 INSERT IGNORE
7+
--
8+
-- 실행 전 검증 (영향 대상 유저 수 확인):
9+
-- SELECT COUNT(DISTINCT pi.user_id)
10+
-- FROM plant_item pi
11+
-- WHERE NOT EXISTS (
12+
-- SELECT 1 FROM plant_collection pc WHERE pc.user_id = pi.user_id
13+
-- );
14+
--
15+
-- 실행 후 검증:
16+
-- SELECT user_id, COUNT(*) AS unlocked FROM plant_collection GROUP BY user_id ORDER BY user_id;
17+
-- ============================================================
18+
19+
INSERT IGNORE INTO plant_collection (user_id, plant_id, created_at)
20+
SELECT DISTINCT
21+
pi.user_id,
22+
pi.plant_id,
23+
NOW()
24+
FROM plant_item pi
25+
INNER JOIN plant p ON p.id = pi.plant_id
26+
AND p.growth_stage = 'Seed';

0 commit comments

Comments
 (0)