diff options
| author | Rutger Broekhoff | 2026-08-29 12:01:42 +0200 |
|---|---|---|
| committer | Rutger Broekhoff | 2026-08-29 12:01:42 +0200 |
| commit | 52b3cd46c76fd4be18aeb8422a08a073484b9fad (patch) | |
| tree | b4f23957c096e6bce5369bb94a4e8e23de71761a /server/src/database.cpp | |
| parent | 35878b76049b9c3e752480e9f298b7e2da6fdf1f (diff) | |
| download | routemon-52b3cd46c76fd4be18aeb8422a08a073484b9fad.tar.gz routemon-52b3cd46c76fd4be18aeb8422a08a073484b9fad.zip | |
More module implementation partition units
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 | ||