82e25de691
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
42 lines
2.5 KiB
JavaScript
42 lines
2.5 KiB
JavaScript
// docstore: a save whose body equals what MySQL already holds is not rewritten (2026-11-15: whole-document rewrites on
|
|
// every save filled core with 115 GB of binlogs). Runs against a fake mysql2 pool, no database needed.
|
|
'use strict';
|
|
const path = require('path'), fs = require('fs'), os = require('os');
|
|
const rows = new Map([['links.json', '{"a":1}']]); let writes = 0;
|
|
const conn = {
|
|
beginTransaction: async () => {}, commit: async () => {}, rollback: async () => {}, release() {},
|
|
query: async (sql, args) => {
|
|
if (/SELECT owner FROM docs_writer/.test(sql)) return [[{ owner: global.__ME }]];
|
|
if (/^INSERT INTO docs \(name, body, updated_at\) VALUES \(\?, \?, \?\) ON DUPLICATE KEY UPDATE body/.test(sql)) { writes++; rows.set(args[0], args[1]); return [{}]; }
|
|
if (/^DELETE FROM docs/.test(sql)) { rows.delete(args[0]); return [{}]; }
|
|
return [[]];
|
|
},
|
|
};
|
|
const pool = {
|
|
getConnection: async () => conn,
|
|
query: async (sql, args) => {
|
|
if (/INSERT INTO docs_writer/.test(sql)) { global.__ME = args[0]; return [{}]; }
|
|
if (/SELECT name, body, updated_at FROM docs/.test(sql)) return [[...rows].map(([name, body]) => ({ name, body, updated_at: 1 }))];
|
|
return [[]];
|
|
},
|
|
};
|
|
require.cache[require.resolve('mysql2/promise')] = { id: 'mysql2/promise', filename: require.resolve('mysql2/promise'), loaded: true, exports: { createPool: () => pool } };
|
|
const ds = require('../docstore');
|
|
let fails = 0; const ok = (c, m) => { console.log((c ? 'PASS ' : 'FAIL ') + m); if (!c) fails++; };
|
|
(async () => {
|
|
const dir = fs.mkdtempSync(path.join(os.tmpdir(), 'ds-'));
|
|
process.env.DOCSTORE_URL = 'mysql://fake/x';
|
|
ds.install(dir); await ds.init();
|
|
const f = path.join(dir, 'links.json');
|
|
fs.writeFileSync(f, '{"a":1}'); await ds.drain(2000);
|
|
ok(writes === 0, 'saving the same body MySQL already has writes nothing (' + writes + ' writes)');
|
|
fs.writeFileSync(f, '{"a":2}'); await ds.drain(2000);
|
|
ok(writes === 1 && rows.get('links.json') === '{"a":2}', 'a changed body is written once');
|
|
fs.writeFileSync(f, '{"a":2}'); await ds.drain(2000);
|
|
ok(writes === 1, 'saving it again unchanged writes nothing');
|
|
ok(fs.readFileSync(f, 'utf8') === '{"a":2}', 'reads still return the latest body');
|
|
fs.writeFileSync(f, '{"a":3}'); fs.writeFileSync(f, '{"a":2}'); await ds.drain(2000);
|
|
ok(rows.get('links.json') === '{"a":2}', 'a change undone before the flush leaves MySQL on the right body');
|
|
console.log(fails ? fails + ' FAILED' : 'all passed'); process.exit(fails ? 1 : 0);
|
|
})().catch(e => { console.error(e); process.exit(1); });
|