From 67a054ace23111bc4a6a13965dc0c7d70c7c21fc Mon Sep 17 00:00:00 2001 From: Frank Elsinga Date: Wed, 10 Jul 2024 21:52:29 +0200 Subject: [PATCH] switched `calendar.entry_type` from an enum to a string --- ...aff2842cad45867c3a72f807ae08c1b10cb37.json | 15 +------------ ...7645c4b7413815693ebe3db92a62f2909dab.json} | 21 ++++--------------- server/main-api/Cargo.toml | 3 ++- .../20240710191621_no_enum_types.down.sql | 3 +++ .../20240710191621_no_enum_types.up.sql | 2 ++ server/main-api/src/calendar/mod.rs | 12 +++++------ server/main-api/src/calendar/models.rs | 14 +++++++++++-- 7 files changed, 30 insertions(+), 40 deletions(-) rename server/.sqlx/{query-d2978b1b6a3b39bedda8e40cc2e53f91b262d8c42e7a6886ce25a14975c87f70.json => query-bc0955e2d7a86890ddbe05808ef57645c4b7413815693ebe3db92a62f2909dab.json} (65%) create mode 100644 server/main-api/migrations/20240710191621_no_enum_types.down.sql create mode 100644 server/main-api/migrations/20240710191621_no_enum_types.up.sql diff --git a/server/.sqlx/query-1ffddd843ece79fa10cfb98c2e8aff2842cad45867c3a72f807ae08c1b10cb37.json b/server/.sqlx/query-1ffddd843ece79fa10cfb98c2e8aff2842cad45867c3a72f807ae08c1b10cb37.json index 0d37f5584..227c836a8 100644 --- a/server/.sqlx/query-1ffddd843ece79fa10cfb98c2e8aff2842cad45867c3a72f807ae08c1b10cb37.json +++ b/server/.sqlx/query-1ffddd843ece79fa10cfb98c2e8aff2842cad45867c3a72f807ae08c1b10cb37.json @@ -12,20 +12,7 @@ "Text", "Text", "Text", - { - "Custom": { - "name": "eventtype", - "kind": { - "Enum": [ - "lecture", - "exercise", - "exam", - "barred", - "other" - ] - } - } - }, + "Text", "Text" ] }, diff --git a/server/.sqlx/query-d2978b1b6a3b39bedda8e40cc2e53f91b262d8c42e7a6886ce25a14975c87f70.json b/server/.sqlx/query-bc0955e2d7a86890ddbe05808ef57645c4b7413815693ebe3db92a62f2909dab.json similarity index 65% rename from server/.sqlx/query-d2978b1b6a3b39bedda8e40cc2e53f91b262d8c42e7a6886ce25a14975c87f70.json rename to server/.sqlx/query-bc0955e2d7a86890ddbe05808ef57645c4b7413815693ebe3db92a62f2909dab.json index 3d1a0ec11..78422b683 100644 --- a/server/.sqlx/query-d2978b1b6a3b39bedda8e40cc2e53f91b262d8c42e7a6886ce25a14975c87f70.json +++ b/server/.sqlx/query-bc0955e2d7a86890ddbe05808ef57645c4b7413815693ebe3db92a62f2909dab.json @@ -1,6 +1,6 @@ { "db_name": "PostgreSQL", - "query": "SELECT id,room_code,start_at,end_at,stp_title_de,stp_title_en,stp_type,entry_type AS \"entry_type!:crate::calendar::models::EventType\",detailed_entry_type\n FROM calendar\n WHERE room_code = $1 AND start_at >= $2 AND end_at <= $3", + "query": "SELECT id,room_code,start_at,end_at,stp_title_de,stp_title_en,stp_type,entry_type,detailed_entry_type\n FROM calendar\n WHERE room_code = $1 AND start_at >= $2 AND end_at <= $3", "describe": { "columns": [ { @@ -40,21 +40,8 @@ }, { "ordinal": 7, - "name": "entry_type!:crate::calendar::models::EventType", - "type_info": { - "Custom": { - "name": "eventtype", - "kind": { - "Enum": [ - "lecture", - "exercise", - "exam", - "barred", - "other" - ] - } - } - } + "name": "entry_type", + "type_info": "Text" }, { "ordinal": 8, @@ -81,5 +68,5 @@ false ] }, - "hash": "d2978b1b6a3b39bedda8e40cc2e53f91b262d8c42e7a6886ce25a14975c87f70" + "hash": "bc0955e2d7a86890ddbe05808ef57645c4b7413815693ebe3db92a62f2909dab" } diff --git a/server/main-api/Cargo.toml b/server/main-api/Cargo.toml index 4e4030b03..1f96e3b51 100644 --- a/server/main-api/Cargo.toml +++ b/server/main-api/Cargo.toml @@ -40,7 +40,8 @@ futures = "0.3.30" unicode-truncate = { git = "https://github.com/CommanderStorm/unicode-truncate.git", rev = "5cc7798"} # database -sqlx = { version = "0.7.4", features = ["postgres", "runtime-tokio", "tls-rustls", "migrate", "macros", "chrono"] } +# v0.8 needs https://github.com/launchbadge/sqlx/issues/3316 is resolved first +sqlx = { version = "0.7.4", features = ["postgres", "runtime-tokio", "tls-rustls", "migrate", "macros", "chrono", "json"] } chrono = { version = "0.4.38", default-features = false, features = ["serde"] } # search diff --git a/server/main-api/migrations/20240710191621_no_enum_types.down.sql b/server/main-api/migrations/20240710191621_no_enum_types.down.sql new file mode 100644 index 000000000..6d1e43654 --- /dev/null +++ b/server/main-api/migrations/20240710191621_no_enum_types.down.sql @@ -0,0 +1,3 @@ +-- Add down migration script here +DELETE FROM calendar WHERE 1=1 -- to make migrating simpler and because it is possible +alter table calendar alter column entry_type type entry_type; diff --git a/server/main-api/migrations/20240710191621_no_enum_types.up.sql b/server/main-api/migrations/20240710191621_no_enum_types.up.sql new file mode 100644 index 000000000..7a1092c4a --- /dev/null +++ b/server/main-api/migrations/20240710191621_no_enum_types.up.sql @@ -0,0 +1,2 @@ +-- Add up migration script here +alter table calendar alter column entry_type type text using entry_type::text diff --git a/server/main-api/src/calendar/mod.rs b/server/main-api/src/calendar/mod.rs index cdbf42e7f..17a751de7 100644 --- a/server/main-api/src/calendar/mod.rs +++ b/server/main-api/src/calendar/mod.rs @@ -116,7 +116,7 @@ async fn get_from_db( ) -> Result, crate::BoxedError> { let mut located_events: HashMap = HashMap::new(); for location in locations { - let events = sqlx::query_as!(Event, r#"SELECT id,room_code,start_at,end_at,stp_title_de,stp_title_en,stp_type,entry_type AS "entry_type!:crate::calendar::models::EventType",detailed_entry_type + let events = sqlx::query_as!(Event, r#"SELECT id,room_code,start_at,end_at,stp_title_de,stp_title_en,stp_type,entry_type,detailed_entry_type FROM calendar WHERE room_code = $1 AND start_at >= $2 AND end_at <= $3"#, location.key, start_after, end_before).fetch_all(pool).await?; @@ -202,7 +202,7 @@ mod tests { stp_title_de: "Quantenteleportation".into(), stp_title_en: "Quantum teleportation".into(), stp_type: "Vorlesung mit Zentralübung".into(), - entry_type: models::EventType::Lecture, + entry_type: models::EventType::Lecture.to_string(), detailed_entry_type: "Abhaltung".into(), }, Event { @@ -213,7 +213,7 @@ mod tests { stp_title_de: "Quantenteleportation 2".into(), stp_title_en: "Quantum teleportation 2".into(), stp_type: "Vorlesung mit Zentralübung".into(), - entry_type: models::EventType::Lecture, + entry_type: models::EventType::Lecture.to_string(), detailed_entry_type: "Abhaltung".into(), }, Event { @@ -224,7 +224,7 @@ mod tests { stp_title_de: "Wartung".into(), stp_title_en: "maintenance".into(), stp_type: "Vorlesung mit Zentralübung".into(), - entry_type: models::EventType::Barred, + entry_type: models::EventType::Barred.to_string(), detailed_entry_type: "Abhaltung".into(), }, Event { @@ -235,7 +235,7 @@ mod tests { stp_title_de: "Quantenteleportation 3".into(), stp_title_en: "Quantum teleportation 3".into(), stp_type: "Vorlesung".into(), - entry_type: models::EventType::Other, + entry_type: models::EventType::Other.to_string(), detailed_entry_type: "Abhaltung".into(), }, Event { @@ -246,7 +246,7 @@ mod tests { stp_title_de: "Quantenteleportation 3".into(), stp_title_en: "Quantum teleportation 3".into(), stp_type: "Vorlesung".into(), - entry_type: models::EventType::Exam, + entry_type: models::EventType::Exam.to_string(), detailed_entry_type: "Abhaltung".into(), }, ], diff --git a/server/main-api/src/calendar/models.rs b/server/main-api/src/calendar/models.rs index fc85c4c2b..157d6ca6b 100644 --- a/server/main-api/src/calendar/models.rs +++ b/server/main-api/src/calendar/models.rs @@ -1,3 +1,5 @@ +use std::fmt::Display; + use chrono::{DateTime, Utc}; use serde::{Deserialize, Serialize}; @@ -48,7 +50,8 @@ pub(super) struct Event { /// e.g. Vorlesung mit Zentralübung pub(super) stp_type: String, /// e.g. lecture - pub(super) entry_type: EventType, + /// in reality this is a [EventType] + pub(super) entry_type: String, /// e.g. Abhaltung pub(super) detailed_entry_type: String, } @@ -77,7 +80,7 @@ impl Event { self.stp_title_de, self.stp_title_en, self.stp_type, - self.entry_type.clone() as EventType, // see https://github.com/launchbadge/sqlx/issues/1004 => our type is not possible (?) + self.entry_type, self.detailed_entry_type, ).execute(&mut **tx).await } @@ -94,3 +97,10 @@ pub enum EventType { Barred, Other, } + +impl Display for EventType { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + let str = serde_json::to_string(self).map_err(|_| std::fmt::Error)?; + f.write_str(&str) + } +}