82 lines
3.5 KiB
JavaScript
82 lines
3.5 KiB
JavaScript
'use strict';
|
||||
|
|
// 正式启动前的升级备份与显式迁移。
|
|||
|
|
const fs = require('node:fs');
|
|||
|
|
const path = require('node:path');
|
|||
|
|
const crypto = require('node:crypto');
|
|||
|
|
const { DatabaseSync, backup } = require('node:sqlite');
|
|||
|
|
|
|||
|
|
// 在 Ledger 构造器执行任何增量建表前备份旧库;只在显式正式启动路径调用。
|
|||
|
|
async function prepareTeamStartupDatabase(dataDir) {
|
|||
|
|
const filename = path.join(dataDir, 'ledger.sqlite');
|
|||
|
|
if (!fs.existsSync(filename)) return { required: false, backupName: null };
|
|||
|
|
const source = new DatabaseSync(filename, { readOnly: true });
|
|||
|
|
let required, backupName;
|
|||
|
|
try {
|
|||
|
|
const hasMeta = source.prepare("SELECT name FROM sqlite_master WHERE type='table' AND name='meta'").get();
|
|||
|
|
const teamReady = hasMeta && source.prepare("SELECT value FROM meta WHERE key='team-mode-v1'").get();
|
|||
|
|
const materialRule =
|
|||
|
|
hasMeta && source.prepare("SELECT value FROM meta WHERE key='team-material-funding-rule'").get();
|
|||
|
|
let materialReady = false;
|
|||
|
|
try {
|
|||
|
|
materialReady = JSON.parse(materialRule?.value || 'null')?.version >= 2;
|
|||
|
|
} catch {}
|
|||
|
|
required = !(teamReady && materialReady);
|
|||
|
|
if (!required) return { required: false, backupName: null };
|
|||
|
|
backupName =
|
|||
|
|
(teamReady ? 'ledger-before-material-usage-v2-' : 'ledger-before-team-v1-') +
|
|||
|
|
new Date().toISOString().replace(/[:.]/g, '-') +
|
|||
|
|
'-' +
|
|||
|
|
crypto.randomUUID().slice(0, 8) +
|
|||
|
|
'.sqlite';
|
|||
|
|
const directory = path.join(dataDir, 'backups');
|
|||
|
|
fs.mkdirSync(directory, { recursive: true, mode: 0o700 });
|
|||
|
|
const destination = path.join(directory, backupName);
|
|||
|
|
await backup(source, destination);
|
|||
|
|
fs.chmodSync(destination, 0o600);
|
|||
|
|
const check = new DatabaseSync(destination, { readOnly: true });
|
|||
|
|
try {
|
|||
|
|
if (check.prepare('PRAGMA integrity_check').get().integrity_check !== 'ok')
|
|||
|
|
throw new Error('三人台账升级前备份完整性检查失败');
|
|||
|
|
} finally {
|
|||
|
|
check.close();
|
|||
|
|
}
|
|||
|
|
return { required: true, backupName };
|
|||
|
|
} finally {
|
|||
|
|
source.close();
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
// 正式启动先备份再显式迁移,完成全部不变量核对后才能监听;createApp 保持同步的测试/嵌入入口。
|
|||
|
|
async function prepareStartupMigrations(app) {
|
|||
|
|
const status = app.ledger.materialPoolAllocationStatus();
|
|||
|
|
if (!status.migrationRequired) return { changed: false, ...status };
|
|||
|
|
let backupName = null;
|
|||
|
|
if (status.usageCount) {
|
|||
|
|
backupName =
|
|||
|
|
'ledger-before-material-fifo-v' +
|
|||
|
|
status.targetVersion +
|
|||
|
|
'-' +
|
|||
|
|
new Date().toISOString().replace(/[:.]/g, '-') +
|
|||
|
|
'-' +
|
|||
|
|
crypto.randomUUID().slice(0, 8) +
|
|||
|
|
'.sqlite';
|
|||
|
|
const destination = path.join(app.dataDir, 'backups', backupName);
|
|||
|
|
await app.ledger.backupTo(destination);
|
|||
|
|
if (!fs.statSync(destination).isFile() || !fs.statSync(destination).size)
|
|||
|
|
throw new Error('日购料升级前备份未完整写入');
|
|||
|
|
const check = new DatabaseSync(destination);
|
|||
|
|
try {
|
|||
|
|
check.exec('PRAGMA query_only=ON');
|
|||
|
|
if (check.prepare('PRAGMA integrity_check').get().integrity_check !== 'ok')
|
|||
|
|
throw new Error('日购料升级前备份完整性检查失败');
|
|||
|
|
} finally {
|
|||
|
|
check.close();
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
const actor = {
|
|||
|
|
id: 'system-material-pool-fifo-v' + status.targetVersion,
|
|||
|
|
name: '系统升级(日购料垫付FIFO v' + status.targetVersion + ')',
|
|||
|
|
role: 'system',
|
|||
|
|
};
|
|||
|
|
return app.ledger.migrateMaterialPoolAllocation(actor, { backupName });
|
|||
|
|
}
|
|||
|
|
module.exports = { prepareTeamStartupDatabase, prepareStartupMigrations };
|