diff options
| author | Rutger Broekhoff | 2026-08-28 18:03:05 +0200 |
|---|---|---|
| committer | Rutger Broekhoff | 2026-08-28 18:03:05 +0200 |
| commit | 973aec43ea54bbf95b64fbcb636403401d1ca60e (patch) | |
| tree | 41b7911c420766a9b463245b9296f44c5bf35258 /server/src/database.cppm | |
| download | routemon-973aec43ea54bbf95b64fbcb636403401d1ca60e.tar.gz routemon-973aec43ea54bbf95b64fbcb636403401d1ca60e.zip | |
Import from e4b104792206ee7ea64bf39c6b7d2c0c230f9d14
Diffstat (limited to 'server/src/database.cppm')
| -rw-r--r-- | server/src/database.cppm | 37 |
1 files changed, 37 insertions, 0 deletions
diff --git a/server/src/database.cppm b/server/src/database.cppm new file mode 100644 index 0000000..0312dda --- /dev/null +++ b/server/src/database.cppm | |||
| @@ -0,0 +1,37 @@ | |||
| 1 | export module routemon:database; | ||
| 2 | |||
| 3 | import std; | ||
| 4 | import :sqlite3; | ||
| 5 | |||
| 6 | namespace routemon::database { | ||
| 7 | |||
| 8 | static constexpr std::int64_t expected_database_version = 1; | ||
| 9 | |||
| 10 | export class connection { | ||
| 11 | sqlite3::connection dbc_; | ||
| 12 | |||
| 13 | explicit connection(sqlite3::connection dbc) : dbc_{std::move(dbc)} {} | ||
| 14 | |||
| 15 | friend auto open(std::string const& filename) -> std::shared_ptr<connection>; | ||
| 16 | |||
| 17 | public: | ||
| 18 | // Nothing here yet | ||
| 19 | }; | ||
| 20 | |||
| 21 | export auto open(std::string const& filename) -> std::shared_ptr<connection> { | ||
| 22 | auto dbc = sqlite3::open(filename); | ||
| 23 | try { | ||
| 24 | auto version = std::optional<std::int64_t>{}; | ||
| 25 | dbc.query("SELECT version FROM migration;").scan_single(version); | ||
| 26 | if (!version) | ||
| 27 | throw std::runtime_error{"failed to fetch database migration version"}; | ||
| 28 | if (version != expected_database_version) { | ||
| 29 | throw std::runtime_error{std::format("database migration version ({}) does not match expected version ({}), consider running migrations", *version, expected_database_version)}; | ||
| 30 | } | ||
| 31 | } catch (std::exception const& e) { | ||
| 32 | throw std::runtime_error{std::format("failed to query database version: {}", e.what())}; | ||
| 33 | } | ||
| 34 | return std::shared_ptr<connection>{new connection{std::move(dbc)}}; | ||
| 35 | } | ||
| 36 | |||
| 37 | } // namespace routemon::database | ||