-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
for back fill short-hash for past existing articles
- Loading branch information
Showing
2 changed files
with
54 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
#!/usr/bin/env -S node --trace-warnings --loader ts-node/esm | ||
|
||
import { sql, ARRAY_TYPE } from "../lib/db.js"; | ||
|
||
// as same as https://github.com/thematters/matters-server/blob/develop/src/common/utils/nanoid.ts | ||
import { customAlphabet } from "nanoid"; | ||
|
||
const ALPHABET = "0123456789abcdefghijklmnopqrstuvwxyz"; | ||
|
||
const nanoid = customAlphabet(ALPHABET, 12); // ~35 years or 308M IDs needed, in order to have a 1% probability of one collision // https://zelark.github.io/nano-id-cc/ | ||
|
||
const BATCH_SIZE = Number.parseInt(process.env.BATCH_SIZE || "", 10) || 100; | ||
|
||
async function main() { | ||
const args = process.argv.slice(2); | ||
|
||
// const [row] = await sql`SELECT VERSION(), CURRENT_TIMESTAMP`; | ||
// console.log(new Date(), `get version:`, row); | ||
|
||
const rows = await sql`-- batch fill short-hash for article table | ||
WITH last_batch AS ( | ||
SELECT id, title, short_hash, ROW_NUMBER() OVER(ORDER BY id DESC) ::int seq | ||
FROM article | ||
WHERE short_hash IS NULL | ||
ORDER BY id DESC LIMIT ${BATCH_SIZE} | ||
), last_batch_with_hash AS ( | ||
SELECT id, title, cte.* | ||
FROM last_batch JOIN ( | ||
SELECT * | ||
FROM UNNEST( | ||
${sql.array( | ||
Array.from({ length: BATCH_SIZE }).map((_, seq) => seq + 1), | ||
ARRAY_TYPE | ||
)} ::int[], | ||
${sql.array( | ||
Array.from({ length: BATCH_SIZE }).map(() => nanoid()), | ||
ARRAY_TYPE | ||
)} ::text[] | ||
) AS t(seq, short_hash) | ||
) cte USING(seq) | ||
) | ||
UPDATE article | ||
SET short_hash=last_batch_with_hash.short_hash | ||
FROM last_batch_with_hash | ||
WHERE article.id=last_batch_with_hash.id | ||
RETURNING article.* ; `; | ||
|
||
console.log(new Date(), `filled ${rows.length} rows:`, rows); | ||
} | ||
|
||
main().catch((err) => console.error(new Date(), "ERROR:", err)); |
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