1- #include < vix/db/db.hpp>
2-
3- #include < iostream>
41#include < filesystem>
2+ #include < iostream>
53#include < string>
64
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- }
5+ #include < vix/db/db.hpp>
216
22- // ------------------------------
23- // 1) Code-based migration example
24- // ------------------------------
25- class CreateUsersTable final : public Migration
7+ class CreateUsersTable final : public vix::db::Migration
268{
279public:
28- std::string id () const override { return " 2026-01-22-create-users" ; }
10+ std::string id () const override
11+ {
12+ return " 2026-01-22-create-users" ;
13+ }
2914
30- void up (Connection &c) override
15+ void up (vix::db:: Connection &c) override
3116 {
3217 auto st = c.prepare (
3318 " CREATE TABLE IF NOT EXISTS users ("
34- " id BIGINT PRIMARY KEY AUTO_INCREMENT, "
35- " name VARCHAR(255) NOT NULL,"
36- " age INT NOT NULL"
19+ " id INTEGER PRIMARY KEY AUTOINCREMENT, "
20+ " name TEXT NOT NULL, "
21+ " age INTEGER NOT NULL"
3722 " );" );
3823 st->exec ();
3924 }
4025
41- void down (Connection &c) override
26+ void down (vix::db:: Connection &c) override
4227 {
4328 auto st = c.prepare (" DROP TABLE IF EXISTS users;" );
4429 st->exec ();
4530 }
4631};
4732
48- static void run_code_migrations ( Database &db)
33+ static void runCodeMigrations (vix::db:: Database &db)
4934{
5035 std::cout << " [migrations] running code migrations...\n " ;
5136
52- Transaction tx (db.pool ());
37+ vix::db:: Transaction tx (db.pool ());
5338
54- CreateUsersTable m1 ;
55- MigrationsRunner runner (tx.conn ());
56- runner.add (&m1 );
39+ CreateUsersTable migration ;
40+ vix::db:: MigrationsRunner runner (tx.conn ());
41+ runner.add (&migration );
5742 runner.runAll ();
5843
5944 tx.commit ();
6045 std::cout << " [migrations] done (code)\n " ;
6146}
6247
63- // ------------------------------
64- // 2) File-based migration example
65- // ------------------------------
66- static void run_file_migrations (Database &db, std::filesystem::path dir)
48+ static void runFileMigrations (vix::db::Database &db,
49+ std::filesystem::path dir)
6750{
68- std::cout << " [migrations] running file migrations from: " << dir.string () << " \n " ;
51+ std::cout << " [migrations] running file migrations from: "
52+ << dir.string () << " \n " ;
6953
70- Transaction tx (db.pool ());
54+ vix::db:: Transaction tx (db.pool ());
7155
72- FileMigrationsRunner runner (tx.conn (), std::move (dir));
73- runner.setTable (" schema_migrations" ); // optional (default already)
56+ vix::db:: FileMigrationsRunner runner (tx.conn (), std::move (dir));
57+ runner.setTable (" schema_migrations" );
7458 runner.applyAll ();
7559
7660 tx.commit ();
@@ -81,13 +65,10 @@ int main()
8165{
8266 try
8367 {
84- Database db (make_mysql_cfg ());
85-
86- // 1) Code migrations
87- run_code_migrations (db);
68+ auto db = vix::db::Database::sqlite (" vix.db" );
8869
89- // 2) File migrations (expects ./migrations/*.up.sql and *.down.sql)
90- run_file_migrations (db, std::filesystem::path{" migrations" });
70+ runCodeMigrations (db);
71+ runFileMigrations (db, std::filesystem::path{" migrations" });
9172
9273 std::cout << " OK\n " ;
9374 return 0 ;
0 commit comments