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: 22 additions & 19 deletions migrations/1699529266062-tool-and-user-login-migration-renamings.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,23 @@
const mongoose = require('mongoose');
const { info, error } = require('../src/logger');

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

async function renameCollection(oldName, newName) {
async function aggregateAndDropCollection(oldName, newName) {
try {
await mongoose.connection.collection(oldName).rename(newName);
info(`Renamed collection ${oldName} to ${newName}`);
const { connection } = mongoose;

// Aggregation pipeline for copying the documents
const pipeline = [{ $match: {} }, { $out: newName }];

// Copy documents from the old collection to the new collection
await connection.collection(oldName).aggregate(pipeline).toArray();
info(`Aggregated and copied documents from ${oldName} to ${newName}`);

// Delete old collection
await connection.collection(oldName).drop();
info(`Dropped collection ${oldName}`);
} catch (err) {
error(`Error renaming collection ${oldName} to ${newName}: ${err.message}`);
error(`Error aggregating, copying, and deleting collection ${oldName} to ${newName}: ${err.message}`);
throw err;
}
}
Expand All @@ -17,27 +26,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 aggregateAndDropCollection('user_login_migrations', 'user-login-migrations');
await aggregateAndDropCollection('external_tools', 'external-tools');
await aggregateAndDropCollection('context_external_tools', 'context-external-tools');
await aggregateAndDropCollection('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 aggregateAndDropCollection('user-login-migrations', 'user_login_migrations');
await aggregateAndDropCollection('external-tools', 'external_tools');
await aggregateAndDropCollection('context-external-tools', 'context_external_tools');
await aggregateAndDropCollection('school-external-tools', 'school_external_tools');

await close();
},
Expand Down
Loading