refactor(db): 移除嵌入式迁移链路
This commit is contained in:
@@ -1,84 +0,0 @@
|
||||
import { defineCommand } from 'citty'
|
||||
|
||||
export default defineCommand({
|
||||
meta: {
|
||||
name: 'migrate',
|
||||
description: 'Apply pending database migrations',
|
||||
},
|
||||
async run() {
|
||||
const [{ env }, { drizzle }, { sql }, { embeddedMigrations }, { getLogger }] = await Promise.all([
|
||||
import('@/env'),
|
||||
import('drizzle-orm/postgres-js'),
|
||||
import('drizzle-orm'),
|
||||
import('@/server/db/migrations.gen'),
|
||||
import('@/server/logger'),
|
||||
])
|
||||
|
||||
const logger = getLogger(['cli', 'migrate'])
|
||||
|
||||
if (embeddedMigrations.length === 0) {
|
||||
logger.info('No migrations bundled into this binary.')
|
||||
return
|
||||
}
|
||||
|
||||
const sha256 = (s: string) => Bun.CryptoHasher.hash('sha256', s, 'hex')
|
||||
|
||||
const db = drizzle({
|
||||
connection: {
|
||||
url: env.DATABASE_URL,
|
||||
max: 1,
|
||||
onnotice: (n) => logger.debug('pg notice', { notice: n.message }),
|
||||
},
|
||||
})
|
||||
try {
|
||||
await db.execute(sql`CREATE SCHEMA IF NOT EXISTS "drizzle"`)
|
||||
await db.execute(sql`
|
||||
CREATE TABLE IF NOT EXISTS "drizzle"."__drizzle_migrations" (
|
||||
id SERIAL PRIMARY KEY,
|
||||
hash text NOT NULL,
|
||||
created_at bigint
|
||||
)
|
||||
`)
|
||||
|
||||
const applied = await db.execute<{ hash: string; created_at: string | null }>(
|
||||
sql`SELECT hash, created_at FROM "drizzle"."__drizzle_migrations" ORDER BY created_at ASC`,
|
||||
)
|
||||
|
||||
// Reject schema drift: any applied migration whose embedded SQL has changed (or is missing) is fatal.
|
||||
for (const row of applied) {
|
||||
const when = Number(row.created_at)
|
||||
const m = embeddedMigrations.find((e) => e.when === when)
|
||||
if (!m) {
|
||||
throw new Error(`Applied migration when=${when} is not in this binary; do not roll back applied migrations.`)
|
||||
}
|
||||
if (sha256(m.sql) !== row.hash) {
|
||||
throw new Error(`Migration hash mismatch at when=${when}; do not edit migrations after they are applied.`)
|
||||
}
|
||||
}
|
||||
|
||||
const appliedWhens = new Set(applied.map((r) => Number(r.created_at)))
|
||||
const pending = embeddedMigrations.filter((m) => !appliedWhens.has(m.when))
|
||||
if (pending.length === 0) {
|
||||
logger.info('Database is up to date.')
|
||||
return
|
||||
}
|
||||
|
||||
logger.info('Applying {count} migration(s)...', { count: pending.length })
|
||||
await db.transaction(async (tx) => {
|
||||
for (const m of pending) {
|
||||
for (const rawStmt of m.sql.split('--> statement-breakpoint')) {
|
||||
const stmt = rawStmt.trim()
|
||||
if (!stmt) continue
|
||||
await tx.execute(sql.raw(stmt))
|
||||
}
|
||||
await tx.execute(
|
||||
sql`INSERT INTO "drizzle"."__drizzle_migrations" ("hash", "created_at") VALUES (${sha256(m.sql)}, ${m.when})`,
|
||||
)
|
||||
}
|
||||
})
|
||||
logger.info('Migrations applied.')
|
||||
} finally {
|
||||
await db.$client.end()
|
||||
}
|
||||
},
|
||||
})
|
||||
Reference in New Issue
Block a user