From b54bf3c4d546b02581c6a1a38a651ea578ca9aec Mon Sep 17 00:00:00 2001 From: Valentin Tolmer Date: Mon, 11 Sep 2023 17:00:20 +0200 Subject: [PATCH] server: clean up database-mapped types --- server/src/domain/sql_tables.rs | 19 +-- server/src/domain/types.rs | 197 ++------------------------------ 2 files changed, 13 insertions(+), 203 deletions(-) diff --git a/server/src/domain/sql_tables.rs b/server/src/domain/sql_tables.rs index 25f12e3..358af62 100644 --- a/server/src/domain/sql_tables.rs +++ b/server/src/domain/sql_tables.rs @@ -1,26 +1,11 @@ use super::sql_migrations::{get_schema_version, migrate_from_version, upgrade_to_v1}; -use sea_orm::Value; +use sea_orm::{DeriveValueType, QueryResult, Value}; pub type DbConnection = sea_orm::DatabaseConnection; -#[derive(Copy, PartialEq, Eq, Debug, Clone, PartialOrd, Ord)] +#[derive(Copy, PartialEq, Eq, Debug, Clone, PartialOrd, Ord, DeriveValueType)] pub struct SchemaVersion(pub i16); -impl sea_orm::TryGetable for SchemaVersion { - fn try_get_by( - res: &sea_orm::QueryResult, - index: I, - ) -> Result { - Ok(SchemaVersion(i16::try_get_by(res, index)?)) - } -} - -impl From for Value { - fn from(version: SchemaVersion) -> Self { - version.0.into() - } -} - pub const LAST_SCHEMA_VERSION: SchemaVersion = SchemaVersion(5); pub async fn init_table(pool: &DbConnection) -> anyhow::Result<()> { diff --git a/server/src/domain/types.rs b/server/src/domain/types.rs index c231243..52f0878 100644 --- a/server/src/domain/types.rs +++ b/server/src/domain/types.rs @@ -3,15 +3,16 @@ use chrono::{NaiveDateTime, TimeZone}; use sea_orm::{ entity::IntoActiveValue, sea_query::{value::ValueType, ArrayType, BlobSize, ColumnType, Nullable, ValueTypeErr}, - DbErr, QueryResult, TryFromU64, TryGetError, TryGetable, Value, + DbErr, DeriveValueType, QueryResult, TryFromU64, TryGetError, TryGetable, Value, }; use serde::{Deserialize, Serialize}; use strum::{EnumString, IntoStaticStr}; pub use super::model::{GroupColumn, UserColumn}; -#[derive(PartialEq, Hash, Eq, Clone, Debug, Default, Serialize, Deserialize)] +#[derive(PartialEq, Hash, Eq, Clone, Debug, Default, Serialize, Deserialize, DeriveValueType)] #[serde(try_from = "&str")] +#[sea_orm(column_type = "String(Some(36))")] pub struct Uuid(String); impl Uuid { @@ -54,48 +55,6 @@ impl std::string::ToString for Uuid { } } -impl TryGetable for Uuid { - fn try_get_by( - res: &QueryResult, - index: I, - ) -> std::result::Result { - Ok(Uuid(String::try_get_by(res, index)?)) - } -} - -impl ValueType for Uuid { - fn try_from(v: Value) -> Result { - >::try_from( - ::try_from(v)?.as_str(), - ) - .map_err(|_| ValueTypeErr {}) - } - - fn type_name() -> String { - "Uuid".to_owned() - } - - fn array_type() -> ArrayType { - ArrayType::String - } - - fn column_type() -> ColumnType { - ColumnType::String(Some(36)) - } -} - -impl From for Value { - fn from(uuid: Uuid) -> Self { - uuid.as_str().into() - } -} - -impl From<&Uuid> for Value { - fn from(uuid: &Uuid) -> Self { - uuid.as_str().into() - } -} - #[cfg(test)] #[macro_export] macro_rules! uuid { @@ -104,7 +63,8 @@ macro_rules! uuid { }; } -#[derive(Clone, PartialEq, Eq, Serialize, Deserialize)] +#[derive(Clone, PartialEq, Eq, Serialize, Deserialize, DeriveValueType)] +#[sea_orm(column_type = "Binary(BlobSize::Long)", array_type = "Bytes")] pub struct Serialized(Vec); const SERIALIZED_I64_LEN: usize = 8; @@ -160,45 +120,9 @@ impl Serialized { } } -impl From for Value { - fn from(ser: Serialized) -> Self { - ser.0.into() - } -} - -impl TryGetable for Serialized { - fn try_get_by(res: &QueryResult, index: I) -> Result { - Ok(Self(Vec::::try_get_by(res, index)?)) - } -} - -impl TryFromU64 for Serialized { - fn try_from_u64(_n: u64) -> Result { - Err(DbErr::ConvertFromU64( - "Serialized cannot be constructed from u64", - )) - } -} - -impl ValueType for Serialized { - fn try_from(v: Value) -> Result { - Ok(Self( as ValueType>::try_from(v)?)) - } - - fn type_name() -> String { - "Serialized".to_owned() - } - - fn array_type() -> ArrayType { - ArrayType::Bytes - } - - fn column_type() -> ColumnType { - ColumnType::Binary(BlobSize::Long) - } -} - -#[derive(PartialEq, Eq, PartialOrd, Ord, Clone, Debug, Default, Serialize, Deserialize)] +#[derive( + PartialEq, Eq, PartialOrd, Ord, Clone, Debug, Default, Serialize, Deserialize, DeriveValueType, +)] #[serde(from = "String")] pub struct UserId(String); @@ -228,24 +152,12 @@ impl From for UserId { } } -impl From for Value { - fn from(user_id: UserId) -> Self { - user_id.into_string().into() - } -} - impl From<&UserId> for Value { fn from(user_id: &UserId) -> Self { user_id.as_str().into() } } -impl TryGetable for UserId { - fn try_get_by(res: &QueryResult, index: I) -> Result { - Ok(UserId::new(&String::try_get_by(res, index)?)) - } -} - impl TryFromU64 for UserId { fn try_from_u64(_n: u64) -> Result { Err(DbErr::ConvertFromU64( @@ -254,33 +166,10 @@ impl TryFromU64 for UserId { } } -impl ValueType for UserId { - fn try_from(v: Value) -> Result { - Ok(UserId::new(::try_from(v)?.as_str())) - } - - fn type_name() -> String { - "UserId".to_owned() - } - - fn array_type() -> ArrayType { - ArrayType::String - } - - fn column_type() -> ColumnType { - ColumnType::String(Some(255)) - } -} - -#[derive(PartialEq, Eq, Clone, Serialize, Deserialize)] +#[derive(PartialEq, Eq, Clone, Serialize, Deserialize, DeriveValueType)] +#[sea_orm(column_type = "Binary(BlobSize::Long)", array_type = "Bytes")] pub struct JpegPhoto(#[serde(with = "serde_bytes")] Vec); -impl From for Value { - fn from(photo: JpegPhoto) -> Self { - photo.0.into() - } -} - impl From<&JpegPhoto> for Value { fn from(photo: &JpegPhoto) -> Self { photo.0.as_slice().into() @@ -378,40 +267,6 @@ impl JpegPhoto { } } -impl TryGetable for JpegPhoto { - fn try_get_by(res: &QueryResult, index: I) -> Result { - >>::try_from(Vec::::try_get_by(res, index)?) - .map_err(|e| { - TryGetError::DbErr(DbErr::TryIntoErr { - from: "[u8]", - into: "JpegPhoto", - source: e.into(), - }) - }) - } -} - -impl ValueType for JpegPhoto { - fn try_from(v: Value) -> Result { - >::try_from( - as sea_orm::sea_query::ValueType>::try_from(v)?.as_slice(), - ) - .map_err(|_| ValueTypeErr {}) - } - - fn type_name() -> String { - "JpegPhoto".to_owned() - } - - fn array_type() -> ArrayType { - ArrayType::Bytes - } - - fn column_type() -> ColumnType { - ColumnType::Binary(BlobSize::Long) - } -} - impl Nullable for JpegPhoto { fn null() -> Value { JpegPhoto::null().into() @@ -459,39 +314,9 @@ impl Default for User { } } -#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)] +#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash, Serialize, Deserialize, DeriveValueType)] pub struct GroupId(pub i32); -impl From for Value { - fn from(group_id: GroupId) -> Self { - group_id.0.into() - } -} - -impl TryGetable for GroupId { - fn try_get_by(res: &QueryResult, index: I) -> Result { - Ok(GroupId(i32::try_get_by(res, index)?)) - } -} - -impl ValueType for GroupId { - fn try_from(v: Value) -> Result { - Ok(GroupId(::try_from(v)?)) - } - - fn type_name() -> String { - "GroupId".to_owned() - } - - fn array_type() -> ArrayType { - ArrayType::Int - } - - fn column_type() -> ColumnType { - ColumnType::Integer - } -} - impl TryFromU64 for GroupId { fn try_from_u64(n: u64) -> Result { Ok(GroupId(i32::try_from_u64(n)?))