-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1651 from serlo/delete-courses-without-pages
feat(db-migrations): Delete courses without pages
- Loading branch information
Showing
3 changed files
with
53 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
39 changes: 39 additions & 0 deletions
39
packages/db-migrations/src/20240703115600-remove-courses-without-any-pages.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
import { | ||
ApiCache, | ||
Database, | ||
SlackLogger, | ||
deleteUuids, | ||
toSqlTuple, | ||
} from './utils' | ||
|
||
export async function up(db: Database) { | ||
const apiCache = new ApiCache() | ||
const logger = new SlackLogger( | ||
'20240703115600-remove-courses-without-any-pages', | ||
) | ||
|
||
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 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() | ||
await apiCache.deleteKeysAndQuit() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters