From 76a32e1dc43ab2b95fa58deed616075360bcfc7f Mon Sep 17 00:00:00 2001 From: mkelvers Date: Tue, 26 May 2026 13:48:38 +0200 Subject: [PATCH] feat: add new-data-fix scaffolding script --- justfile | 3 ++ scripts/new-data-fix.ts | 69 +++++++++++++++++++++++++++++++++++++++++ tsconfig.json | 2 +- 3 files changed, 73 insertions(+), 1 deletion(-) create mode 100644 scripts/new-data-fix.ts diff --git a/justfile b/justfile index 2c5f0d6..0a51098 100644 --- a/justfile +++ b/justfile @@ -43,3 +43,6 @@ dev: build clean: rm -rf dist/* rm -f server + +new-data-fix name: + bun scripts/new-data-fix.ts {{name}} diff --git a/scripts/new-data-fix.ts b/scripts/new-data-fix.ts new file mode 100644 index 0000000..9fc84d3 --- /dev/null +++ b/scripts/new-data-fix.ts @@ -0,0 +1,69 @@ +import { mkdir, writeFile, access } from 'node:fs/promises'; +import { constants as fsConstants } from 'node:fs'; +import path from 'node:path'; + +function toSlug(raw: string): string { + const trimmed = raw.trim().toLowerCase(); + const slug = trimmed.replace(/[^a-z0-9]+/g, '_').replace(/^_+|_+$/g, ''); + return slug; +} + +function formatYYYYMMDD(date: Date): string { + const year = String(date.getFullYear()); + const month = String(date.getMonth() + 1).padStart(2, '0'); + const day = String(date.getDate()).padStart(2, '0'); + return `${year}${month}${day}`; +} + +async function fileExists(filePath: string): Promise { + try { + await access(filePath, fsConstants.F_OK); + return true; + } catch { + return false; + } +} + +async function main(): Promise { + const rawName = process.argv[2] ?? ''; + const slug = toSlug(rawName); + if (slug.length === 0) { + throw new Error('usage: bun scripts/new-data-fix.ts '); + } + + const id = `${formatYYYYMMDD(new Date())}_${slug}`; + const dir = path.join(process.cwd(), 'internal', 'database'); + const filePath = path.join(dir, `fix_${id}.go`); + + await mkdir(dir, { recursive: true }); + + if (await fileExists(filePath)) { + throw new Error(`data fix already exists: ${filePath}`); + } + + const contents = `package database + +import ( + "context" + "database/sql" + "fmt" +) + +func init() { + registerDataFix(dataFix{ + id: "${id}", + apply: func(ctx context.Context, sqlDB *sql.DB) error { + // TODO: implement fix + // _, err := sqlDB.ExecContext(ctx, \`UPDATE ...\`) + // if err != nil { return fmt.Errorf("...: %w", err) } + return fmt.Errorf("unimplemented data fix: ${id}") + }, + }) +} +`; + + await writeFile(filePath, contents, { encoding: 'utf8' }); + process.stdout.write(`${filePath}\n`); +} + +await main(); diff --git a/tsconfig.json b/tsconfig.json index 4ea4308..afe4057 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -12,5 +12,5 @@ "removeComments": false, "skipLibCheck": true }, - "include": ["eslint.config.ts", "static/**/*.ts"] + "include": ["eslint.config.ts", "static/**/*.ts", "scripts/**/*.ts"] }