@@ -124,6 +124,7 @@ interface LegacyProject {
124124 countUniqueDonorsForActiveQfRound : number | null
125125 countUniqueDonors : number | null
126126 qualityScore : number
127+ totalPower : number | null
127128 adminUserId : number
128129 creationDate : Date | null
129130 updatedAt : Date | null
@@ -1088,14 +1089,22 @@ export class LegacyDataSyncService implements OnModuleDestroy {
10881089 while ( offset < totalCount ) {
10891090 const projects = await this . queryLegacyDb < LegacyProject > (
10901091 `SELECT
1091- id, title, slug, "slugHistory", description, "descriptionSummary",
1092- image, "impactLocation", verified, "verificationStatus", "reviewStatus",
1093- "totalDonations", "totalReactions", "totalProjectUpdates",
1094- "sumDonationValueUsdForActiveQfRound", "countUniqueDonorsForActiveQfRound",
1095- "countUniqueDonors", "qualityScore", "adminUserId", "creationDate", "updatedAt",
1096- "statusId", "isGivbackEligible", "latestUpdateCreationDate"
1097- FROM project
1098- ORDER BY id
1092+ p.id, p.title, p.slug, p."slugHistory", p.description, p."descriptionSummary",
1093+ p.image, p."impactLocation", p.verified, p."verificationStatus", p."reviewStatus",
1094+ p."totalDonations", p."totalReactions", p."totalProjectUpdates",
1095+ p."sumDonationValueUsdForActiveQfRound", p."countUniqueDonorsForActiveQfRound",
1096+ p."countUniqueDonors", p."qualityScore", ppv."totalPower", p."adminUserId",
1097+ p."creationDate", p."updatedAt", p."statusId", p."isGivbackEligible",
1098+ p."latestUpdateCreationDate"
1099+ FROM project p
1100+ LEFT JOIN LATERAL (
1101+ SELECT "totalPower"
1102+ FROM project_power_view ppv
1103+ WHERE ppv."projectId" = p.id
1104+ ORDER BY ppv."round" DESC NULLS LAST
1105+ LIMIT 1
1106+ ) ppv ON TRUE
1107+ ORDER BY p.id
10991108 LIMIT $1 OFFSET $2` ,
11001109 [ this . batchSize , offset ] ,
11011110 )
@@ -1135,7 +1144,7 @@ export class LegacyDataSyncService implements OnModuleDestroy {
11351144 ${ p . totalDonations } , ${ p . totalReactions } , ${ p . totalProjectUpdates ?? 'NULL' } ,
11361145 ${ p . sumDonationValueUsdForActiveQfRound ?? 'NULL' } ,
11371146 ${ p . countUniqueDonorsForActiveQfRound ?? 'NULL' } ,
1138- ${ p . countUniqueDonors ?? 'NULL' } , ${ p . qualityScore ?? 0 } , ${ p . adminUserId } ,
1147+ ${ p . countUniqueDonors ?? 'NULL' } , ${ p . qualityScore ?? 0 } , ${ p . totalPower ?? 'NULL' } , ${ p . adminUserId } ,
11391148 '${ p . createdAt } ', NOW(),
11401149 ${ p . latestUpdateCreationDate ? `'${ new Date ( p . latestUpdateCreationDate ) . toISOString ( ) } '` : 'NULL' } )` ,
11411150 )
@@ -1147,7 +1156,7 @@ export class LegacyDataSyncService implements OnModuleDestroy {
11471156 image, impact_location, vouched, is_givbacks_eligible, review_status, status,
11481157 total_donations, total_reactions, total_project_updates,
11491158 sum_donation_value_usd_for_active_qf_round, count_unique_donors_for_active_qf_round,
1150- count_unique_donors, quality_score, admin_user_id, created_at, updated_at,
1159+ count_unique_donors, quality_score, total_power, admin_user_id, created_at, updated_at,
11511160 latest_update_creation_date)
11521161 VALUES ${ values }
11531162 ON CONFLICT (id) DO UPDATE SET
@@ -1169,6 +1178,7 @@ export class LegacyDataSyncService implements OnModuleDestroy {
11691178 count_unique_donors_for_active_qf_round = EXCLUDED.count_unique_donors_for_active_qf_round,
11701179 count_unique_donors = EXCLUDED.count_unique_donors,
11711180 quality_score = EXCLUDED.quality_score,
1181+ total_power = EXCLUDED.total_power,
11721182 admin_user_id = EXCLUDED.admin_user_id,
11731183 latest_update_creation_date = EXCLUDED.latest_update_creation_date,
11741184 updated_at = NOW()
@@ -1954,6 +1964,26 @@ export class LegacyDataSyncService implements OnModuleDestroy {
19541964 ` )
19551965 }
19561966
1967+ // Legacy sync inserts explicit IDs. Reset sequence so future inserts
1968+ // (e.g. boost_temp Prisma upserts that create rows) don't collide on PK(id).
1969+ // Keep setval monotonic so concurrent inserts cannot move it backwards.
1970+ try {
1971+ await this . prisma . $executeRawUnsafe ( `
1972+ SELECT setval(
1973+ pg_get_serial_sequence('"power_boostings"', 'id'),
1974+ GREATEST(
1975+ COALESCE((SELECT MAX(id) FROM "power_boostings"), 0) + 1,
1976+ (SELECT last_value FROM "power_boostings_id_seq")
1977+ ),
1978+ false
1979+ )
1980+ ` )
1981+ } catch ( error ) {
1982+ this . logger . warn (
1983+ `Failed to reset power_boostings id sequence after legacy sync: ${ error . message } ` ,
1984+ )
1985+ }
1986+
19571987 this . logger . log ( `Synced ${ validPowerBoostings . length } power boostings` )
19581988 return { synced : validPowerBoostings . length }
19591989 }
0 commit comments