56 lines
1.6 KiB
JavaScript
56 lines
1.6 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', '002_create_purchase_inventory.sql');
|
||
|
|
|
||
|
|
console.log('开始执行采购库存数据库迁移...');
|
||
|
|
console.log('数据库文件:', dbPath);
|
||
|
|
console.log('迁移脚本:', migrationPath);
|
||
|
|
|
||
|
|
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.serialize(() => {
|
||
|
|
const statements = migrationScript.split(';').filter(s => s.trim());
|
||
|
|
|
||
|
|
statements.forEach((stmt, index) => {
|
||
|
|
if (stmt.trim()) {
|
||
|
|
console.log(`执行语句 ${index + 1}/${statements.length}`);
|
||
|
|
db.run(stmt.trim(), (err) => {
|
||
|
|
if (err) {
|
||
|
|
if (err.message.includes('duplicate column name') ||
|
||
|
|
err.message.includes('already exists')) {
|
||
|
|
console.log(' 跳过(已存在)');
|
||
|
|
} else {
|
||
|
|
console.error(' 错误:', err.message);
|
||
|
|
}
|
||
|
|
} else {
|
||
|
|
console.log(' 成功');
|
||
|
|
}
|
||
|
|
});
|
||
|
|
}
|
||
|
|
});
|
||
|
|
});
|
||
|
|
|
||
|
|
db.all("SELECT name FROM sqlite_master WHERE type='table' ORDER BY name", (err, rows) => {
|
||
|
|
if (err) {
|
||
|
|
console.error('查询表失败:', err.message);
|
||
|
|
} else {
|
||
|
|
console.log('\n当前数据库表:');
|
||
|
|
rows.forEach(row => console.log('-', row.name));
|
||
|
|
}
|
||
|
|
|
||
|
|
db.close(() => {
|
||
|
|
console.log('\n迁移完成!');
|
||
|
|
});
|
||
|
|
});
|