Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/moderation-api-v2' into multi-pd…
Browse files Browse the repository at this point in the history
…s-auth-moderation-api-v2
  • Loading branch information
devinivy committed Nov 30, 2023
2 parents 6db08df + a8b95bf commit 3d17be7
Showing 1 changed file with 34 additions and 0 deletions.
34 changes: 34 additions & 0 deletions packages/bsky/src/migrate-moderation-data.ts
Original file line number Diff line number Diff line change
Expand Up @@ -309,6 +309,39 @@ const syncBlobCids = async (db: PrimaryDatabase) => {
console.log(`Updated blob cids on ${results.numUpdatedOrDeletedRows} rows`)
}

export async function migrateUnresolvedReports(db: PrimaryDatabase) {
const { ref } = db.db.dynamic
const reports = (await db.db
// @ts-ignore
.selectFrom('moderation_report')
.whereNotExists((qb) =>
qb
.selectFrom('moderation_report_resolution')
.selectAll()
// @ts-ignore
.whereRef('reportId', '=', ref('moderation_report.id')),
)
// @ts-ignore
.select((eb) => eb.fn.min<number>('id').as('firstUnresolvedReportId'))
.executeTakeFirstOrThrow()) as { firstUnresolvedReportId: number }

if (!reports.firstUnresolvedReportId) {
console.log('No unresolved reports to migrate')
return
}

console.log(
`Migrating unresolved reports from id ${reports.firstUnresolvedReportId}`,
)

await createEvents(db, {
onlyReportsAboveId: reports.firstUnresolvedReportId,
})
await processNewReports(db, reports.firstUnresolvedReportId)
await setReportedAtTimestamp(db)
console.log(`Migrated all unresolved reports`)
}

export async function MigrateModerationData() {
const env = getEnv()
const db = new DatabaseCoordinator({
Expand Down Expand Up @@ -362,6 +395,7 @@ export async function MigrateModerationData() {
await createStatusFromActions(primaryDb)
await setReportedAtTimestamp(primaryDb)
await syncBlobCids(primaryDb)
await migrateUnresolvedReports(primaryDb)

console.log(`Time spent: ${(Date.now() - startedAt) / 1000 / 60} minutes`)
console.log('Migration complete!')
Expand Down

0 comments on commit 3d17be7

Please sign in to comment.