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

Replace migration mechanism #2654

Merged
merged 3 commits into from
Oct 3, 2022
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
12 changes: 12 additions & 0 deletions conf/migrations.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
const conf = require('./config.json');

module.exports = {
mongodb: {
url: conf.dbConnectionUri,
options: { useNewUrlParser: true }
},
migrationsDir: "migrations",
changelogCollectionName: "changelog",
migrationFileExtension: ".js",
useFileHash: false
};
14 changes: 1 addition & 13 deletions install.js
Original file line number Diff line number Diff line change
Expand Up @@ -316,7 +316,7 @@ function start() {
createMasterTenant,
createSuperUser,
buildFrontend,
syncMigrations
installHelpers.runMigrations
], function(error, results) {
if(error) {
console.error('ERROR: ', error);
Expand Down Expand Up @@ -517,18 +517,6 @@ function buildFrontend(callback) {
});
}

//As this is a fresh install we dont need to run the migrations so add them to the db and set them to up
function syncMigrations(callback) {
installHelpers.syncMigrations(function(err, migrations) {
database.getDatabase(function(err, db) {
if(err) {
return callback(err);
}
db.update('migration', {}, {'state': 'up'}, callback)
}, masterTenant._id)
});
}

// helper functions

function addConfig(newConfigItems) {
Expand Down
54 changes: 18 additions & 36 deletions lib/installHelpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ var inquirer = require('inquirer');
var logger = require('./logger');
var logUpdate = require('log-update');
var mailer = require('./mailer');
var migrateMongoose = require('migrate-mongoose');
var migrateMongo = require('migrate-mongo');
var optimist = require('optimist');
var path = require('path');
var request = require('request');
Expand Down Expand Up @@ -71,8 +71,7 @@ var exports = module.exports = {
getInstalledVersions,
getLatestVersions,
getUpdateData,
getMigrationConfig,
syncMigrations,
runMigrations,
installFramework,
updateFramework,
cloneRepo,
Expand Down Expand Up @@ -313,39 +312,22 @@ function getFrameworkRoot() {
return path.join(configuration.serverRoot, 'temp', configuration.getConfig('masterTenantID'), 'adapt_framework');
}

function getMigrationConfig(callback) {
var confPath = path.join(configuration.serverRoot, 'conf', 'migrate.json');
fs.readJson(confPath, callback);
}

/**
* Adds all migrations to the DB in a down state
* @param callback
*/
function syncMigrations(callback) {
getMigrationConfig(function(err, config) {
if(err){
return callback(err)
}

fs.ensureDir(config.migrationsDir, function(err) {
if(err){
return callback(err)
}

var migrator = new migrateMongoose({
migrationsPath: config.migrationsDir,
dbConnectionUri: config.dbConnectionUri,
autosync: true
});

migrator.sync()
.then(
function(value) { return callback(null, value) },
function(reason) { return callback(reason); }
)
});
});
async function runMigrations(callback) {
if(!callback) {
throw new Error('Migrator#runTask expects a callback parameter');
}
const migrateMongo = require('migrate-mongo');
const app = require('./application')();

const { client, db } = app.db.conn;
let error, data;
try {
migrateMongo.config.set(this.config);
data = await migrateMongo.up(db, client);
} catch(e) {
error = e;
}
callback(error, data);
}

/**
Expand Down
17 changes: 17 additions & 0 deletions migrations/20181108112051-purgesessions.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
/**
* GENERAL NOTES:
*
* Both functions should return a Promise (and/or use async & await).
*
* You can either access the DB directly via the Node.js MongoDB driver and the db param
* See https://mongodb.github.io/node-mongodb-native/4.4/
*
* You can also use the Origin APIs using the getApp function:
* await this.getApp()
*/
module.exports = {
async up(db, client) {
return db.dropCollection('sessions');
},
async down(db, client) {}
};
34 changes: 34 additions & 0 deletions migrations/20190411153144-replacepurgedroles.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/**
* GENERAL NOTES:
*
* Both functions should return a Promise (and/or use async & await).
*
* You can either access the DB directly via the Node.js MongoDB driver and the db param
* See https://mongodb.github.io/node-mongodb-native/4.4/
*
* You can also use the Origin APIs using the getApp function:
* await this.getApp()
*/
module.exports = {
async up(db, client) {
const roles = db.collection('roles');
const users = db.collection('users');
const replacements = [
['Product Manager', 'Course Creator'],
['Tenant Admin', 'Super Admin']
];
await Promise.all(replacements.map(async ([oldName, newName]) => {
const [oldRole, newRole] = await Promise.all([ roles.find({ name: oldName }), roles.find({ name: newName }) ]);
if(!oldRole) {
return;
}
if(!newRole) {
throw new Error(`Db must define the ${newRole} to replace ${oldRole}`);
}
await users.updateMany({ roles: { $in: [oldRole._id] } }, { $set: { roles: [newRole._id] } });
await roles.deleteOne({ name: oldName });
}));
},

async down(db, client) {}
};
46 changes: 46 additions & 0 deletions migrations/20220908105711-convertarialabels.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
const origin = require('../lib/application');
/**
* GENERAL NOTES:
*
* Both functions should return a Promise (and/or use async & await).
*
* You can either access the DB directly via the Node.js MongoDB driver and the db param
* See https://mongodb.github.io/node-mongodb-native/4.4/
*
* You can also use the Origin APIs using the getApp function:
* await this.getApp()
*/
module.exports = {
async up(db, client) {
const collection = db.collection('configs');
const oldDefaults = {
_menu: 1,
_menuGroup: 2,
_menuItem: 2,
_page: 1,
_article: 2,
_block: 3,
_component: 4,
_componentItem: 5,
_notify: 1,
};
const props = origin().db.getModel('config')?.schema?.tree?._accessibility?._ariaLevels?.properties;
if(!props) {
throw new Error(`Couldn't parse ARIA level defaults`);
}
const newDefaults = Object.entries(props).reduce((m, [k, v]) => Object.assign(m, { [k]: v.default }), {});
const configs = await (collection.find().toArray());

await Promise.all(configs.map(async c => {
if(!c?._accessibility?._ariaLevels) {
return;
}
const levels = c._accessibility._ariaLevels;
Object.entries(levels).forEach(([k, v]) => levels[k] = (v === oldDefaults[k]) ? newDefaults[k] : v.toString());
return collection.updateOne({ _id: c._id }, { $set: c });
}));
throw new Error();
},

async down(db, client) {}
};
17 changes: 17 additions & 0 deletions migrations/20220908162037-purgemigrations.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
/**
* GENERAL NOTES:
*
* Both functions should return a Promise (and/or use async & await).
*
* You can either access the DB directly via the Node.js MongoDB driver and the db param
* See https://mongodb.github.io/node-mongodb-native/4.4/
*
* You can also use the Origin APIs using the getApp function:
* await this.getApp()
*/
module.exports = {
async up(db, client) {
return db.dropCollection('migrations');
},
async down(db, client) {}
};
19 changes: 0 additions & 19 deletions migrations/helpers/helper.js

This file was deleted.

25 changes: 0 additions & 25 deletions migrations/helpers/template.js

This file was deleted.

9 changes: 0 additions & 9 deletions migrations/lib/1541676051120-0.6.1.js

This file was deleted.

84 changes: 0 additions & 84 deletions migrations/lib/1554993104498-remove_unused_roles_from_roles.js

This file was deleted.

16 changes: 16 additions & 0 deletions migrations/sample-migration.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
/**
* GENERAL NOTES:
*
* Both functions should return a Promise (and/or use async & await).
*
* You can either access the DB directly via the Node.js MongoDB driver and the db param
* See https://mongodb.github.io/node-mongodb-native/4.4/
*/
module.exports = {
async up(db, client) {
// Code to migrate old data goes here
},
async down(db, client) {
// Code to rollback your migration goes here (if possible)
}
};
Loading