Advertisement
Guest User

migrate

a guest
Jul 11th, 2016
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /**
  2.  * Created by l.heddendorp on 11.07.2016.
  3.  */
  4.  
  5. if (process.argv.length < 3) {
  6.   console.warn('Pass the name of the old database as an argument!');
  7.   process.exit(0);
  8. }
  9. var yaml = require('js-yaml');
  10. var fs = require('fs');
  11. var knex = require('knex');
  12. var oldName = process.argv[ 2 ];
  13. var emptyPerms = {
  14.   manage: false,
  15.   approve: false,
  16.   instances_manage: false,
  17.   instances_view: false,
  18.   users_view: false,
  19.   users_manage: false
  20. }
  21. console.log('Reading database config from parameters.yml ...')
  22. try {
  23.   var params = yaml.safeLoad(fs.readFileSync('../app/config/parameters.yml', 'utf8')).parameters;
  24.   var conf = {};
  25.   conf.host = params.database_host;
  26.   //conf.port = params.database_port;
  27.   conf.user = params.database_user;
  28.   conf.password = params.database_password || '';
  29.   var newName = params.database_name;
  30. } catch (e) {
  31.   console.log('Error while reading database config from parameters.yml');
  32.   console.log(e);
  33.   return;
  34. }
  35. console.log('Database config found:', conf);
  36. console.log('Establishing Database connection');
  37. var oldDB = knex({
  38.   client: 'mysql2',
  39.   connection: Object.assign(conf, { database: oldName })
  40. });
  41. var newDB = knex({
  42.   client: 'mysql2',
  43.   connection: Object.assign(conf, { database: newName })
  44. });
  45. console.log('Connection successful, migrating unchanged tables');
  46. var promises = [];
  47. var assignPromises = [];
  48. promises.push(directMigration('app'));
  49. promises.push(directMigration('app_benefit'));
  50. promises.push(directMigration('app_category'));
  51. promises.push(directMigration('app_cookbook_entry'));
  52. promises.push(directMigration('app_faq'));
  53. promises.push(directMigration('app_feedback_category'));
  54. promises.push(directMigration('app_media'));
  55. promises.push(directMigration('app_promotion'));
  56. promises.push(directMigration('app_requirement'));
  57. promises.push(directMigration('blacklisted_domains', 'blacklisted_domain'));
  58. promises.push(directMigration('logs', 'log'));
  59. promises.push(directMigration('pack'));
  60. promises.push(directMigration('pack_category'));
  61. Promise.all(promises).then(() => {
  62.   console.log('Inital Migrations completed, assignments following');
  63.   assignPromises.push(directMigration('app_category_assignment', 'app_category_assignment', true));
  64.   assignPromises.push(directMigration('pack_category_assignment', 'pack_category_assignment', true));
  65.   oldDB.select().from('mc_event_log').then(rows=> {
  66.     assignPromises.push(newDB.batchInsert('myc_event_log', rows.map(dateFixer).map(log)).then(() => {
  67.       console.log(`Table "${oldName}.myc_event_log" was migrated to "${newName}.myc_event_log"`);
  68.     }));
  69.   });
  70.   oldDB.select().from('mc_update_polling_logs').then(rows=> {
  71.     assignPromises.push(newDB.batchInsert('myc_update_polling_log', rows.map(dateFixer).map(log)).then(() => {
  72.       console.log(`Table "${oldName}.myc_update_polling_logs" was migrated to "${newName}.myc_update_polling_log"`);
  73.     }));
  74.   });
  75.   oldDB.select().from('mc_user').then(rows=> {
  76.     assignPromises.push(newDB.batchInsert('myc_user', rows.map(dateFixer).map(user)).then(() => {
  77.       console.log(`Table "${oldName}.myc_user" was migrated to "${newName}.myc_user"`);
  78.       Promise.all(assignPromises).then(() => {
  79.         console.log('Process finished, terminating');
  80.         process.exit(1);
  81.       });
  82.     }));
  83.   });
  84. });
  85.  
  86. function dateFixer (row) {
  87.   if (!row.created || row.created.toString() == 'Invalid Date')
  88.     row.created = new Date('2001-01-01 00:00:00');
  89.   if (!row.updated || row.updated.toString() == 'Invalid Date')
  90.     row.updated = new Date('2001-01-01 00:00:00');
  91.   return row;
  92. }
  93.  
  94. function log (log) {
  95.   delete log.updated;
  96.   return log;
  97. }
  98.  
  99. function user (user) {
  100.   delete user.appstore_access;
  101.   delete user.appstore_downloads;
  102.   user.accepted_conditions = user.accept_conditions;
  103.   delete user.accept_conditions;
  104.   user.function = user.title;
  105.   delete user.title;
  106.   user = Object.assign(user, emptyPerms);
  107.   return user;
  108. }
  109.  
  110. function directMigration (oldTable, newTable = oldTable, skipDateFix = false) {
  111.   return new Promise(resolve => {
  112.     oldDB.select().from('mc_' + oldTable).then(rows=> {
  113.       if (skipDateFix) {
  114.         newDB('myc_' + newTable).insert(rows).then(() => {
  115.           console.log(`Table "${oldName}.mc_${oldTable}" was migrated to "${newName}.myc_${newTable}"`);
  116.           resolve();
  117.         });
  118.       }
  119.       else {
  120.         newDB.batchInsert('myc_' + newTable, rows.map(dateFixer)).then(() => {
  121.           console.log(`Table "${oldName}.mc_${oldTable}" was migrated to "${newName}.myc_${newTable}"`);
  122.           resolve();
  123.         });
  124.       }
  125.     });
  126.   })
  127. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement