diff --git a/packages/migration/src/migrations/application-config/20240708170106-remove-config-collections.ts b/packages/migration/src/migrations/application-config/20240708170106-remove-config-collections.ts index db151129b1..bb7dec7bc4 100644 --- a/packages/migration/src/migrations/application-config/20240708170106-remove-config-collections.ts +++ b/packages/migration/src/migrations/application-config/20240708170106-remove-config-collections.ts @@ -11,12 +11,30 @@ import { Db, MongoClient } from 'mongodb' +const dropCollectionsExcept = async (db: Db, collectionToKeep: string) => { + const allCollections = await db.listCollections().toArray() + const collectionNames = allCollections.map((col) => col.name) + const filteredCollectionNames = collectionNames.filter( + (c) => c !== collectionToKeep + ) + + for (const collection of filteredCollectionNames) { + await db + .collection(collection) + .drop() + .catch((error) => { + console.error(`Error dropping collection ${collection}:`, error) + }) + } + + console.log(`Removed collections: ${filteredCollectionNames.toString()}`) +} + export const up = async (db: Db, client: MongoClient) => { const session = client.startSession() try { await session.withTransaction(async () => { - const result = await db.dropDatabase() - console.log(`Database drop result: ${result}`) + await dropCollectionsExcept(db, 'changelog') }) } finally { console.log( diff --git a/packages/migration/src/migrations/openhim/20240305193551-remove-openhim-collections.ts b/packages/migration/src/migrations/openhim/20240305193551-remove-openhim-collections.ts index 9877c7b5f8..3594af7117 100644 --- a/packages/migration/src/migrations/openhim/20240305193551-remove-openhim-collections.ts +++ b/packages/migration/src/migrations/openhim/20240305193551-remove-openhim-collections.ts @@ -11,27 +11,30 @@ import { Db, MongoClient } from 'mongodb' +const dropCollectionsExcept = async (db: Db, collectionToKeep: string) => { + const allCollections = await db.listCollections().toArray() + const collectionNames = allCollections.map((col) => col.name) + const filteredCollectionNames = collectionNames.filter( + (c) => c !== collectionToKeep + ) + + for (const collection of filteredCollectionNames) { + await db + .collection(collection) + .drop() + .catch((error) => { + console.error(`Error dropping collection ${collection}:`, error) + }) + } + + console.log(`Removed collections: ${filteredCollectionNames.toString()}`) +} + export const up = async (db: Db, client: MongoClient) => { const session = client.startSession() try { await session.withTransaction(async () => { - const collectionToKeep = 'changelog' as const - const allCollections = await db.listCollections().toArray() - const collectionNames = allCollections.map((col) => col.name) - const filteredCollectionNames = collectionNames.filter( - (c) => c !== collectionToKeep - ) - - for (const collection of filteredCollectionNames) { - await db - .collection(collection) - .drop() - .catch((error) => { - console.error(`Error dropping collection ${collection}:`, error) - }) - } - - console.log(`Removed collections: ${filteredCollectionNames.toString()}`) + await dropCollectionsExcept(db, 'changelog') }) } finally { await session.endSession()