Skip to content

Commit

Permalink
N21-1029 checks if new table already exists, because of starting of m…
Browse files Browse the repository at this point in the history
…igrations and server at the same time
  • Loading branch information
arnegns committed Nov 10, 2023
1 parent e7a9584 commit 24f2ba0
Showing 1 changed file with 23 additions and 18 deletions.
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

0 comments on commit 24f2ba0

Please sign in to comment.