46 lines
1.2 KiB
JavaScript
46 lines
1.2 KiB
JavaScript
const sqlite3 = require('sqlite3').verbose();
|
|
const path = require('path');
|
|
const fs = require('fs');
|
|
|
|
// 数据库路径
|
|
const dbPath = path.join(__dirname, 'company_finance.db');
|
|
const migrationPath = path.join(__dirname, 'migrations', '001_create_category_tree.sql');
|
|
|
|
console.log('开始执行数据库迁移...');
|
|
console.log('数据库文件:', dbPath);
|
|
|
|
// 读取迁移脚本
|
|
const migrationScript = fs.readFileSync(migrationPath, 'utf8');
|
|
|
|
// 连接数据库
|
|
const db = new sqlite3.Database(dbPath, (err) => {
|
|
if (err) {
|
|
console.error('数据库连接失败:', err.message);
|
|
process.exit(1);
|
|
}
|
|
console.log('数据库连接成功');
|
|
});
|
|
|
|
// 执行迁移
|
|
db.exec(migrationScript, (err) => {
|
|
if (err) {
|
|
console.error('迁移执行失败:', err.message);
|
|
process.exit(1);
|
|
}
|
|
console.log('迁移执行成功!');
|
|
|
|
// 验证结果
|
|
db.all("SELECT name FROM sqlite_master WHERE type='table'", (err, rows) => {
|
|
if (err) {
|
|
console.error('查询表失败:', err.message);
|
|
} else {
|
|
console.log('当前数据库表:');
|
|
rows.forEach(row => console.log('-', row.name));
|
|
}
|
|
|
|
db.close(() => {
|
|
console.log('数据库连接已关闭');
|
|
});
|
|
});
|
|
});
|