diff options
Diffstat (limited to 'server/src/database.cpp')
| -rw-r--r-- | server/src/database.cpp | 34 |
1 files changed, 34 insertions, 0 deletions
diff --git a/server/src/database.cpp b/server/src/database.cpp new file mode 100644 index 0000000..54e5f34 --- /dev/null +++ b/server/src/database.cpp | |||
| @@ -0,0 +1,34 @@ | |||
| 1 | module routemon:database$impl; | ||
| 2 | |||
| 3 | import :database; | ||
| 4 | |||
| 5 | namespace routemon::database { | ||
| 6 | |||
| 7 | constexpr std::int64_t expected_database_version = 1; | ||
| 8 | |||
| 9 | auto open(std::string const& filename) -> std::shared_ptr<connection> | ||
| 10 | { | ||
| 11 | auto dbc = sqlite3::open(filename); | ||
| 12 | try | ||
| 13 | { | ||
| 14 | auto version = std::optional<std::int64_t>{}; | ||
| 15 | dbc.query("SELECT version FROM migration;").scan_single(version); | ||
| 16 | if (!version) | ||
| 17 | throw std::runtime_error{"failed to fetch database migration version"}; | ||
| 18 | if (version != expected_database_version) | ||
| 19 | { | ||
| 20 | throw std::runtime_error{std::format( | ||
| 21 | "database migration version ({}) does not match expected " | ||
| 22 | "version ({}), consider running migrations", | ||
| 23 | *version, expected_database_version)}; | ||
| 24 | } | ||
| 25 | } | ||
| 26 | catch (std::exception const& e) | ||
| 27 | { | ||
| 28 | throw std::runtime_error{std::format( | ||
| 29 | "failed to query database version: {}", e.what())}; | ||
| 30 | } | ||
| 31 | return std::shared_ptr<connection>{new connection{std::move(dbc)}}; | ||
| 32 | } | ||
| 33 | |||
| 34 | } // namespace routemon::database | ||