migrate.js 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. "use strict";
  2. var _process = _interopRequireDefault(require("process"));
  3. var _yargs = require("../core/yargs");
  4. var _migrator = require("../core/migrator");
  5. var _helpers = _interopRequireDefault(require("../helpers"));
  6. var _lodash = _interopRequireDefault(require("lodash"));
  7. function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
  8. exports.builder = yargs => (0, _yargs._baseOptions)(yargs).option('to', {
  9. describe: 'Migration name to run migrations until',
  10. type: 'string'
  11. }).option('from', {
  12. describe: 'Migration name to start migrations from (excluding)',
  13. type: 'string'
  14. }).option('name', {
  15. describe: 'Migration name. When specified, only this migration will be run. Mutually exclusive with --to and --from',
  16. type: 'string',
  17. conflicts: ['to', 'from']
  18. }).argv;
  19. exports.handler = async function (args) {
  20. const command = args._[0];
  21. // legacy, gulp used to do this
  22. await _helpers.default.config.init();
  23. switch (command) {
  24. case 'db:migrate':
  25. await migrate(args);
  26. break;
  27. case 'db:migrate:schema:timestamps:add':
  28. await migrateSchemaTimestampAdd(args);
  29. break;
  30. case 'db:migrate:status':
  31. await migrationStatus(args);
  32. break;
  33. }
  34. _process.default.exit(0);
  35. };
  36. function migrate(args) {
  37. return (0, _migrator.getMigrator)('migration', args).then(migrator => {
  38. return (0, _migrator.ensureCurrentMetaSchema)(migrator).then(() => migrator.pending()).then(migrations => {
  39. const options = {};
  40. if (migrations.length === 0) {
  41. _helpers.default.view.log('No migrations were executed, database schema was already up to date.');
  42. _process.default.exit(0);
  43. }
  44. if (args.to) {
  45. if (migrations.filter(migration => migration.file === args.to).length === 0) {
  46. _helpers.default.view.log('No migrations were executed, database schema was already up to date.');
  47. _process.default.exit(0);
  48. }
  49. options.to = args.to;
  50. }
  51. if (args.from) {
  52. if (migrations.map(migration => migration.file).lastIndexOf(args.from) === -1) {
  53. _helpers.default.view.log('No migrations were executed, database schema was already up to date.');
  54. _process.default.exit(0);
  55. }
  56. options.from = args.from;
  57. }
  58. return options;
  59. }).then(options => {
  60. if (args.name) {
  61. return migrator.up(args.name);
  62. } else {
  63. return migrator.up(options);
  64. }
  65. });
  66. }).catch(e => _helpers.default.view.error(e));
  67. }
  68. function migrationStatus(args) {
  69. return (0, _migrator.getMigrator)('migration', args).then(migrator => {
  70. return (0, _migrator.ensureCurrentMetaSchema)(migrator).then(() => migrator.executed()).then(migrations => {
  71. _lodash.default.forEach(migrations, migration => {
  72. _helpers.default.view.log('up', migration.file);
  73. });
  74. }).then(() => migrator.pending()).then(migrations => {
  75. _lodash.default.forEach(migrations, migration => {
  76. _helpers.default.view.log('down', migration.file);
  77. });
  78. });
  79. }).catch(e => _helpers.default.view.error(e));
  80. }
  81. function migrateSchemaTimestampAdd(args) {
  82. return (0, _migrator.getMigrator)('migration', args).then(migrator => {
  83. return (0, _migrator.addTimestampsToSchema)(migrator).then(items => {
  84. if (items) {
  85. _helpers.default.view.log('Successfully added timestamps to MetaTable.');
  86. } else {
  87. _helpers.default.view.log('MetaTable already has timestamps.');
  88. }
  89. });
  90. }).catch(e => _helpers.default.view.error(e));
  91. }