|
| 1 | +/** |
| 2 | + * Regression guards for harper#2049: on RocksDB, all tables in a database share one |
| 3 | + * transaction log with no per-table purge granularity, so a table-scoped |
| 4 | + * delete_transaction_logs_before used to silently purge EVERY table's log in the |
| 5 | + * database. A nonexistent table name (e.g. a typo) fell through to the same |
| 6 | + * whole-database purge. Both must be rejected before any purge happens. |
| 7 | + */ |
| 8 | +require('../testUtils'); |
| 9 | +const assert = require('node:assert'); |
| 10 | +const { setupTestDBPath } = require('../testUtils'); |
| 11 | +const { table } = require('#src/resources/databases'); |
| 12 | +const { setMainIsWorker } = require('#js/server/threads/manageThreads'); |
| 13 | +const harperBridge = require('#src/dataLayer/harperBridge/harperBridge').default; |
| 14 | + |
| 15 | +describe('deleteTransactionLogsBefore on RocksDB (harper#2049)', () => { |
| 16 | + if (process.env.HARPER_STORAGE_ENGINE === 'lmdb') return; |
| 17 | + |
| 18 | + const DB = 'txnLogPurgeScope'; |
| 19 | + let TableA, TableB; |
| 20 | + |
| 21 | + before(async () => { |
| 22 | + setupTestDBPath(); |
| 23 | + setMainIsWorker(true); |
| 24 | + const attributes = [{ name: 'id', isPrimaryKey: true }, { name: 'name' }]; |
| 25 | + TableA = table({ table: 'TableA', database: DB, attributes }); |
| 26 | + TableB = table({ table: 'TableB', database: DB, attributes }); |
| 27 | + await TableA.put(1, { name: 'a1' }); |
| 28 | + await TableA.put(2, { name: 'a2' }); |
| 29 | + await TableB.put(1, { name: 'b1' }); |
| 30 | + }); |
| 31 | + |
| 32 | + async function historyCount(tbl) { |
| 33 | + let count = 0; |
| 34 | + for await (const _entry of tbl.getHistory()) count++; |
| 35 | + return count; |
| 36 | + } |
| 37 | + |
| 38 | + it('rejects a table-scoped delete with a 400 and purges nothing', async () => { |
| 39 | + await assert.rejects( |
| 40 | + harperBridge.deleteTransactionLogsBefore({ database: DB, table: 'TableA', timestamp: Date.now() + 1000 }), |
| 41 | + (error) => { |
| 42 | + assert.strictEqual(error.statusCode, 400); |
| 43 | + assert.match(error.message, /not supported for RocksDB/); |
| 44 | + return true; |
| 45 | + } |
| 46 | + ); |
| 47 | + assert.ok((await historyCount(TableA)) >= 2, 'TableA history should be untouched'); |
| 48 | + assert.ok((await historyCount(TableB)) >= 1, 'TableB history should be untouched'); |
| 49 | + }); |
| 50 | + |
| 51 | + it('rejects a nonexistent table with a 404 instead of purging the whole database', async () => { |
| 52 | + await assert.rejects( |
| 53 | + harperBridge.deleteTransactionLogsBefore({ database: DB, table: 'NoSuchTable', timestamp: Date.now() + 1000 }), |
| 54 | + (error) => { |
| 55 | + assert.strictEqual(error.statusCode, 404); |
| 56 | + return true; |
| 57 | + } |
| 58 | + ); |
| 59 | + assert.ok((await historyCount(TableA)) >= 2, 'TableA history should be untouched'); |
| 60 | + assert.ok((await historyCount(TableB)) >= 1, 'TableB history should be untouched'); |
| 61 | + }); |
| 62 | + |
| 63 | + it('treats a falsy table name as table-scoped rather than database-wide', async () => { |
| 64 | + // A table named "0" addressed numerically must not fall into the no-table |
| 65 | + // branch (which would purge the whole database's log on RocksDB). |
| 66 | + table({ table: '0', database: DB, attributes: [{ name: 'id', isPrimaryKey: true }] }); |
| 67 | + await assert.rejects( |
| 68 | + harperBridge.deleteTransactionLogsBefore({ database: DB, table: 0, timestamp: Date.now() + 1000 }), |
| 69 | + (error) => { |
| 70 | + assert.strictEqual(error.statusCode, 400); |
| 71 | + assert.match(error.message, /not supported for RocksDB/); |
| 72 | + return true; |
| 73 | + } |
| 74 | + ); |
| 75 | + }); |
| 76 | + |
| 77 | + it('rejects a nonexistent database with a 404', async () => { |
| 78 | + await assert.rejects( |
| 79 | + harperBridge.deleteTransactionLogsBefore({ database: 'NoSuchDatabase', timestamp: Date.now() + 1000 }), |
| 80 | + (error) => { |
| 81 | + assert.strictEqual(error.statusCode, 404); |
| 82 | + return true; |
| 83 | + } |
| 84 | + ); |
| 85 | + }); |
| 86 | + |
| 87 | + it('still performs the database-wide purge when no table is given', async () => { |
| 88 | + const results = await harperBridge.deleteTransactionLogsBefore({ database: DB, timestamp: Date.now() + 1000 }); |
| 89 | + assert.ok(results, 'should return results rather than throw'); |
| 90 | + assert.strictEqual(typeof results.entries_deleted, 'number'); |
| 91 | + assert.strictEqual(typeof results.log_files_deleted, 'number'); |
| 92 | + }); |
| 93 | +}); |
0 commit comments