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.
 
 
 
 

51 lines
1.3 KiB

import { drizzle } from 'drizzle-orm/better-sqlite3'
import { migrate } from 'drizzle-orm/better-sqlite3/migrator'
import Database from 'better-sqlite3'
import path from 'node:path'
import { fileURLToPath } from 'node:url'
const argv = process.argv.slice(2)
const migrationsFolderRelative = argv[0]
if (!migrationsFolderRelative) {
throw new Error('migrations 文件夹未设置')
}
export async function runMigrations() {
const dbUrl = process.env.DATABASE_URL || ''
const sqlitePath = dbUrl.startsWith('file:') ? dbUrl.slice(5) : dbUrl
if (!sqlitePath) {
throw new Error('DATABASE_URL 未设置,且未提供有效的 SQLite 文件路径')
}
const sqlite = new Database(sqlitePath)
const db = drizzle(sqlite)
try {
console.log(`🚀 开始执行 SQLite 迁移... (${sqlitePath})`)
const migrationsFolder = path.resolve(process.cwd(), migrationsFolderRelative)
await migrate(db, {
migrationsFolder,
})
console.log('✅ SQLite 迁移完成!')
} catch (err) {
console.log('❌ 迁移失败:', err)
throw err
} finally {
sqlite.close()
}
}
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)
})
}