File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 1+ #!/usr/bin/env node
2+ import { sql } from "drizzle-orm" ;
3+ import { db } from "./lib/db/client.js" ;
4+ import { migrate } from "drizzle-orm/vercel-postgres/migrator" ;
5+
6+ async function runMigrations ( ) {
7+ try {
8+ // 检查迁移历史表是否存在
9+ const result = await db . execute ( sql `
10+ SELECT EXISTS (
11+ SELECT FROM information_schema.tables
12+ WHERE table_schema = 'drizzle'
13+ AND table_name = '__drizzle_migrations'
14+ );
15+ ` ) ;
16+
17+ const tableExists = result . rows [ 0 ] ?. exists ;
18+
19+ if ( ! tableExists ) {
20+ console . log ( "首次部署,执行数据库迁移..." ) ;
21+ await migrate ( db , { migrationsFolder : "./drizzle" } ) ;
22+ console . log ( "✓ 迁移完成" ) ;
23+ } else {
24+ console . log ( "迁移历史已存在,跳过迁移" ) ;
25+ }
26+ } catch ( error ) {
27+ console . error ( "迁移检查失败:" , error ) ;
28+ // 不阻止构建继续
29+ process . exit ( 0 ) ;
30+ }
31+ }
32+
33+ runMigrations ( ) ;
You can’t perform that action at this time.
0 commit comments