fix: harden financial transaction boundaries

This commit is contained in:
2026-07-18 15:33:41 +08:00
parent 4af7acbeaa
commit 5836b421d8
19 changed files with 514 additions and 314 deletions

View File

@@ -117,6 +117,44 @@ export class DatabaseMigrationsService implements OnApplicationBootstrap {
description VARCHAR(300), recorded_by INTEGER,
created_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP
)`);
await runner.query(`CREATE TABLE IF NOT EXISTS financial_operations (
id ${pk}, operation_id VARCHAR(64) NOT NULL UNIQUE, type VARCHAR(64) NOT NULL,
status VARCHAR(20) NOT NULL DEFAULT 'running', result_json TEXT, error_message VARCHAR(500),
created_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
updated_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP
)`);
const walletTransactions = await runner.getTable('wallet_transactions');
if (walletTransactions) {
const columns = new Set(walletTransactions.columns.map((column) => column.name));
if (!columns.has('operation_id')) {
await runner.query('ALTER TABLE wallet_transactions ADD COLUMN operation_id VARCHAR(64)');
}
}
const billItems = await runner.getTable('bill_items');
if (billItems) {
const columns = new Set(billItems.columns.map((column) => column.name));
for (const [name, definition] of [
['room_expense_id', 'INTEGER'],
['personal_expense_id', 'INTEGER'],
]) {
if (!columns.has(name)) await runner.query(`ALTER TABLE bill_items ADD COLUMN ${name} ${definition}`);
}
}
const roomExpenses = await runner.getTable('room_expenses');
if (roomExpenses) {
const columns = new Set(roomExpenses.columns.map((column) => column.name));
if (!columns.has('import_key')) {
await runner.query('ALTER TABLE room_expenses ADD COLUMN import_key VARCHAR(120)');
}
const refreshedRoomExpenses = await runner.getTable('room_expenses');
const hasImportKey = refreshedRoomExpenses?.indices.some((index) =>
index.isUnique && index.columnNames.length === 1 && index.columnNames[0] === 'import_key');
if (!hasImportKey) {
await runner.query(isMySQL
? 'CREATE UNIQUE INDEX idx_room_expenses_import_key ON room_expenses (import_key)'
: 'CREATE UNIQUE INDEX IF NOT EXISTS idx_room_expenses_import_key ON room_expenses (import_key)');
}
}
const bills = await runner.getTable('bills');
if (bills) {
const columns = new Set(bills.columns.map((column) => column.name));