diff --git a/CHANGELOG.md b/CHANGELOG.md index 31a38c2..03cdd7c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,8 @@ ## Unreleased +- fixed: Do not create rolling-database list entries on `syncOnly` processes, so a sync-only process can no longer register a period database it will not physically create. + ## 0.2.24 (2026-03-18) - added: `syncedDocument` accepts an optional `SyncedDocumentOptions` object with `cleanFailStrategy` (`'preserve'` to keep the last good value, `'reset'` to revert to defaults) and `onCleanFail` callback for notification when the cleaner rejects a document. diff --git a/src/couchdb/rolling-database.ts b/src/couchdb/rolling-database.ts index 751c59b..8fd9b09 100644 --- a/src/couchdb/rolling-database.ts +++ b/src/couchdb/rolling-database.ts @@ -459,6 +459,7 @@ export function makeRollingDatabase( currentCluster = 'default', disableWatching = false, log = console.log, + syncOnly = false, watchCluster, onError = error => { log(`Error while maintaining "${name}" databases: ${String(error)}`) @@ -514,27 +515,38 @@ export function makeRollingDatabase( list = [{ archived, name, startDate }, ...list] } - // Create past databases if the list is empty: - if (list.length === 0) { - if (archiveStart == null) { - const [date, suffix] = pickPeriodicMonth(now, period, false) - await addDb(`${name}-${suffix}`, date) - } else { - for ( - let [date, suffix] = pickPeriodicMonth(archiveStart, 'year', false); - date.valueOf() < now.valueOf(); - [date, suffix] = pickPeriodicMonth(date, 'year', true) - ) { + // Only the authoritative (non-syncOnly) process may mutate the list. + // A syncOnly process reads the list and targets queries, but must never + // append a doc it cannot back with a physical database: + if (!syncOnly) { + // Create past databases if the list is empty: + if (list.length === 0) { + if (archiveStart == null) { + const [date, suffix] = pickPeriodicMonth(now, period, false) await addDb(`${name}-${suffix}`, date) + } else { + for ( + let [date, suffix] = pickPeriodicMonth( + archiveStart, + 'year', + false + ); + date.valueOf() < now.valueOf(); + [date, suffix] = pickPeriodicMonth(date, 'year', true) + ) { + await addDb(`${name}-${suffix}`, date) + } } } - } - // Always keep one spare database in the future: - const lastStart = list[0].startDate - if (lastStart.valueOf() < now.valueOf()) { - const [date, suffix] = pickPeriodicMonth(now, period, true) - await addDb(`${name}-${suffix}`, date) + // Always keep one spare database in the future: + if (list.length > 0) { + const lastStart = list[0].startDate + if (lastStart.valueOf() < now.valueOf()) { + const [date, suffix] = pickPeriodicMonth(now, period, true) + await addDb(`${name}-${suffix}`, date) + } + } } // Reset the cleanup list: