diff --git a/packages/db-migrations/src/20240703115600-remove-courses-without-any-pages.ts b/packages/db-migrations/src/20240703115600-remove-courses-without-any-pages.ts index a798923c8..ffed42493 100644 --- a/packages/db-migrations/src/20240703115600-remove-courses-without-any-pages.ts +++ b/packages/db-migrations/src/20240703115600-remove-courses-without-any-pages.ts @@ -1,4 +1,10 @@ -import { ApiCache, Database, SlackLogger, deleteUuids } from './utils' +import { + ApiCache, + Database, + SlackLogger, + deleteUuids, + toSqlTuple, +} from './utils' export async function up(db: Database) { const apiCache = new ApiCache() @@ -6,14 +12,26 @@ export async function up(db: Database) { '20240703115600-remove-courses-without-any-pages', ) - const revisions = await db.runSql<{ id: number; entityId: number }[]>(` - SELECT id, repository_id as entityId + const liveRevisions = await db.runSql<{ id: number; entityId: number }[]>(` + SELECT entity_revision.id, entity.id AS entityId FROM entity_revision + JOIN entity ON entity.id = repository_id WHERE content LIKE '%"pages":[]%' + AND entity.current_revision_id = entity_revision.id `) - const uuids = revisions.flatMap((revision) => { + + const oldRevisions = await db.runSql<{ id: number }[]>(` + SELECT id + FROM entity_revision + WHERE content LIKE '%"pages":[]%' + AND id NOT IN ${toSqlTuple(liveRevisions.map((revision) => revision.id))} + `) + + const uuids = liveRevisions.flatMap((revision) => { return [revision.id, revision.entityId] }) + uuids.concat(oldRevisions.map((revision) => revision.id)) + await deleteUuids(db, apiCache, uuids) await logger.closeAndSend()