上线单个审计覆盖索引并严格核验旧库的结构增量
This commit is contained in:
1 parent
cf0b0be83f
commit
240452d8a1
5 files changed
+230
-6
No files matched your search
@@ -0,0 +1,131 @@
|
||||
'use strict';
|
||||
|
||||
const assert = require('node:assert/strict');
|
||||
const { spawnSync } = require('node:child_process');
|
||||
const { mkdtempSync, rmSync } = require('node:fs');
|
||||
const { tmpdir } = require('node:os');
|
||||
const { join } = require('node:path');
|
||||
const { DatabaseSync } = require('node:sqlite');
|
||||
const test = require('node:test');
|
||||
const { Ledger } = require('../core/ledger');
|
||||
|
||||
const name = 'ix_audit_entity_action_id';
|
||||
|
||||
function filename(t) {
|
||||
const dir = mkdtempSync(join(tmpdir(), 'weiguang-audit-index-'));
|
||||
t.after(() => rmSync(dir, { recursive: true, force: true }));
|
||||
return join(dir, 'ledger.sqlite');
|
||||
}
|
||||
|
||||
function assertIndex(db) {
|
||||
const found = db.prepare("SELECT * FROM pragma_index_list('audit') WHERE name=?").get(name);
|
||||
assert(found);
|
||||
assert.equal(found.unique, 0);
|
||||
assert.equal(found.origin, 'c');
|
||||
assert.equal(found.partial, 0);
|
||||
const columns = db.prepare('SELECT * FROM pragma_index_xinfo(?) ORDER BY seqno').all(name);
|
||||
assert.deepEqual(
|
||||
columns.filter(row => row.key).map(row => [row.name, row.desc, row.coll]),
|
||||
[
|
||||
['entity', 0, 'BINARY'],
|
||||
['action', 0, 'BINARY'],
|
||||
['id', 0, 'BINARY'],
|
||||
],
|
||||
);
|
||||
}
|
||||
|
||||
function seedOldAudit(file) {
|
||||
const db = new DatabaseSync(file);
|
||||
try {
|
||||
db.exec(`CREATE TABLE audit(id INTEGER PRIMARY KEY AUTOINCREMENT,entity TEXT NOT NULL,
|
||||
action TEXT NOT NULL,actor TEXT NOT NULL,detail TEXT NOT NULL,created TEXT NOT NULL)`);
|
||||
const insert = db.prepare('INSERT INTO audit(entity,action,actor,detail,created) VALUES (?,?,?,?,?)');
|
||||
const created = '2026-11-30T01:00:00.000Z';
|
||||
for (const [entity, action] of [
|
||||
['one', '保存草稿'],
|
||||
['two', '保存草稿'],
|
||||
['one', '复核'],
|
||||
['one', '保存草稿'],
|
||||
])
|
||||
insert.run(entity, action, '合成员工', '{}', created);
|
||||
return db.prepare('SELECT * FROM audit ORDER BY id').all();
|
||||
} finally {
|
||||
db.close();
|
||||
}
|
||||
}
|
||||
|
||||
test('新库启动创建一个普通审计索引,旧表升级和重复启动保留原行、唯一性与排序', t => {
|
||||
const fresh = new Ledger(':memory:');
|
||||
try {
|
||||
assertIndex(fresh.db);
|
||||
} finally {
|
||||
fresh.close();
|
||||
}
|
||||
const file = filename(t);
|
||||
const rows = seedOldAudit(file);
|
||||
for (let boot = 0; boot < 2; boot++) {
|
||||
const ledger = new Ledger(file);
|
||||
try {
|
||||
assertIndex(ledger.db);
|
||||
assert.deepEqual(ledger.rows('SELECT * FROM audit ORDER BY id'), rows);
|
||||
assert.deepEqual(
|
||||
ledger.rows('SELECT id FROM audit WHERE entity=? ORDER BY id DESC LIMIT 100', 'one').map(row => row.id),
|
||||
[4, 3, 1],
|
||||
);
|
||||
assert.equal(ledger.one('SELECT MIN(id) AS id FROM audit WHERE entity=? AND action=?', 'one', '保存草稿').id, 1);
|
||||
} finally {
|
||||
ledger.close();
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
test('打开索引事务后进程中断,未提交索引回滚,重启补齐且保留审计数据', t => {
|
||||
const file = filename(t);
|
||||
const rows = seedOldAudit(file);
|
||||
// 在独立进程包住实际启动 DDL,中断发生在 SQLite 已建好索引且尚未提交时。
|
||||
const stopped = spawnSync(
|
||||
process.execPath,
|
||||
[
|
||||
'-e',
|
||||
`
|
||||
const assert = require('node:assert/strict');
|
||||
const { DatabaseSync } = require('node:sqlite');
|
||||
const { writeSync } = require('node:fs');
|
||||
const { Ledger } = require(process.argv[1]);
|
||||
const original = DatabaseSync.prototype.exec;
|
||||
DatabaseSync.prototype.exec = function(sql) {
|
||||
if (!sql.includes('CREATE INDEX IF NOT EXISTS ix_audit_entity_action_id ON audit(entity,action,id)'))
|
||||
return original.call(this, sql);
|
||||
original.call(this, 'BEGIN IMMEDIATE');
|
||||
original.call(this, sql);
|
||||
assert(this.isTransaction);
|
||||
assert(this.prepare("SELECT name FROM sqlite_master WHERE name='ix_audit_entity_action_id'").get());
|
||||
writeSync(1, 'index created in open transaction');
|
||||
process.kill(process.pid, 'SIGKILL');
|
||||
};
|
||||
new Ledger(process.argv[2]);
|
||||
`,
|
||||
require.resolve('../core/ledger'),
|
||||
file,
|
||||
],
|
||||
{ encoding: 'utf8', timeout: 10000 },
|
||||
);
|
||||
assert.equal(stopped.stdout, 'index created in open transaction', stopped.stderr);
|
||||
assert.equal(stopped.signal, 'SIGKILL', stopped.stderr);
|
||||
|
||||
const recovering = new DatabaseSync(file);
|
||||
try {
|
||||
assert.equal(recovering.prepare('SELECT name FROM sqlite_master WHERE name=?').get(name), undefined);
|
||||
assert.deepEqual(recovering.prepare('SELECT * FROM audit ORDER BY id').all(), rows);
|
||||
assert.equal(recovering.prepare('PRAGMA quick_check').get().quick_check, 'ok');
|
||||
} finally {
|
||||
recovering.close();
|
||||
}
|
||||
const ledger = new Ledger(file);
|
||||
try {
|
||||
assertIndex(ledger.db);
|
||||
assert.deepEqual(ledger.rows('SELECT * FROM audit ORDER BY id'), rows);
|
||||
} finally {
|
||||
ledger.close();
|
||||
}
|
||||
});
|
||||
Reference in new issue
Block a user