diff --git a/services/web/app/src/infrastructure/mongodb.mjs b/services/web/app/src/infrastructure/mongodb.mjs index d61f45f5f0..1032f0da83 100644 --- a/services/web/app/src/infrastructure/mongodb.mjs +++ b/services/web/app/src/infrastructure/mongodb.mjs @@ -54,6 +54,7 @@ export const db = { groupAuditLogEntries: internalDb.collection('groupAuditLogEntries'), institutions: internalDb.collection('institutions'), libraryReferences: internalDb.collection('libraryReferences'), + librarySyncStates: internalDb.collection('librarySyncStates'), messages: internalDb.collection('messages'), migrations: internalDb.collection('migrations'), notifications: internalDb.collection('notifications'), diff --git a/services/web/scripts/zotero/clear_synced_references.mjs b/services/web/scripts/zotero/clear_synced_references.mjs new file mode 100644 index 0000000000..250f55a124 --- /dev/null +++ b/services/web/scripts/zotero/clear_synced_references.mjs @@ -0,0 +1,101 @@ +// @ts-check +import minimist from 'minimist' +import logger from '@overleaf/logger' +import { db, ObjectId } from '../../app/src/infrastructure/mongodb.mjs' +import { scriptRunner } from '../lib/ScriptRunner.mjs' + +// Only zotero is wired up today; the flag exists so the script generalises to +// other reference providers without a rewrite. +const SUPPORTED_SOURCES = ['zotero'] + +const argv = minimist(process.argv.slice(2), { + boolean: ['commit', 'keep-sync-state', 'help'], + string: ['user-id', 'source'], + default: { source: 'zotero' }, +}) + +function usage() { + logger.info( + {}, + `Usage: node scripts/zotero/clear_synced_references.mjs --user-id [options] + +Resets a user's reference-sync state for local debugging. Deletes every +libraryReferences entry linked to the given source (sources. present) +and removes the user's librarySyncStates rows for that provider. + +Options: + --user-id Required. The Overleaf user _id to reset. + --source Reference source to clear (default: zotero). + Supported: ${SUPPORTED_SOURCES.join(', ')}. + --commit Apply changes. Without this, runs as a dry run. + --keep-sync-state Only delete the references; leave the + librarySyncStates rows untouched. +` + ) +} + +if (argv.help || !argv['user-id']) { + usage() + process.exit(argv.help ? 0 : 1) +} + +const source = argv.source +if (!SUPPORTED_SOURCES.includes(source)) { + logger.error( + { source }, + `unsupported source; expected one of ${SUPPORTED_SOURCES.join(', ')}` + ) + process.exit(1) +} + +const userId = new ObjectId(argv['user-id']) + +/** @param {(message: string) => Promise} trackProgress */ +async function main(trackProgress) { + if (!argv.commit) { + await trackProgress('DRY RUN. Pass --commit to apply changes.') + } + + const refFilter = { userId, [`sources.${source}`]: { $exists: true } } + const refCount = await db.libraryReferences.countDocuments(refFilter) + if (argv.commit) { + const result = await db.libraryReferences.deleteMany(refFilter) + await trackProgress( + `deleted ${result.deletedCount} ${source}-linked references` + ) + } else { + await trackProgress( + `[dry-run] would delete ${refCount} ${source}-linked references` + ) + } + + if (argv['keep-sync-state']) { + return + } + + const stateFilter = { userId, provider: source } + if (argv.commit) { + const result = await db.librarySyncStates.deleteMany(stateFilter) + await trackProgress( + `deleted ${result.deletedCount} librarySyncStates row(s)` + ) + } else { + const stateCount = await db.librarySyncStates.countDocuments(stateFilter) + await trackProgress( + `[dry-run] would delete ${stateCount} librarySyncStates row(s)` + ) + } +} + +try { + await scriptRunner(main, { + userId: argv['user-id'], + source, + commit: Boolean(argv.commit), + keepSyncState: Boolean(argv['keep-sync-state']), + }) + process.exit(0) +} catch (err) { + logger.error({ err }, 'clear_synced_references failed') + process.exit(1) +} diff --git a/tools/migrations/20260604174200_zotero_libraryReferences_sources.mjs b/tools/migrations/20260604174200_zotero_libraryReferences_sources.mjs new file mode 100644 index 0000000000..27577ac228 --- /dev/null +++ b/tools/migrations/20260604174200_zotero_libraryReferences_sources.mjs @@ -0,0 +1,42 @@ +import { batchedUpdate } from '@overleaf/mongo-utils/batchedUpdate.js' +import Helpers from './lib/helpers.mjs' + +const tags = ['saas'] + +const indexes = [ + { + key: { + userId: 1, + 'sources.zotero.libraryType': 1, + 'sources.zotero.libraryId': 1, + 'sources.zotero.itemKey': 1, + }, + name: 'userId_1_sources.zotero.libraryType_1_sources.zotero.libraryId_1_sources.zotero.itemKey_1', + unique: true, + // Not `sparse`: userId is always present, so a sparse compound index would + // include every doc and collide native entries on (userId, null, null, + // null). Scope uniqueness to Zotero-linked docs only. + partialFilterExpression: { 'sources.zotero.itemKey': { $exists: true } }, + }, +] + +const migrate = async client => { + const { db } = client + await batchedUpdate( + db.libraryReferences, + { sources: { $exists: false } }, + { $set: { sources: {} } } + ) + await Helpers.addIndexesToCollection(db.libraryReferences, indexes) +} + +const rollback = async client => { + const { db } = client + await Helpers.dropIndexesFromCollection(db.libraryReferences, indexes) +} + +export default { + tags, + migrate, + rollback, +} diff --git a/tools/migrations/20260609120000_create_librarySyncStates_collection.mjs b/tools/migrations/20260609120000_create_librarySyncStates_collection.mjs new file mode 100644 index 0000000000..8a99cf0ea9 --- /dev/null +++ b/tools/migrations/20260609120000_create_librarySyncStates_collection.mjs @@ -0,0 +1,28 @@ +import Helpers from './lib/helpers.mjs' +import { getCollectionInternal } from './lib/mongodb.mjs' + +const tags = ['saas'] + +const indexes = [ + { + key: { userId: 1, provider: 1, libraryType: 1, libraryId: 1 }, + name: 'userId_1_provider_1_libraryType_1_libraryId_1', + unique: true, + }, +] + +const migrate = async () => { + const librarySyncStates = await getCollectionInternal('librarySyncStates') + await Helpers.addIndexesToCollection(librarySyncStates, indexes) +} + +const rollback = async () => { + const librarySyncStates = await getCollectionInternal('librarySyncStates') + await Helpers.dropIndexesFromCollection(librarySyncStates, indexes) +} + +export default { + tags, + migrate, + rollback, +}