75 lines
2.6 KiB
JavaScript
75 lines
2.6 KiB
JavaScript
'use strict';
|
|||
|
|
|
||
|
|
const assert = require('node:assert/strict');
|
||
|
|
const { DatabaseSync } = require('node:sqlite');
|
||
|
|
|
||
|
|
const name = 'ix_audit_entity_action_id';
|
||
|
|
const masterEntry = {
|
||
|
|
type: 'index',
|
||
|
|
name,
|
||
|
|
table: 'audit',
|
||
|
|
sql: 'CREATE INDEX ix_audit_entity_action_id ON audit(entity,action,id)',
|
||
|
|
};
|
||
|
|
const listEntry = { name, unique: 0, columns: ['entity', 'action', 'id'] };
|
||
|
|
const definition = {
|
||
|
|
...masterEntry,
|
||
|
|
unique: 0,
|
||
|
|
origin: 'c',
|
||
|
|
partial: 0,
|
||
|
|
columns: [
|
||
|
|
{ seqno: 0, cid: 1, name: 'entity', desc: 0, coll: 'BINARY', key: 1 },
|
||
|
|
{ seqno: 1, cid: 2, name: 'action', desc: 0, coll: 'BINARY', key: 1 },
|
||
|
|
{ seqno: 2, cid: 0, name: 'id', desc: 0, coll: 'BINARY', key: 1 },
|
||
|
|
{ seqno: 3, cid: -1, name: null, desc: 0, coll: 'BINARY', key: 0 },
|
||
|
|
],
|
||
|
|
};
|
||
|
|
|
||
|
|
function insertExpected(entries, entry, follows) {
|
||
|
|
const i = entries.findIndex(follows);
|
||
|
|
return entries.toSpliced(i === -1 ? entries.length : i, 0, entry);
|
||
|
|
}
|
||
|
|
|
||
|
|
function expectedMaster(entries) {
|
||
|
|
const recorded = entries.filter(entry => entry.name === name);
|
||
|
|
if (recorded.length) {
|
||
|
|
assert.deepEqual(recorded, [masterEntry], '基线中的批准审计索引定义不符');
|
||
|
|
return entries;
|
||
|
|
}
|
||
|
|
return insertExpected(entries, masterEntry, row => row.type > 'index' || (row.type === 'index' && row.name > name));
|
||
|
|
}
|
||
|
|
|
||
|
|
function expectedLegacySchema(schema, { afterStartup = false } = {}) {
|
||
|
|
const recorded = Object.entries(schema).flatMap(([table, { indexes }]) =>
|
||
|
|
indexes.filter(index => index.name === name).map(index => ({ table, ...index })),
|
||
|
|
);
|
||
|
|
if (recorded.length) {
|
||
|
|
assert.deepEqual(recorded, [{ table: masterEntry.table, ...listEntry }], '基线中的批准审计索引定义不符');
|
||
|
|
return schema;
|
||
|
|
}
|
||
|
|
if (!afterStartup) return schema;
|
||
|
|
const { audit } = schema;
|
||
|
|
const indexes = insertExpected(audit.indexes, listEntry, row => row.name > name);
|
||
|
|
return { ...schema, audit: { ...audit, indexes } };
|
||
|
|
}
|
||
|
|
|
||
|
|
function inspect(filename) {
|
||
|
|
const db = new DatabaseSync(filename, { readOnly: true });
|
||
|
|
try {
|
||
|
|
const master = db
|
||
|
|
.prepare("SELECT type,name,tbl_name AS 'table',sql FROM sqlite_master WHERE type='index' AND name=?")
|
||
|
|
.get(name);
|
||
|
|
if (!master) return null;
|
||
|
|
const flags = db
|
||
|
|
.prepare('SELECT "unique",origin,partial FROM pragma_index_list(?) WHERE name=?')
|
||
|
|
.get(master.table, name);
|
||
|
|
const columns = db
|
||
|
|
.prepare('SELECT seqno,cid,name,"desc",coll,"key" FROM pragma_index_xinfo(?) ORDER BY seqno')
|
||
|
|
.all(name)
|
||
|
|
.map(row => ({ ...row }));
|
||
|
|
return { ...master, ...flags, columns };
|
||
|
|
} finally {
|
||
|
|
db.close();
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
module.exports = { definition, expectedMaster, expectedLegacySchema, inspect };
|