From 2b9604a32255b7d126dbb3f651672e4b85e57596 Mon Sep 17 00:00:00 2001 From: Zachary Hamm Date: Thu, 27 Jun 2024 17:00:40 -0500 Subject: [PATCH] feat(module-index-server): add schema data to module index table before backfill --- .../U0006__modules_schema_id_etc.sql | 4 + .../src/models/si_module.rs | 94 +++---------------- .../src/models/si_module/module_id.rs | 79 ++++++++++++++++ .../src/models/si_module/schema_id.rs | 79 ++++++++++++++++ .../src/routes/promote_builtin_route.rs | 5 +- .../src/routes/reject_module_route.rs | 3 + 6 files changed, 181 insertions(+), 83 deletions(-) create mode 100644 lib/module-index-server/src/migrations/U0006__modules_schema_id_etc.sql create mode 100644 lib/module-index-server/src/models/si_module/module_id.rs create mode 100644 lib/module-index-server/src/models/si_module/schema_id.rs diff --git a/lib/module-index-server/src/migrations/U0006__modules_schema_id_etc.sql b/lib/module-index-server/src/migrations/U0006__modules_schema_id_etc.sql new file mode 100644 index 0000000000..0b1e9471c2 --- /dev/null +++ b/lib/module-index-server/src/migrations/U0006__modules_schema_id_etc.sql @@ -0,0 +1,4 @@ +ALTER TABLE modules + ADD schema_name TEXT, + ADD schema_category TEXT, + ADD schema_id ident; \ No newline at end of file diff --git a/lib/module-index-server/src/models/si_module.rs b/lib/module-index-server/src/models/si_module.rs index 539394559d..21626b968f 100644 --- a/lib/module-index-server/src/models/si_module.rs +++ b/lib/module-index-server/src/models/si_module.rs @@ -1,7 +1,12 @@ use sea_orm::{entity::prelude::*, sea_query, TryGetError}; use serde::{Deserialize, Serialize}; use std::str::FromStr; -use ulid::Ulid; + +pub mod module_id; +pub mod schema_id; + +pub use module_id::ModuleId; +pub use schema_id::SchemaId; #[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)] #[serde(rename_all = "camelCase")] @@ -93,6 +98,12 @@ pub struct Model { pub kind: ModuleKind, pub is_builtin_at: Option, pub is_builtin_at_by_display_name: Option, + #[sea_orm(column_type = "Text")] + pub schema_name: Option, + #[sea_orm(column_type = "Text")] + pub schema_category: Option, + #[sea_orm(column_type = r##"custom("ident")"##)] + pub schema_id: Option, } #[derive(Copy, Clone, Debug, EnumIter, DeriveRelation)] @@ -100,87 +111,6 @@ pub enum Relation {} impl ActiveModelBehavior for ActiveModel {} -// custom ulid type - -#[derive(Clone, Copy, Debug, PartialEq, Eq, Serialize, Deserialize)] -pub struct ModuleId(pub Ulid); - -impl From for Value { - fn from(source: ModuleId) -> Self { - Value::String(Some(Box::new(source.0.to_string()))) - // Value::String(serde_json::to_string(&source).ok().map(Box::new)) - } -} - -impl std::fmt::Display for ModuleId { - fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { - write!(f, "{}", self.0) - } -} - -impl TryFrom for ModuleId { - type Error = sea_orm::DbErr; - fn try_from(s: String) -> Result { - Ok(ModuleId( - Ulid::from_string(&s).map_err(|err| DbErr::Type(err.to_string()))?, - )) - } -} - -impl sea_orm::TryFromU64 for ModuleId { - fn try_from_u64(_: u64) -> Result { - Err(sea_orm::DbErr::Exec(sea_orm::RuntimeErr::Internal( - format!("{} cannot be converted from u64", stringify!(ModuleId)), - ))) - } -} - -impl From for String { - fn from(val: ModuleId) -> Self { - val.0.to_string() - } -} - -impl sea_orm::sea_query::Nullable for ModuleId { - fn null() -> sea_orm::Value { - sea_orm::Value::String(None) - } -} - -impl sea_orm::TryGetable for ModuleId { - fn try_get_by(res: &QueryResult, idx: I) -> Result { - let json_str: String = res.try_get_by(idx).map_err(TryGetError::DbErr)?; - Ulid::from_string(&json_str) - .map_err(|e| TryGetError::DbErr(DbErr::Type(e.to_string()))) - .map(ModuleId) - // serde_json::from_str(&json_str).map_err(|e| TryGetError::DbErr(DbErr::Json(e.to_string()))) - } -} - -impl sea_query::ValueType for ModuleId { - fn try_from(v: Value) -> Result { - match v { - Value::String(Some(x)) => Ok(ModuleId( - Ulid::from_string(&x).map_err(|_| sea_query::ValueTypeErr)?, - // serde_json::from_str(&x).map_err(|_| sea_query::ValueTypeErr)?, - )), - _ => Err(sea_query::ValueTypeErr), - } - } - - fn type_name() -> String { - stringify!(ModuleId).to_owned() - } - - fn array_type() -> sea_orm::sea_query::ArrayType { - sea_orm::sea_query::ArrayType::String - } - - fn column_type() -> sea_query::ColumnType { - sea_query::ColumnType::String(None) - } -} - impl TryInto for Model { type Error = crate::routes::upsert_module_route::UpsertModuleError; diff --git a/lib/module-index-server/src/models/si_module/module_id.rs b/lib/module-index-server/src/models/si_module/module_id.rs new file mode 100644 index 0000000000..c67126f810 --- /dev/null +++ b/lib/module-index-server/src/models/si_module/module_id.rs @@ -0,0 +1,79 @@ +use sea_orm::{entity::prelude::*, sea_query, TryGetError}; +use serde::{Deserialize, Serialize}; +use ulid::Ulid; + +#[derive(Clone, Copy, Debug, PartialEq, Eq, Serialize, Deserialize)] +pub struct ModuleId(pub Ulid); + +impl From for Value { + fn from(source: ModuleId) -> Self { + Value::String(Some(Box::new(source.0.to_string()))) + } +} + +impl std::fmt::Display for ModuleId { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + write!(f, "{}", self.0) + } +} + +impl TryFrom for ModuleId { + type Error = sea_orm::DbErr; + fn try_from(s: String) -> Result { + Ok(ModuleId( + Ulid::from_string(&s).map_err(|err| DbErr::Type(err.to_string()))?, + )) + } +} + +impl sea_orm::TryFromU64 for ModuleId { + fn try_from_u64(_: u64) -> Result { + Err(sea_orm::DbErr::Exec(sea_orm::RuntimeErr::Internal( + format!("{} cannot be converted from u64", stringify!(ModuleId)), + ))) + } +} + +impl From for String { + fn from(val: ModuleId) -> Self { + val.0.to_string() + } +} + +impl sea_orm::sea_query::Nullable for ModuleId { + fn null() -> sea_orm::Value { + sea_orm::Value::String(None) + } +} + +impl sea_orm::TryGetable for ModuleId { + fn try_get_by(res: &QueryResult, idx: I) -> Result { + let json_str: String = res.try_get_by(idx).map_err(TryGetError::DbErr)?; + Ulid::from_string(&json_str) + .map_err(|e| TryGetError::DbErr(DbErr::Type(e.to_string()))) + .map(ModuleId) + } +} + +impl sea_query::ValueType for ModuleId { + fn try_from(v: Value) -> Result { + match v { + Value::String(Some(x)) => Ok(ModuleId( + Ulid::from_string(&x).map_err(|_| sea_query::ValueTypeErr)?, + )), + _ => Err(sea_query::ValueTypeErr), + } + } + + fn type_name() -> String { + stringify!(ModuleId).to_owned() + } + + fn array_type() -> sea_orm::sea_query::ArrayType { + sea_orm::sea_query::ArrayType::String + } + + fn column_type() -> sea_query::ColumnType { + sea_query::ColumnType::String(None) + } +} diff --git a/lib/module-index-server/src/models/si_module/schema_id.rs b/lib/module-index-server/src/models/si_module/schema_id.rs new file mode 100644 index 0000000000..df5b2cf7e5 --- /dev/null +++ b/lib/module-index-server/src/models/si_module/schema_id.rs @@ -0,0 +1,79 @@ +use sea_orm::{entity::prelude::*, sea_query, TryGetError}; +use serde::{Deserialize, Serialize}; +use ulid::Ulid; + +#[derive(Clone, Copy, Debug, PartialEq, Eq, Serialize, Deserialize)] +pub struct SchemaId(pub Ulid); + +impl From for Value { + fn from(source: SchemaId) -> Self { + Value::String(Some(Box::new(source.0.to_string()))) + } +} + +impl std::fmt::Display for SchemaId { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + write!(f, "{}", self.0) + } +} + +impl TryFrom for SchemaId { + type Error = sea_orm::DbErr; + fn try_from(s: String) -> Result { + Ok(SchemaId( + Ulid::from_string(&s).map_err(|err| DbErr::Type(err.to_string()))?, + )) + } +} + +impl sea_orm::TryFromU64 for SchemaId { + fn try_from_u64(_: u64) -> Result { + Err(sea_orm::DbErr::Exec(sea_orm::RuntimeErr::Internal( + format!("{} cannot be converted from u64", stringify!(SchemaId)), + ))) + } +} + +impl From for String { + fn from(val: SchemaId) -> Self { + val.0.to_string() + } +} + +impl sea_orm::sea_query::Nullable for SchemaId { + fn null() -> sea_orm::Value { + sea_orm::Value::String(None) + } +} + +impl sea_orm::TryGetable for SchemaId { + fn try_get_by(res: &QueryResult, idx: I) -> Result { + let json_str: String = res.try_get_by(idx).map_err(TryGetError::DbErr)?; + Ulid::from_string(&json_str) + .map_err(|e| TryGetError::DbErr(DbErr::Type(e.to_string()))) + .map(SchemaId) + } +} + +impl sea_query::ValueType for SchemaId { + fn try_from(v: Value) -> Result { + match v { + Value::String(Some(x)) => Ok(SchemaId( + Ulid::from_string(&x).map_err(|_| sea_query::ValueTypeErr)?, + )), + _ => Err(sea_query::ValueTypeErr), + } + } + + fn type_name() -> String { + stringify!(SchemaId).to_owned() + } + + fn array_type() -> sea_orm::sea_query::ArrayType { + sea_orm::sea_query::ArrayType::String + } + + fn column_type() -> sea_query::ColumnType { + sea_query::ColumnType::String(None) + } +} diff --git a/lib/module-index-server/src/routes/promote_builtin_route.rs b/lib/module-index-server/src/routes/promote_builtin_route.rs index 80bfa33a1e..96b2464d36 100644 --- a/lib/module-index-server/src/routes/promote_builtin_route.rs +++ b/lib/module-index-server/src/routes/promote_builtin_route.rs @@ -91,9 +91,12 @@ pub async fn promote_builtin_route( Utc.fix(), ))), is_builtin_at_by_display_name: Set(Some(data)), + schema_name: Set(module.schema_name), + schema_category: Set(module.schema_category), + schema_id: Set(module.schema_id), }; - let updated_module: si_module::Model = dbg!(active_module.update(&txn).await)?; + let updated_module: si_module::Model = active_module.update(&txn).await?; txn.commit().await?; diff --git a/lib/module-index-server/src/routes/reject_module_route.rs b/lib/module-index-server/src/routes/reject_module_route.rs index 5052987c35..2c08c7810e 100644 --- a/lib/module-index-server/src/routes/reject_module_route.rs +++ b/lib/module-index-server/src/routes/reject_module_route.rs @@ -91,6 +91,9 @@ pub async fn reject_module( kind: Set(module.kind), is_builtin_at: Set(module.is_builtin_at), is_builtin_at_by_display_name: Set(module.is_builtin_at_by_display_name), + schema_category: Set(module.schema_category), + schema_name: Set(module.schema_name), + schema_id: Set(module.schema_id), }; let updated_module: si_module::Model = dbg!(active_module.update(&txn).await)?;