|
| 1 | +/** |
| 2 | + * |
| 3 | + * @file MigratorCLI.cpp |
| 4 | + * @author Gaspard Kirira |
| 5 | + * |
| 6 | + * Copyright 2025, Gaspard Kirira. All rights reserved. |
| 7 | + * https://github.com/vixcpp/vix |
| 8 | + * Use of this source code is governed by a MIT license |
| 9 | + * that can be found in the License file. |
| 10 | + * |
| 11 | + * Vix.cpp |
| 12 | + */ |
| 13 | +#include "MigratorCLI.hpp" |
| 14 | + |
| 15 | +#include <vix/db/db.hpp> |
| 16 | +#include <vix/db/FileMigrationsRunner.hpp> |
| 17 | +#include <vix/db/Drivers.hpp> |
| 18 | + |
| 19 | +#include <iostream> |
| 20 | +#include <stdexcept> |
| 21 | +#include <memory> |
| 22 | + |
| 23 | +namespace vix::db::tools |
| 24 | +{ |
| 25 | + void MigratorCLI::printUsage(const char *prog) |
| 26 | + { |
| 27 | + std::cout |
| 28 | + << "Vix ORM Migrator\n\n" |
| 29 | + << "Usage:\n" |
| 30 | + << " " << prog << " <host> <user> <pass> <db> migrate [--dir <migrations_dir>]\n" |
| 31 | + << " " << prog << " <host> <user> <pass> <db> rollback --steps <n> [--dir <migrations_dir>]\n" |
| 32 | + << " " << prog << " <host> <user> <pass> <db> status [--dir <migrations_dir>]\n\n" |
| 33 | + << "Examples:\n" |
| 34 | + << " " << prog << " tcp://127.0.0.1:3306 root '' mydb migrate --dir ./migrations\n" |
| 35 | + << " " << prog << " tcp://127.0.0.1:3306 root '' mydb rollback --steps 1\n" |
| 36 | + << " " << prog << " tcp://127.0.0.1:3306 root '' mydb status --dir db/migrations\n"; |
| 37 | + } |
| 38 | + |
| 39 | + std::string MigratorCLI::getFlagValue( |
| 40 | + const std::vector<std::string> &args, |
| 41 | + const std::string &key) |
| 42 | + { |
| 43 | + for (size_t i = 0; i + 1 < args.size(); ++i) |
| 44 | + { |
| 45 | + if (args[i] == key) |
| 46 | + return args[i + 1]; |
| 47 | + } |
| 48 | + return {}; |
| 49 | + } |
| 50 | + |
| 51 | + bool MigratorCLI::hasFlag( |
| 52 | + const std::vector<std::string> &args, |
| 53 | + const std::string &key) |
| 54 | + { |
| 55 | + for (const auto &a : args) |
| 56 | + { |
| 57 | + if (a == key) |
| 58 | + return true; |
| 59 | + } |
| 60 | + return false; |
| 61 | + } |
| 62 | + |
| 63 | + std::string MigratorCLI::parseDir(const std::vector<std::string> &args) |
| 64 | + { |
| 65 | + std::string v = getFlagValue(args, "--dir"); |
| 66 | + if (!v.empty()) |
| 67 | + return v; |
| 68 | + |
| 69 | + for (const auto &a : args) |
| 70 | + { |
| 71 | + const std::string p = "--dir="; |
| 72 | + if (a.rfind(p, 0) == 0) |
| 73 | + { |
| 74 | + std::string vv = a.substr(p.size()); |
| 75 | + if (!vv.empty()) |
| 76 | + return vv; |
| 77 | + } |
| 78 | + } |
| 79 | + |
| 80 | + return "migrations"; |
| 81 | + } |
| 82 | + |
| 83 | + int MigratorCLI::parseStepsOrThrow(const std::vector<std::string> &args) |
| 84 | + { |
| 85 | + std::string steps_s = getFlagValue(args, "--steps"); |
| 86 | + if (steps_s.empty()) |
| 87 | + throw std::runtime_error("rollback requires --steps <n>"); |
| 88 | + |
| 89 | + int steps = 0; |
| 90 | + try |
| 91 | + { |
| 92 | + steps = std::stoi(steps_s); |
| 93 | + } |
| 94 | + catch (...) |
| 95 | + { |
| 96 | + throw std::runtime_error("invalid --steps value (must be an integer)"); |
| 97 | + } |
| 98 | + |
| 99 | + if (steps <= 0) |
| 100 | + throw std::runtime_error("--steps must be >= 1"); |
| 101 | + |
| 102 | + return steps; |
| 103 | + } |
| 104 | + |
| 105 | + void MigratorCLI::validateOrThrow(const Options &opt) |
| 106 | + { |
| 107 | + if (opt.help) |
| 108 | + return; |
| 109 | + |
| 110 | + if (opt.host.empty() || opt.user.empty() || opt.db.empty()) |
| 111 | + throw std::runtime_error("missing required args: <host> <user> <pass> <db> <command>"); |
| 112 | + |
| 113 | + if (opt.command != "migrate" && opt.command != "rollback" && opt.command != "status") |
| 114 | + throw std::runtime_error("unknown command: " + opt.command); |
| 115 | + |
| 116 | + if (opt.command == "rollback" && opt.steps <= 0) |
| 117 | + throw std::runtime_error("rollback requires --steps <n>"); |
| 118 | + |
| 119 | + if (opt.migrationsDir.empty()) |
| 120 | + throw std::runtime_error("migrations dir is empty (use --dir <path>)"); |
| 121 | + } |
| 122 | + |
| 123 | + MigratorCLI::Options MigratorCLI::parseArgsOrThrow(int argc, char **argv) |
| 124 | + { |
| 125 | + Options opt; |
| 126 | + |
| 127 | + std::vector<std::string> all; |
| 128 | + all.reserve(static_cast<size_t>(argc)); |
| 129 | + for (int i = 0; i < argc; ++i) |
| 130 | + all.emplace_back(argv[i]); |
| 131 | + |
| 132 | + if (argc <= 1 || hasFlag(all, "-h") || hasFlag(all, "--help")) |
| 133 | + { |
| 134 | + opt.help = true; |
| 135 | + return opt; |
| 136 | + } |
| 137 | + |
| 138 | + if (argc < 6) |
| 139 | + throw std::runtime_error("not enough arguments"); |
| 140 | + |
| 141 | + opt.host = argv[1]; |
| 142 | + opt.user = argv[2]; |
| 143 | + opt.pass = argv[3]; |
| 144 | + opt.db = argv[4]; |
| 145 | + opt.command = argv[5]; |
| 146 | + |
| 147 | + std::vector<std::string> extra; |
| 148 | + for (int i = 6; i < argc; ++i) |
| 149 | + extra.emplace_back(argv[i]); |
| 150 | + |
| 151 | + opt.migrationsDir = parseDir(extra); |
| 152 | + |
| 153 | + if (opt.command == "rollback") |
| 154 | + opt.steps = parseStepsOrThrow(extra); |
| 155 | + |
| 156 | + validateOrThrow(opt); |
| 157 | + return opt; |
| 158 | + } |
| 159 | + |
| 160 | + int MigratorCLI::run(int argc, char **argv) |
| 161 | + { |
| 162 | + try |
| 163 | + { |
| 164 | + Options opt = parseArgsOrThrow(argc, argv); |
| 165 | + |
| 166 | + if (opt.help) |
| 167 | + { |
| 168 | + printUsage(argv[0]); |
| 169 | + return 0; |
| 170 | + } |
| 171 | + |
| 172 | + vix::db::ConnectionPtr conn; |
| 173 | + std::unique_ptr<vix::db::FileMigrationsRunner> runner; |
| 174 | + |
| 175 | +#if VIX_DB_HAS_MYSQL |
| 176 | + { |
| 177 | + auto factory = vix::db::make_mysql_factory(opt.host, opt.user, opt.pass, opt.db); |
| 178 | + conn = factory(); |
| 179 | + runner = std::make_unique<vix::db::FileMigrationsRunner>(*conn, opt.migrationsDir); |
| 180 | + } |
| 181 | +#elif VIX_DB_HAS_SQLITE |
| 182 | + { |
| 183 | + auto factory = vix::db::make_sqlite_factory(opt.db); |
| 184 | + conn = factory(); |
| 185 | + runner = std::make_unique<vix::db::FileMigrationsRunner>(*conn, opt.migrationsDir); |
| 186 | + } |
| 187 | +#else |
| 188 | + std::cerr << "[ERR] vix_db_migrator built without DB drivers.\n" |
| 189 | + << "Enable one with:\n" |
| 190 | + << " -DVIX_DB_HAS_MYSQL=1 (or add SQLite support)\n"; |
| 191 | + return 1; |
| 192 | +#endif |
| 193 | + |
| 194 | + if (opt.command == "migrate") |
| 195 | + { |
| 196 | + runner->applyAll(); |
| 197 | + std::cout << "[OK] migrations applied\n"; |
| 198 | + return 0; |
| 199 | + } |
| 200 | + |
| 201 | + if (opt.command == "rollback") |
| 202 | + { |
| 203 | + runner->rollback(opt.steps); |
| 204 | + std::cout << "[OK] rollback " << opt.steps << " step(s)\n"; |
| 205 | + return 0; |
| 206 | + } |
| 207 | + |
| 208 | + if (opt.command == "status") |
| 209 | + { |
| 210 | + std::cout << "[OK] migrations dir: " << opt.migrationsDir << "\n"; |
| 211 | + std::cout << "Tip: implement FileMigrationsRunner::status() to show applied vs pending.\n"; |
| 212 | + return 0; |
| 213 | + } |
| 214 | + |
| 215 | + printUsage(argv[0]); |
| 216 | + return 1; |
| 217 | + } |
| 218 | + catch (const std::exception &e) |
| 219 | + { |
| 220 | + std::cerr << "[ERR] " << e.what() << "\n"; |
| 221 | + std::cerr << "Tip: run with --help\n"; |
| 222 | + return 1; |
| 223 | + } |
| 224 | + } |
| 225 | + |
| 226 | +} // namespace vix::db::tools |
0 commit comments