Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: amend the deletion of collection instead of dropping db #7696

Merged
merged 4 commits into from
Oct 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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')
naftis marked this conversation as resolved.
Show resolved Hide resolved
})
} finally {
await session.endSession()
Expand Down
Loading