You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

42 lines
1022 B

import { drizzle } from 'drizzle-orm/node-postgres'
import { migrate } from 'drizzle-orm/node-postgres/migrator'
import { Pool } from 'pg'
import path from 'node:path'
import { fileURLToPath } from 'node:url'
export async function runMigrations() {
const databaseUrl = process.env.DATABASE_URL
if (!databaseUrl) {
throw new Error('DATABASE_URL 未设置')
}
const pool = new Pool({ connectionString: databaseUrl })
const db = drizzle(pool)
try {
console.log('🚀 开始执行 PostgreSQL 迁移...')
const migrationsFolder = path.resolve(process.cwd(), 'migrations')
await migrate(db, {
migrationsFolder,
})
console.log('✅ PostgreSQL 迁移完成!')
} catch (err) {
console.log('❌ 迁移失败:', err)
throw err
} finally {
await pool.end()
}
}
const isMain =
process.argv[1] &&
path.resolve(process.argv[1]) === fileURLToPath(import.meta.url)
if (isMain) {
runMigrations().catch((err) => {
console.error(err)
process.exit(1)
})
}