Skip to content

Commit

Permalink
refactor(course-without-pages): only delete courses whose current rev…
Browse files Browse the repository at this point in the history
…ision has no pages
  • Loading branch information
hugotiburtino committed Jul 3, 2024
1 parent 44bc477 commit acc4ca9
Showing 1 changed file with 22 additions and 4 deletions.
Original file line number Diff line number Diff line change
@@ -1,19 +1,37 @@
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()
const logger = new SlackLogger(
'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()
Expand Down

0 comments on commit acc4ca9

Please sign in to comment.