Reuse fixed SQL statements for observation pruning
This commit is contained in:
@@ -141,6 +141,31 @@ describe('routine host observation retention', () => {
|
||||
expect(api.handle.prepare("SELECT COUNT(*) AS count FROM ec_host_operations WHERE kind='instance.restart'").get()).toEqual({ count: 2000 });
|
||||
});
|
||||
|
||||
it('reuses the same three delete statements across different batch sizes', async () => {
|
||||
const api = await setup();
|
||||
const statements = new Map<string, ReturnType<typeof api.handle.prepare>>();
|
||||
for (const count of [1, 2, 7, 50, 500]) {
|
||||
api.handle.transaction(() => {
|
||||
const insert = api.handle.prepare(`INSERT INTO ec_host_operations
|
||||
(id,host_id,kind,payload,state,created_at,updated_at,observation_success)
|
||||
VALUES(?,?,'files.stat','{}','completed',?,?,1)`);
|
||||
for (let i = 0; i < count; i++) insert.run(`read-${count}-${i}`, api.host.id, at(2 * day), at(2 * day));
|
||||
})();
|
||||
const prepare = vi.spyOn(api.handle, 'prepare');
|
||||
expect(api.pruneHostObservations()).toBe(count);
|
||||
const deletes = prepare.mock.calls.map(([sql]) => sql).filter(sql => sql.startsWith('DELETE FROM ec_host_'));
|
||||
prepare.mockRestore();
|
||||
expect(deletes).toHaveLength(3);
|
||||
for (const sql of deletes) {
|
||||
const statement = api.handle.prepare(sql);
|
||||
if (statements.has(sql)) expect(statement).toBe(statements.get(sql));
|
||||
statements.set(sql, statement);
|
||||
}
|
||||
}
|
||||
expect(statements.size).toBe(3);
|
||||
expect(api.handle.pragma('foreign_key_check')).toEqual([]);
|
||||
});
|
||||
|
||||
it('backfills legacy observation classification once while preserving actions and negative or invalid results', async () => {
|
||||
const api = await setup();
|
||||
const read = api.add('files.manifest');
|
||||
|
||||
Reference in New Issue
Block a user