Files
seastem-electronjs/embed-migrations.ts
T
imbytecat 7e27640a26 feat(deploy): migrations 嵌入二进制,实现真单文件部署
- embed-migrations.ts:扫 ./drizzle/meta/_journal.json,生成 src/server/db/migrations.gen.ts,每条 SQL 通过 `import sql_<idx> from '../../../drizzle/<tag>.sql' with { type: 'text' }` 在 bun build --compile 时被静态嵌入二进制
- migrate.ts 重写:runtime 用 createHash('sha256') 计算迁移哈希,仅用 db.execute(sql) + db.transaction() 公开 API 写入 drizzle.__drizzle_migrations 簿记表(不依赖 @internal 的 db.dialect/db.session)
- db:generate 链 db:embed,保证 SQL 改动总是同步到 migrations.gen.ts
- Dockerfile 删 COPY drizzle/,binary 是部署唯一 artifact
- 同步 README / AGENTS / biome.json
2026-04-25 14:05:58 +08:00

39 lines
1.4 KiB
TypeScript

import { existsSync } from 'node:fs'
import { readFile, writeFile } from 'node:fs/promises'
const JOURNAL = './drizzle/meta/_journal.json'
const OUTPUT = './src/server/db/migrations.gen.ts'
const SQL_RELATIVE_FROM_OUTPUT = '../../../drizzle'
type JournalEntry = { idx: number; tag: string; when: number; breakpoints: boolean }
type Journal = { entries: JournalEntry[] }
const main = async () => {
const entries: JournalEntry[] = existsSync(JOURNAL)
? ((JSON.parse(await readFile(JOURNAL, 'utf-8')) as Journal).entries ?? []).sort((a, b) => a.idx - b.idx)
: []
const imports = entries
.map((e) => `import sql_${e.idx} from '${SQL_RELATIVE_FROM_OUTPUT}/${e.tag}.sql' with { type: 'text' }`)
.join('\n')
const arrayBody = entries.length
? `[\n${entries.map((e) => ` { tag: '${e.tag}', sql: sql_${e.idx}, when: ${e.when}, breakpoints: ${e.breakpoints} },`).join('\n')}\n]`
: '[]'
const out = `// AUTO-GENERATED by \`bun run db:embed\`. Do not edit.
${imports ? `${imports}\n` : ''}
export type EmbeddedMigration = { tag: string; sql: string; when: number; breakpoints: boolean }
export const embeddedMigrations: readonly EmbeddedMigration[] = ${arrayBody}
`
await writeFile(OUTPUT, out)
console.log(`${OUTPUT} (${entries.length} migration${entries.length === 1 ? '' : 's'})`)
}
main().catch((err) => {
console.error('❌', err instanceof Error ? err.message : err)
process.exit(1)
})