Dual-track reads
When “success” is the bug.
After a migration a PostgreSQL table can be empty while the JSON store still holds production
data. A query that “succeeds” with zero rows, if trusted, makes the whole admin console look
wiped. The dual-track layer decides — per domain — when PostgreSQL is authoritative and when
to fall back, so the rollout could be staged, and reversed, with one environment variable.
server/services/dual-track.js
// PG has rows → use PG
// PG table missing → fall back to JSON, or throw if authoritative
// PG empty, JSON has data → fall back, or trust PG if authoritative
// (a deleted row can never be "resurrected")
function isPgAuthoritative(domain) {
if (isTruthyEnv('PG_JSON_FALLBACK')) return false; // escape hatch
if (isTruthyEnv('PG_AUTHORITATIVE')) return true;
if (process.env.NODE_ENV === 'production') return true;
const list = String(process.env.PG_AUTHORITATIVE_DOMAINS || '')
.split(',').map((s) => s.trim()).filter(Boolean);
return !!(domain && list.includes(domain));
}
async function loadListPreferPg({ loadPg, loadJson, domain, autoSeed }) {
const authoritative = isPgAuthoritative(domain);
let pgList;
try { pgList = await loadPg(); }
catch (err) {
// only a missing table may fall back — never a network or auth error
if (!isMissingRelation(err) || authoritative) throw err;
return normalizeList(loadJson());
}
if (pgList.length > 0) return pgList;
const jsonList = normalizeList(loadJson());
if (jsonList.length && autoSeed) await autoSeed(jsonList); // idempotent
return authoritative ? pgList : jsonList;
}