mirror of
https://github.com/beilunyang/moemail.git
synced 2025-10-05 15:37:03 +08:00
refactor: Update database migration scripts and enhance CI workflow
This commit is contained in:
67
scripts/migrate.ts
Normal file
67
scripts/migrate.ts
Normal file
@@ -0,0 +1,67 @@
|
||||
import { parse } from '@iarna/toml'
|
||||
import { readFileSync } from 'fs'
|
||||
import { exec } from 'child_process'
|
||||
import { promisify } from 'util'
|
||||
import { join } from 'path'
|
||||
|
||||
const execAsync = promisify(exec)
|
||||
|
||||
interface D1Database {
|
||||
binding: string
|
||||
database_name: string
|
||||
database_id: string
|
||||
}
|
||||
|
||||
interface WranglerConfig {
|
||||
d1_databases: D1Database[]
|
||||
}
|
||||
|
||||
async function migrate() {
|
||||
try {
|
||||
// Get command line arguments
|
||||
const args = process.argv.slice(2)
|
||||
const mode = args[0]
|
||||
|
||||
if (!mode || !['local', 'remote'].includes(mode)) {
|
||||
console.error('Error: Please specify mode (local or remote)')
|
||||
process.exit(1)
|
||||
}
|
||||
|
||||
// Read wrangler.toml
|
||||
const wranglerPath = join(process.cwd(), 'wrangler.toml')
|
||||
let wranglerContent: string
|
||||
|
||||
try {
|
||||
wranglerContent = readFileSync(wranglerPath, 'utf-8')
|
||||
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
||||
} catch (error) {
|
||||
console.error('Error: wrangler.toml not found')
|
||||
process.exit(1)
|
||||
}
|
||||
|
||||
// Parse wrangler.toml
|
||||
const config = parse(wranglerContent) as unknown as WranglerConfig
|
||||
|
||||
if (!config.d1_databases?.[0]?.database_name) {
|
||||
console.error('Error: Database name not found in wrangler.toml')
|
||||
process.exit(1)
|
||||
}
|
||||
|
||||
const dbName = config.d1_databases[0].database_name
|
||||
|
||||
// Generate migrations
|
||||
console.log('Generating migrations...')
|
||||
await execAsync('drizzle-kit generate')
|
||||
|
||||
// Apply migrations
|
||||
console.log(`Applying migrations to ${mode} database: ${dbName}`)
|
||||
await execAsync(`wrangler d1 migrations apply ${dbName} --${mode}`)
|
||||
|
||||
console.log('Migration completed successfully!')
|
||||
} catch (error) {
|
||||
console.error('Migration failed:', error)
|
||||
process.exit(1)
|
||||
}
|
||||
}
|
||||
|
||||
migrate()
|
Reference in New Issue
Block a user