diff options
| author | Rutger Broekhoff | 2026-11-30 11:47:36 +0100 |
|---|---|---|
| committer | Rutger Broekhoff | 2026-11-30 11:47:36 +0100 |
| commit | 02475e05ccd1aa15c6a6163834f28bfebbef796f (patch) | |
| tree | 6bc6f453f07dc76f763310e1e5eade9a3e1115b7 /server/src/database.cpp | |
| parent | 5732f6c14618f50ffaf315b93795340219a67c66 (diff) | |
| download | routemon-02475e05ccd1aa15c6a6163834f28bfebbef796f.tar.gz routemon-02475e05ccd1aa15c6a6163834f28bfebbef796f.zip | |
RWGPS webhook and such
Diffstat (limited to 'server/src/database.cpp')
| -rw-r--r-- | server/src/database.cpp | 116 |
1 files changed, 109 insertions, 7 deletions
diff --git a/server/src/database.cpp b/server/src/database.cpp index 54e5f34..c1f9ab2 100644 --- a/server/src/database.cpp +++ b/server/src/database.cpp | |||
| @@ -4,23 +4,125 @@ import :database; | |||
| 4 | 4 | ||
| 5 | namespace routemon::database { | 5 | namespace routemon::database { |
| 6 | 6 | ||
| 7 | constexpr std::int64_t expected_database_version = 1; | 7 | namespace schema { |
| 8 | 8 | ||
| 9 | auto open(std::string const& filename) -> std::shared_ptr<connection> | 9 | constexpr std::int64_t expected_version = 2; |
| 10 | |||
| 11 | auto try_to_string(rwgps_notification_cause c) -> std::optional<std::string> | ||
| 12 | { | ||
| 13 | switch (c) | ||
| 14 | { | ||
| 15 | case rwgps_notification_cause::unknown: | ||
| 16 | return std::nullopt; | ||
| 17 | case rwgps_notification_cause::created: | ||
| 18 | return "created"; | ||
| 19 | case rwgps_notification_cause::updated: | ||
| 20 | return "updated"; | ||
| 21 | case rwgps_notification_cause::deleted: | ||
| 22 | return "deleted"; | ||
| 23 | case rwgps_notification_cause::added: | ||
| 24 | return "added"; | ||
| 25 | case rwgps_notification_cause::removed: | ||
| 26 | return "removed"; | ||
| 27 | } | ||
| 28 | } | ||
| 29 | |||
| 30 | auto try_to_string(rwgps_notification_item_type t) -> std::optional<std::string> | ||
| 31 | { | ||
| 32 | switch (t) | ||
| 33 | { | ||
| 34 | case rwgps_notification_item_type::unknown: | ||
| 35 | return std::nullopt; | ||
| 36 | case rwgps_notification_item_type::trip: | ||
| 37 | return "trip"; | ||
| 38 | case rwgps_notification_item_type::route: | ||
| 39 | return "route"; | ||
| 40 | case rwgps_notification_item_type::event: | ||
| 41 | return "event"; | ||
| 42 | case rwgps_notification_item_type::event_participant: | ||
| 43 | return "event_participant"; | ||
| 44 | case rwgps_notification_item_type::club_member: | ||
| 45 | return "club_member"; | ||
| 46 | case rwgps_notification_item_type::authorization: | ||
| 47 | return "authorization"; | ||
| 48 | } | ||
| 49 | } | ||
| 50 | |||
| 51 | auto try_to_string(rwgps_notification_target_collection_type t) | ||
| 52 | -> std::optional<std::string> | ||
| 53 | { | ||
| 54 | switch (t) | ||
| 55 | { | ||
| 56 | case rwgps_notification_target_collection_type::unknown: | ||
| 57 | return std::nullopt; | ||
| 58 | case rwgps_notification_target_collection_type::pinned: | ||
| 59 | return "pinned"; | ||
| 60 | } | ||
| 61 | }; | ||
| 62 | |||
| 63 | } // namespace schema | ||
| 64 | |||
| 65 | auto connection::insert_rwgps_notifications( | ||
| 66 | std::vector<std::pair< | ||
| 67 | schema::rwgps_notification, | ||
| 68 | std::optional<schema::rwgps_notification_target_collection> | ||
| 69 | >>& notifs) -> void | ||
| 70 | { | ||
| 71 | auto tx = sqlite3::tx{dbc_}; | ||
| 72 | for (auto& [notif, mtarget_collection] : notifs) | ||
| 73 | { | ||
| 74 | tx.query( | ||
| 75 | "INSERT INTO rwgps_notification " | ||
| 76 | " (rwgps_timestamp, user_id, item_type, item_id, " | ||
| 77 | " item_user_id, item_url, action_) " | ||
| 78 | "VALUES (:rwgps_timestamp, :user_id, :item_type, :item_id, " | ||
| 79 | " :item_user_id, :item_url, :action) " | ||
| 80 | "RETURNING id", | ||
| 81 | [¬if](auto& s) | ||
| 82 | { | ||
| 83 | auto const ts = time::iso8601_secs(notif.rwgps_timestamp); | ||
| 84 | s.bind(":rwgps_timestamp", std::string_view{ts}); | ||
| 85 | s.bind(":user_id", notif.user_id); | ||
| 86 | s.bind(":item_type", try_to_string(notif.item_type)); | ||
| 87 | s.bind(":item_id", notif.item_id); | ||
| 88 | s.bind(":item_user_id", notif.item_user_id); | ||
| 89 | s.bind(":item_url", notif.item_url); | ||
| 90 | s.bind(":action", try_to_string(notif.action)); | ||
| 91 | }) | ||
| 92 | .scan_row_max1(notif.id); | ||
| 93 | |||
| 94 | if (mtarget_collection) | ||
| 95 | { | ||
| 96 | tx.exec( | ||
| 97 | "INSERT INTO rwgps_notification_target_collection " | ||
| 98 | " (notification_id, id, type_, url) " | ||
| 99 | "VALUES (:notification_id, :id, :type, :url)", | ||
| 100 | [¬if, &mtarget_collection](auto& s) | ||
| 101 | { | ||
| 102 | s.bind(":notification_id", notif.id); | ||
| 103 | s.bind(":id", mtarget_collection->id); | ||
| 104 | s.bind(":type", try_to_string(mtarget_collection->type)); | ||
| 105 | s.bind(":url", mtarget_collection->url); | ||
| 106 | }); | ||
| 107 | } | ||
| 108 | } | ||
| 109 | } | ||
| 110 | |||
| 111 | auto open(util::zstring_view filename) -> std::shared_ptr<connection> | ||
| 10 | { | 112 | { |
| 11 | auto dbc = sqlite3::open(filename); | 113 | auto dbc = sqlite3::open(filename); |
| 12 | try | 114 | try |
| 13 | { | 115 | { |
| 14 | auto version = std::optional<std::int64_t>{}; | 116 | auto mversion = std::optional<std::int64_t>{}; |
| 15 | dbc.query("SELECT version FROM migration;").scan_single(version); | 117 | dbc.query("SELECT version FROM migration;").scan_row_max1(mversion); |
| 16 | if (!version) | 118 | if (!mversion) |
| 17 | throw std::runtime_error{"failed to fetch database migration version"}; | 119 | throw std::runtime_error{"failed to fetch database migration version"}; |
| 18 | if (version != expected_database_version) | 120 | if (mversion != schema::expected_version) |
| 19 | { | 121 | { |
| 20 | throw std::runtime_error{std::format( | 122 | throw std::runtime_error{std::format( |
| 21 | "database migration version ({}) does not match expected " | 123 | "database migration version ({}) does not match expected " |
| 22 | "version ({}), consider running migrations", | 124 | "version ({}), consider running migrations", |
| 23 | *version, expected_database_version)}; | 125 | *mversion, schema::expected_version)}; |
| 24 | } | 126 | } |
| 25 | } | 127 | } |
| 26 | catch (std::exception const& e) | 128 | catch (std::exception const& e) |