Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * Created by l.heddendorp on 11.07.2016.
- */
- if (process.argv.length < 3) {
- console.warn('Pass the name of the old database as an argument!');
- process.exit(0);
- }
- var yaml = require('js-yaml');
- var fs = require('fs');
- var knex = require('knex');
- var oldName = process.argv[ 2 ];
- var emptyPerms = {
- manage: false,
- approve: false,
- instances_manage: false,
- instances_view: false,
- users_view: false,
- users_manage: false
- }
- console.log('Reading database config from parameters.yml ...')
- try {
- var params = yaml.safeLoad(fs.readFileSync('../app/config/parameters.yml', 'utf8')).parameters;
- var conf = {};
- conf.host = params.database_host;
- //conf.port = params.database_port;
- conf.user = params.database_user;
- conf.password = params.database_password || '';
- var newName = params.database_name;
- } catch (e) {
- console.log('Error while reading database config from parameters.yml');
- console.log(e);
- return;
- }
- console.log('Database config found:', conf);
- console.log('Establishing Database connection');
- var oldDB = knex({
- client: 'mysql2',
- connection: Object.assign(conf, { database: oldName })
- });
- var newDB = knex({
- client: 'mysql2',
- connection: Object.assign(conf, { database: newName })
- });
- console.log('Connection successful, migrating unchanged tables');
- var promises = [];
- var assignPromises = [];
- promises.push(directMigration('app'));
- promises.push(directMigration('app_benefit'));
- promises.push(directMigration('app_category'));
- promises.push(directMigration('app_cookbook_entry'));
- promises.push(directMigration('app_faq'));
- promises.push(directMigration('app_feedback_category'));
- promises.push(directMigration('app_media'));
- promises.push(directMigration('app_promotion'));
- promises.push(directMigration('app_requirement'));
- promises.push(directMigration('blacklisted_domains', 'blacklisted_domain'));
- promises.push(directMigration('logs', 'log'));
- promises.push(directMigration('pack'));
- promises.push(directMigration('pack_category'));
- Promise.all(promises).then(() => {
- console.log('Inital Migrations completed, assignments following');
- assignPromises.push(directMigration('app_category_assignment', 'app_category_assignment', true));
- assignPromises.push(directMigration('pack_category_assignment', 'pack_category_assignment', true));
- oldDB.select().from('mc_event_log').then(rows=> {
- assignPromises.push(newDB.batchInsert('myc_event_log', rows.map(dateFixer).map(log)).then(() => {
- console.log(`Table "${oldName}.myc_event_log" was migrated to "${newName}.myc_event_log"`);
- }));
- });
- oldDB.select().from('mc_update_polling_logs').then(rows=> {
- assignPromises.push(newDB.batchInsert('myc_update_polling_log', rows.map(dateFixer).map(log)).then(() => {
- console.log(`Table "${oldName}.myc_update_polling_logs" was migrated to "${newName}.myc_update_polling_log"`);
- }));
- });
- oldDB.select().from('mc_user').then(rows=> {
- assignPromises.push(newDB.batchInsert('myc_user', rows.map(dateFixer).map(user)).then(() => {
- console.log(`Table "${oldName}.myc_user" was migrated to "${newName}.myc_user"`);
- Promise.all(assignPromises).then(() => {
- console.log('Process finished, terminating');
- process.exit(1);
- });
- }));
- });
- });
- function dateFixer (row) {
- if (!row.created || row.created.toString() == 'Invalid Date')
- row.created = new Date('2001-01-01 00:00:00');
- if (!row.updated || row.updated.toString() == 'Invalid Date')
- row.updated = new Date('2001-01-01 00:00:00');
- return row;
- }
- function log (log) {
- delete log.updated;
- return log;
- }
- function user (user) {
- delete user.appstore_access;
- delete user.appstore_downloads;
- user.accepted_conditions = user.accept_conditions;
- delete user.accept_conditions;
- user.function = user.title;
- delete user.title;
- user = Object.assign(user, emptyPerms);
- return user;
- }
- function directMigration (oldTable, newTable = oldTable, skipDateFix = false) {
- return new Promise(resolve => {
- oldDB.select().from('mc_' + oldTable).then(rows=> {
- if (skipDateFix) {
- newDB('myc_' + newTable).insert(rows).then(() => {
- console.log(`Table "${oldName}.mc_${oldTable}" was migrated to "${newName}.myc_${newTable}"`);
- resolve();
- });
- }
- else {
- newDB.batchInsert('myc_' + newTable, rows.map(dateFixer)).then(() => {
- console.log(`Table "${oldName}.mc_${oldTable}" was migrated to "${newName}.myc_${newTable}"`);
- resolve();
- });
- }
- });
- })
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement