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

N21-1029 bugfix renaming of collections #4551

Merged
merged 8 commits into from
Nov 10, 2023
41 changes: 23 additions & 18 deletions migrations/1699529266062-tool-and-user-login-migration-renamings.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,25 @@
const mongoose = require('mongoose');
const { info, error } = require('../src/logger');

const { connect, close } = require('../src/utils/database');

async function renameCollection(oldName, newName) {
async function renameAndDeleteCollection(oldName, newName) {
try {
await mongoose.connection.collection(oldName).rename(newName);
const { connection } = mongoose;

// Check whether the new collection already exists
const newCollectionExists = await connection.db.listCollections({ name: newName }).hasNext();

if (newCollectionExists) {
// If the new collection already exists, delete it
await connection.collection(newName).drop();
info(`Dropped existing collection ${newName}`);
}

// Rename the old collection to the new collection
await connection.collection(oldName).rename(newName);
info(`Renamed collection ${oldName} to ${newName}`);
} catch (err) {
error(`Error renaming collection ${oldName} to ${newName}: ${err.message}`);
error(`Error renaming and deleting collection ${oldName} to ${newName}: ${err.message}`);
throw err;
}
}
Expand All @@ -17,27 +28,21 @@ module.exports = {
up: async function up() {
await connect();

await renameCollection('user_login_migrations', 'user-login-migrations');

await renameCollection('external_tools', 'external-tools');

await renameCollection('context_external_tools', 'context-external-tools');

await renameCollection('school_external_tools', 'school-external-tools');
await renameAndDeleteCollection('user_login_migrations', 'user-login-migrations');
await renameAndDeleteCollection('external_tools', 'external-tools');
await renameAndDeleteCollection('context_external_tools', 'context-external-tools');
await renameAndDeleteCollection('school_external_tools', 'school-external-tools');

await close();
},

down: async function down() {
await connect();

await renameCollection('user-login-migrations', 'user_login_migrations');

await renameCollection('external-tools', 'external_tools');

await renameCollection('context-external-tools', 'context_external_tools');

await renameCollection('school-external-tools', 'school_external_tools');
await renameAndDeleteCollection('user-login-migrations', 'user_login_migrations');
await renameAndDeleteCollection('external-tools', 'external_tools');
await renameAndDeleteCollection('context-external-tools', 'context_external_tools');
await renameAndDeleteCollection('school-external-tools', 'school_external_tools');

await close();
},
Expand Down
Loading