diff options
| author | Rutger Broekhoff | 2026-11-30 21:27:55 +0100 |
|---|---|---|
| committer | Rutger Broekhoff | 2026-11-30 21:27:55 +0100 |
| commit | 7bba7176194c4d1d9c7066f8eda7a94491d924e6 (patch) | |
| tree | a9a171e8608786e09d6342729ee13b8471c0c9c2 /server/src/database/migrator.cpp | |
| parent | 02475e05ccd1aa15c6a6163834f28bfebbef796f (diff) | |
| download | routemon-7bba7176194c4d1d9c7066f8eda7a94491d924e6.tar.gz routemon-7bba7176194c4d1d9c7066f8eda7a94491d924e6.zip | |
Cleanups and updates
Diffstat (limited to 'server/src/database/migrator.cpp')
| -rw-r--r-- | server/src/database/migrator.cpp | 122 |
1 files changed, 122 insertions, 0 deletions
diff --git a/server/src/database/migrator.cpp b/server/src/database/migrator.cpp new file mode 100644 index 0000000..d6c7926 --- /dev/null +++ b/server/src/database/migrator.cpp | |||
| @@ -0,0 +1,122 @@ | |||
| 1 | module; | ||
| 2 | |||
| 3 | #include <cassert> | ||
| 4 | |||
| 5 | export module routemon:database.migrator$impl; | ||
| 6 | |||
| 7 | import :database.migrator; | ||
| 8 | |||
| 9 | namespace routemon::database { | ||
| 10 | |||
| 11 | auto load_migrations( | ||
| 12 | std::filesystem::path const& dir, std::int64_t current_version, | ||
| 13 | std::int64_t target_version) -> std::vector<std::string> | ||
| 14 | { | ||
| 15 | if (current_version < 0 || target_version < 0) | ||
| 16 | throw std::invalid_argument{"database versions must be nonnegative"}; | ||
| 17 | if (target_version < current_version) | ||
| 18 | throw std::runtime_error{"migrating down is not supported yet"}; | ||
| 19 | if (current_version == target_version) | ||
| 20 | return {}; | ||
| 21 | assert(current_version < target_version); | ||
| 22 | |||
| 23 | // TODO: narrow_cast or something like that | ||
| 24 | auto migration_sqls = | ||
| 25 | std::vector<std::optional<std::pair<std::filesystem::path, std::string>>>{ | ||
| 26 | static_cast<std::size_t>(target_version - current_version) | ||
| 27 | }; | ||
| 28 | for (auto const& entry : std::filesystem::directory_iterator{dir}) | ||
| 29 | { | ||
| 30 | if (!std::filesystem::is_regular_file(entry.status())) | ||
| 31 | continue; | ||
| 32 | auto const filename = std::string{entry.path().filename()}; | ||
| 33 | if (!filename.ends_with("_up.sql")) | ||
| 34 | continue; | ||
| 35 | auto const [no_str, mrest] = util::split_on(filename, '_'); | ||
| 36 | if (!mrest) | ||
| 37 | continue; | ||
| 38 | auto const mno = util::parse_int<std::int64_t>(no_str); | ||
| 39 | if (!mno || *mno < 1) | ||
| 40 | continue; | ||
| 41 | if (*mno <= current_version || target_version < *mno) | ||
| 42 | continue; | ||
| 43 | auto const i = static_cast<std::size_t>(*mno - current_version - 1); | ||
| 44 | |||
| 45 | if (migration_sqls[i]) | ||
| 46 | throw std::runtime_error{std::format( | ||
| 47 | "found more than one (up) migration file for version {}", *mno)}; | ||
| 48 | |||
| 49 | auto f = std::ifstream{entry.path(), std::ios::binary}; | ||
| 50 | auto contents = std::string{ | ||
| 51 | std::istreambuf_iterator<char>{f}, | ||
| 52 | std::istreambuf_iterator<char>{}, | ||
| 53 | }; | ||
| 54 | |||
| 55 | migration_sqls[i] = {entry.path(), contents}; | ||
| 56 | } | ||
| 57 | |||
| 58 | return migration_sqls | std::views::enumerate | ||
| 59 | | std::views::transform( | ||
| 60 | [current_version](auto&& pair) -> std::string | ||
| 61 | { | ||
| 62 | auto&& [i, mentry] = pair; | ||
| 63 | if (!mentry) | ||
| 64 | throw std::runtime_error{std::format( | ||
| 65 | "no migration found to version {}", | ||
| 66 | current_version + i + 1)}; | ||
| 67 | return std::move(mentry->second); | ||
| 68 | }) | ||
| 69 | | std::ranges::to<std::vector<std::string>>(); | ||
| 70 | } | ||
| 71 | |||
| 72 | auto migrator::current_version() -> std::int64_t | ||
| 73 | { | ||
| 74 | if (auto const [has_migrations_table] = | ||
| 75 | dbc_.query( | ||
| 76 | "SELECT COUNT(*) FROM sqlite_schema WHERE " | ||
| 77 | "type IS 'table' AND " | ||
| 78 | "name IS 'migration'") | ||
| 79 | .scan_row_max1<std::int64_t>(); | ||
| 80 | has_migrations_table) | ||
| 81 | return std::get<0>(dbc_.query("SELECT version FROM migration") | ||
| 82 | .scan_row_max1<std::int64_t>()); | ||
| 83 | return 0; | ||
| 84 | } | ||
| 85 | |||
| 86 | migrator::migrator( | ||
| 87 | log::logger const& l, std::filesystem::path const& db_filename, | ||
| 88 | std::filesystem::path migrations_dir) | ||
| 89 | : l_{l.sub("migrator")}, dbc_{sqlite3::open(db_filename)}, | ||
| 90 | migrations_dir_{std::move(migrations_dir)} | ||
| 91 | { | ||
| 92 | } | ||
| 93 | |||
| 94 | auto migrator::migrate_to(std::int64_t target_version) -> void | ||
| 95 | { | ||
| 96 | // Part of function contract. Consider throwing instead? | ||
| 97 | if (target_version < 0) | ||
| 98 | throw std::invalid_argument{"target database version must be nonnegative"}; | ||
| 99 | |||
| 100 | auto current = current_version(); | ||
| 101 | auto const migration_sqls = | ||
| 102 | load_migrations(migrations_dir_, current, target_version); | ||
| 103 | |||
| 104 | for (auto const& sql : migration_sqls) | ||
| 105 | { | ||
| 106 | l_.info("Migrating from schema version {} to {}", current, current + 1); | ||
| 107 | migrate_step(++current, sql); | ||
| 108 | } | ||
| 109 | } | ||
| 110 | |||
| 111 | auto migrator::migrate_step(std::int64_t target_version, util::zstring_view sql) | ||
| 112 | -> void | ||
| 113 | { | ||
| 114 | auto tx = sqlite3::tx{dbc_}; | ||
| 115 | while (!sql.empty()) | ||
| 116 | tx.exec_first(sql); | ||
| 117 | tx.exec( | ||
| 118 | "UPDATE migration SET version = :version", | ||
| 119 | [target_version](auto& s) { s.bind(":version", target_version); }); | ||
| 120 | } | ||
| 121 | |||
| 122 | } // namespace routemon::database | ||