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.
49 lines
1.3 KiB
49 lines
1.3 KiB
import { drizzle } from 'drizzle-orm/libsql'
|
|
import { migrate } from 'drizzle-orm/libsql/migrator'
|
|
import { createClient } from '@libsql/client'
|
|
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 client = createClient({ url: `file:${sqlitePath}` })
|
|
const db = drizzle(client)
|
|
|
|
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
|
|
}
|
|
}
|
|
|
|
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)
|
|
})
|
|
}
|
|
|