|
| 1 | +#include <vix/db/db.hpp> |
| 2 | + |
| 3 | +#include <iostream> |
| 4 | +#include <filesystem> |
| 5 | +#include <string> |
| 6 | + |
| 7 | +using namespace vix::db; |
| 8 | + |
| 9 | +static DbConfig make_mysql_cfg() |
| 10 | +{ |
| 11 | + DbConfig cfg; |
| 12 | + cfg.engine = Engine::MySQL; |
| 13 | + cfg.mysql.host = "tcp://127.0.0.1:3306"; |
| 14 | + cfg.mysql.user = "root"; |
| 15 | + cfg.mysql.password = ""; |
| 16 | + cfg.mysql.database = "vixdb"; |
| 17 | + cfg.mysql.pool.min = 1; |
| 18 | + cfg.mysql.pool.max = 4; |
| 19 | + return cfg; |
| 20 | +} |
| 21 | + |
| 22 | +// ------------------------------ |
| 23 | +// 1) Code-based migration example |
| 24 | +// ------------------------------ |
| 25 | +class CreateUsersTable final : public Migration |
| 26 | +{ |
| 27 | +public: |
| 28 | + std::string id() const override { return "2026-01-22-create-users"; } |
| 29 | + |
| 30 | + void up(Connection &c) override |
| 31 | + { |
| 32 | + auto st = c.prepare( |
| 33 | + "CREATE TABLE IF NOT EXISTS users (" |
| 34 | + " id BIGINT PRIMARY KEY AUTO_INCREMENT," |
| 35 | + " name VARCHAR(255) NOT NULL," |
| 36 | + " age INT NOT NULL" |
| 37 | + ");"); |
| 38 | + st->exec(); |
| 39 | + } |
| 40 | + |
| 41 | + void down(Connection &c) override |
| 42 | + { |
| 43 | + auto st = c.prepare("DROP TABLE IF EXISTS users;"); |
| 44 | + st->exec(); |
| 45 | + } |
| 46 | +}; |
| 47 | + |
| 48 | +static void run_code_migrations(Database &db) |
| 49 | +{ |
| 50 | + std::cout << "[migrations] running code migrations...\n"; |
| 51 | + |
| 52 | + Transaction tx(db.pool()); |
| 53 | + |
| 54 | + CreateUsersTable m1; |
| 55 | + MigrationsRunner runner(tx.conn()); |
| 56 | + runner.add(&m1); |
| 57 | + runner.runAll(); |
| 58 | + |
| 59 | + tx.commit(); |
| 60 | + std::cout << "[migrations] done (code)\n"; |
| 61 | +} |
| 62 | + |
| 63 | +// ------------------------------ |
| 64 | +// 2) File-based migration example |
| 65 | +// ------------------------------ |
| 66 | +static void run_file_migrations(Database &db, std::filesystem::path dir) |
| 67 | +{ |
| 68 | + std::cout << "[migrations] running file migrations from: " << dir.string() << "\n"; |
| 69 | + |
| 70 | + Transaction tx(db.pool()); |
| 71 | + |
| 72 | + FileMigrationsRunner runner(tx.conn(), std::move(dir)); |
| 73 | + runner.setTable("schema_migrations"); // optional (default already) |
| 74 | + runner.applyAll(); |
| 75 | + |
| 76 | + tx.commit(); |
| 77 | + std::cout << "[migrations] done (files)\n"; |
| 78 | +} |
| 79 | + |
| 80 | +int main() |
| 81 | +{ |
| 82 | + try |
| 83 | + { |
| 84 | + Database db(make_mysql_cfg()); |
| 85 | + |
| 86 | + // 1) Code migrations |
| 87 | + run_code_migrations(db); |
| 88 | + |
| 89 | + // 2) File migrations (expects ./migrations/*.up.sql and *.down.sql) |
| 90 | + run_file_migrations(db, std::filesystem::path{"migrations"}); |
| 91 | + |
| 92 | + std::cout << "OK\n"; |
| 93 | + return 0; |
| 94 | + } |
| 95 | + catch (const std::exception &e) |
| 96 | + { |
| 97 | + std::cerr << "ERROR: " << e.what() << "\n"; |
| 98 | + return 1; |
| 99 | + } |
| 100 | +} |
0 commit comments