-
Notifications
You must be signed in to change notification settings - Fork 113
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
enh: add sId column on content fragments (#8647)
* 0;276;0cenh: add sId column on content fragments * index concurrently * add backfill script ref in SQL * move index creation to second migration --------- Co-authored-by: Henry Fontanier <henry@dust.tt>
- Loading branch information
1 parent
2e3c53b
commit 448a5a2
Showing
5 changed files
with
86 additions
and
2 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
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
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,62 @@ | ||
import { Op } from "sequelize"; | ||
|
||
import { ContentFragmentModel } from "@app/lib/resources/storage/models/content_fragment"; | ||
import { generateRandomModelSId } from "@app/lib/resources/string_ids"; | ||
import { makeScript } from "@app/scripts/helpers"; | ||
|
||
makeScript({}, async ({ execute }, logger) => { | ||
let lastSeenId = 0; | ||
const batchSize = 1000; | ||
|
||
for (;;) { | ||
// Find content fragments without sId | ||
const contentFragments: ContentFragmentModel[] = | ||
await ContentFragmentModel.findAll({ | ||
// @ts-expect-error -- sequelize type for sId is not nullable (it temporarily is in db) | ||
where: { | ||
id: { | ||
[Op.gt]: lastSeenId, | ||
}, | ||
sId: { | ||
[Op.is]: null, | ||
}, | ||
}, | ||
order: [["id", "ASC"]], | ||
limit: batchSize, | ||
}); | ||
|
||
if (contentFragments.length === 0) { | ||
break; | ||
} | ||
|
||
logger.info( | ||
`Processing ${contentFragments.length} content fragments starting from ID ${lastSeenId}` | ||
); | ||
|
||
if (execute) { | ||
await Promise.all( | ||
contentFragments.map(async (cf) => { | ||
const sId = generateRandomModelSId("cf"); | ||
await cf.update({ sId }); | ||
logger.info( | ||
{ | ||
contentFragmentId: cf.id, | ||
sId, | ||
}, | ||
"Updated content fragment with sId" | ||
); | ||
}) | ||
); | ||
} else { | ||
logger.info( | ||
{ | ||
lastSeenId, | ||
count: contentFragments.length, | ||
}, | ||
"Dry run - would have updated content fragments with sIds" | ||
); | ||
} | ||
|
||
lastSeenId = contentFragments[contentFragments.length - 1].id; | ||
} | ||
}); |
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,5 @@ | ||
-- Migration created on Nov 14, 2024 | ||
ALTER TABLE | ||
"public"."content_fragments" | ||
ADD | ||
COLUMN "sId" VARCHAR(255); |
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,10 @@ | ||
-- Migration created on Nov 14, 2024 | ||
-- Backfill script that needs to be ran: 20241114_backfill_cf_sid.ts | ||
ALTER TABLE | ||
"public"."content_fragments" | ||
ALTER COLUMN | ||
"sId" | ||
SET | ||
NOT NULL; | ||
|
||
CREATE INDEX CONCURRENTLY "content_fragments_s_id" ON "content_fragments" ("sId"); |