From 7bba7176194c4d1d9c7066f8eda7a94491d924e6 Mon Sep 17 00:00:00 2001 From: Rutger Broekhoff Date: Mon, 30 Nov 2026 21:27:55 +0100 Subject: Cleanups and updates --- server/src/database.cpp | 136 ------------------------------------------------ 1 file changed, 136 deletions(-) delete mode 100644 server/src/database.cpp (limited to 'server/src/database.cpp') diff --git a/server/src/database.cpp b/server/src/database.cpp deleted file mode 100644 index c1f9ab2..0000000 --- a/server/src/database.cpp +++ /dev/null @@ -1,136 +0,0 @@ -module routemon:database$impl; - -import :database; - -namespace routemon::database { - -namespace schema { - -constexpr std::int64_t expected_version = 2; - -auto try_to_string(rwgps_notification_cause c) -> std::optional -{ - switch (c) - { - case rwgps_notification_cause::unknown: - return std::nullopt; - case rwgps_notification_cause::created: - return "created"; - case rwgps_notification_cause::updated: - return "updated"; - case rwgps_notification_cause::deleted: - return "deleted"; - case rwgps_notification_cause::added: - return "added"; - case rwgps_notification_cause::removed: - return "removed"; - } -} - -auto try_to_string(rwgps_notification_item_type t) -> std::optional -{ - switch (t) - { - case rwgps_notification_item_type::unknown: - return std::nullopt; - case rwgps_notification_item_type::trip: - return "trip"; - case rwgps_notification_item_type::route: - return "route"; - case rwgps_notification_item_type::event: - return "event"; - case rwgps_notification_item_type::event_participant: - return "event_participant"; - case rwgps_notification_item_type::club_member: - return "club_member"; - case rwgps_notification_item_type::authorization: - return "authorization"; - } -} - -auto try_to_string(rwgps_notification_target_collection_type t) - -> std::optional -{ - switch (t) - { - case rwgps_notification_target_collection_type::unknown: - return std::nullopt; - case rwgps_notification_target_collection_type::pinned: - return "pinned"; - } -}; - -} // namespace schema - -auto connection::insert_rwgps_notifications( - std::vector - >>& notifs) -> void -{ - auto tx = sqlite3::tx{dbc_}; - for (auto& [notif, mtarget_collection] : notifs) - { - tx.query( - "INSERT INTO rwgps_notification " - " (rwgps_timestamp, user_id, item_type, item_id, " - " item_user_id, item_url, action_) " - "VALUES (:rwgps_timestamp, :user_id, :item_type, :item_id, " - " :item_user_id, :item_url, :action) " - "RETURNING id", - [¬if](auto& s) - { - auto const ts = time::iso8601_secs(notif.rwgps_timestamp); - s.bind(":rwgps_timestamp", std::string_view{ts}); - s.bind(":user_id", notif.user_id); - s.bind(":item_type", try_to_string(notif.item_type)); - s.bind(":item_id", notif.item_id); - s.bind(":item_user_id", notif.item_user_id); - s.bind(":item_url", notif.item_url); - s.bind(":action", try_to_string(notif.action)); - }) - .scan_row_max1(notif.id); - - if (mtarget_collection) - { - tx.exec( - "INSERT INTO rwgps_notification_target_collection " - " (notification_id, id, type_, url) " - "VALUES (:notification_id, :id, :type, :url)", - [¬if, &mtarget_collection](auto& s) - { - s.bind(":notification_id", notif.id); - s.bind(":id", mtarget_collection->id); - s.bind(":type", try_to_string(mtarget_collection->type)); - s.bind(":url", mtarget_collection->url); - }); - } - } -} - -auto open(util::zstring_view filename) -> std::shared_ptr -{ - auto dbc = sqlite3::open(filename); - try - { - auto mversion = std::optional{}; - dbc.query("SELECT version FROM migration;").scan_row_max1(mversion); - if (!mversion) - throw std::runtime_error{"failed to fetch database migration version"}; - if (mversion != schema::expected_version) - { - throw std::runtime_error{std::format( - "database migration version ({}) does not match expected " - "version ({}), consider running migrations", - *mversion, schema::expected_version)}; - } - } - catch (std::exception const& e) - { - throw std::runtime_error{std::format( - "failed to query database version: {}", e.what())}; - } - return std::shared_ptr{new connection{std::move(dbc)}}; -} - -} // namespace routemon::database -- cgit v1.3.1