|
| 1 | +/** |
| 2 | + * The MIT License (MIT) |
| 3 | + * Copyright (c) 2017 Forfuture, LLC <[email protected]> |
| 4 | + */ |
| 5 | +/* eslint-disable no-console */ |
| 6 | + |
| 7 | + |
| 8 | +// built-in modules |
| 9 | +const async = require("asyncawait/async"); |
| 10 | +const await = require("asyncawait/await"); |
| 11 | +const path = require("path"); |
| 12 | + |
| 13 | + |
| 14 | +// installed modules |
| 15 | +const Debug = require("debug"); |
| 16 | +const program = require("commander"); |
| 17 | + |
| 18 | + |
| 19 | +// own modules |
| 20 | +const migrations = require("./migrations"); |
| 21 | +const pkg = require("../package"); |
| 22 | +const utils = require("./utils"); |
| 23 | + |
| 24 | + |
| 25 | +// module variables |
| 26 | +const debug = Debug("migrate:cli"); |
| 27 | + |
| 28 | + |
| 29 | +program |
| 30 | + .version(pkg.version) |
| 31 | + .usage("[options] [version]") |
| 32 | + .option("-i, --init-path <path>", "path to initialization file", "script/migrate") |
| 33 | + .option("-m, --migrations-path <path>", "path to migrations directory", "script/migrations") |
| 34 | + .option("-c, --current <version>", "use as current version") |
| 35 | + .option("-u, --undo", "undo last migration") |
| 36 | + .option("-l, --list", "list available data versions") |
| 37 | + .option("-w, --which", "show current data version") |
| 38 | + .option("-t, --history", "show migration history") |
| 39 | + ; // eslint-disable-line indent |
| 40 | + |
| 41 | + |
| 42 | +const run = async (function run() { |
| 43 | + let projectHandle; |
| 44 | + let output; |
| 45 | + let error; |
| 46 | + |
| 47 | + try { await (async (function() { |
| 48 | + program.parse(process.argv); |
| 49 | + |
| 50 | + const paths = {}; |
| 51 | + ["initPath", "migrationsPath"].forEach(function(key) { |
| 52 | + paths[key] = path.resolve(program[key]); |
| 53 | + }); |
| 54 | + |
| 55 | + const versions = await (utils.getVersions(paths.migrationsPath)); |
| 56 | + if (program.list) { |
| 57 | + console.log(versions); |
| 58 | + return; |
| 59 | + } |
| 60 | + |
| 61 | + projectHandle = await (utils.openProject(paths.initPath)); |
| 62 | + |
| 63 | + const { dbVersions, context } = projectHandle; |
| 64 | + const currentVersion = (dbVersions.current && dbVersions.current.version) || program.current; |
| 65 | + if (!currentVersion) { |
| 66 | + console.error("error: current version not found"); |
| 67 | + return; |
| 68 | + } |
| 69 | + if (program.which) { |
| 70 | + console.log(currentVersion); |
| 71 | + return; |
| 72 | + } |
| 73 | + debug("current version: %s", currentVersion); |
| 74 | + if (program.history) { |
| 75 | + console.log(dbVersions); |
| 76 | + return; |
| 77 | + } |
| 78 | + debug("versions in db: %j", dbVersions); |
| 79 | + |
| 80 | + let targetVersion = program.args.shift(); |
| 81 | + if (targetVersion && versions.indexOf(targetVersion) === -1) { |
| 82 | + throw "target version not found"; |
| 83 | + } |
| 84 | + if (!targetVersion && program.undo) { |
| 85 | + if (!dbVersions.previous) { |
| 86 | + return; |
| 87 | + } |
| 88 | + targetVersion = dbVersions.previous.version; |
| 89 | + } |
| 90 | + targetVersion = targetVersion || migrations.getLatestVersion(versions); |
| 91 | + if (!targetVersion) { |
| 92 | + throw "target version not found"; |
| 93 | + } |
| 94 | + debug("target version: %s", targetVersion); |
| 95 | + |
| 96 | + if (currentVersion === targetVersion) { |
| 97 | + debug("currentVersion(%s) -eq targetVersion(%s)", currentVersion, targetVersion); |
| 98 | + return; |
| 99 | + } |
| 100 | + |
| 101 | + const targetMigrations = migrations.findVersions(versions, currentVersion, targetVersion).map(function(version) { |
| 102 | + return { |
| 103 | + version, |
| 104 | + module: require(path.join(paths.migrationsPath, version)), |
| 105 | + }; |
| 106 | + }); |
| 107 | + |
| 108 | + if (!targetMigrations.length) { |
| 109 | + debug("no migrations to run"); |
| 110 | + return; |
| 111 | + } |
| 112 | + |
| 113 | + const up = migrations.goingUp(currentVersion, targetVersion); |
| 114 | + await (migrations.migrate(up, targetMigrations, context)); |
| 115 | + |
| 116 | + output = { |
| 117 | + currentVersion: targetVersion, |
| 118 | + previousVersion: currentVersion, |
| 119 | + }; |
| 120 | + debug("migration output: %j", output); |
| 121 | + })()); } catch (ex) { |
| 122 | + error = ex; |
| 123 | + } |
| 124 | + |
| 125 | + // Clean up. |
| 126 | + if (projectHandle) { |
| 127 | + await (utils.closeProject(projectHandle, error, output)); |
| 128 | + } |
| 129 | + |
| 130 | + // Error handling. |
| 131 | + // TODO: |
| 132 | + // Improve error logging! |
| 133 | + if (error) { |
| 134 | + console.error("error:", error.message || error); |
| 135 | + if (error.stack) { |
| 136 | + const padding = " "; |
| 137 | + console.error(padding + error.stack.split("\n").slice(1).map((l) => l.trim()).join(`\n${padding}`)); |
| 138 | + } |
| 139 | + throw error; |
| 140 | + } |
| 141 | + |
| 142 | + return output; |
| 143 | +}); |
| 144 | + |
| 145 | + |
| 146 | +const runAndExit = async (function runAndExit() { |
| 147 | + try { |
| 148 | + await (run()); |
| 149 | + } catch (ex) { |
| 150 | + process.exit(1); |
| 151 | + } |
| 152 | + process.exit(); |
| 153 | +}); |
| 154 | + |
| 155 | + |
| 156 | +if (require.main === module) { |
| 157 | + runAndExit(); |
| 158 | +} |
| 159 | + |
| 160 | + |
| 161 | +exports = module.exports = { |
| 162 | + /** |
| 163 | + * Run the CLI. |
| 164 | + * @kind function |
| 165 | + * @return {Promise} |
| 166 | + */ |
| 167 | + run, |
| 168 | + /** |
| 169 | + * Run the CLI and exit immediately. |
| 170 | + * *Automatically invoked if this file is run as script.* |
| 171 | + * @kind function |
| 172 | + */ |
| 173 | + runAndExit, |
| 174 | +}; |
0 commit comments