Skip to content

Commit

Permalink
merge: #4054
Browse files Browse the repository at this point in the history
4054: feat(module-index-server): add schema data to module index table before backfill r=zacharyhamm a=zacharyhamm



Co-authored-by: Zachary Hamm <[email protected]>
  • Loading branch information
si-bors-ng[bot] and zacharyhamm authored Jun 27, 2024
2 parents a3a63c5 + 2b9604a commit c1dc506
Show file tree
Hide file tree
Showing 6 changed files with 181 additions and 83 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
ALTER TABLE modules
ADD schema_name TEXT,
ADD schema_category TEXT,
ADD schema_id ident;
94 changes: 12 additions & 82 deletions lib/module-index-server/src/models/si_module.rs
Original file line number Diff line number Diff line change
@@ -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")]
Expand Down Expand Up @@ -93,94 +98,19 @@ pub struct Model {
pub kind: ModuleKind,
pub is_builtin_at: Option<DateTimeWithTimeZone>,
pub is_builtin_at_by_display_name: Option<String>,
#[sea_orm(column_type = "Text")]
pub schema_name: Option<String>,
#[sea_orm(column_type = "Text")]
pub schema_category: Option<String>,
#[sea_orm(column_type = r##"custom("ident")"##)]
pub schema_id: Option<SchemaId>,
}

#[derive(Copy, Clone, Debug, EnumIter, DeriveRelation)]
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<ModuleId> 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<String> for ModuleId {
type Error = sea_orm::DbErr;
fn try_from(s: String) -> Result<Self, Self::Error> {
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<Self, sea_orm::DbErr> {
Err(sea_orm::DbErr::Exec(sea_orm::RuntimeErr::Internal(
format!("{} cannot be converted from u64", stringify!(ModuleId)),
)))
}
}

impl From<ModuleId> 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<I: sea_orm::ColIdx>(res: &QueryResult, idx: I) -> Result<Self, TryGetError> {
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<Self, sea_query::ValueTypeErr> {
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<module_index_client::ModuleDetailsResponse> for Model {
type Error = crate::routes::upsert_module_route::UpsertModuleError;

Expand Down
79 changes: 79 additions & 0 deletions lib/module-index-server/src/models/si_module/module_id.rs
Original file line number Diff line number Diff line change
@@ -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<ModuleId> 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<String> for ModuleId {
type Error = sea_orm::DbErr;
fn try_from(s: String) -> Result<Self, Self::Error> {
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<Self, sea_orm::DbErr> {
Err(sea_orm::DbErr::Exec(sea_orm::RuntimeErr::Internal(
format!("{} cannot be converted from u64", stringify!(ModuleId)),
)))
}
}

impl From<ModuleId> 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<I: sea_orm::ColIdx>(res: &QueryResult, idx: I) -> Result<Self, TryGetError> {
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<Self, sea_query::ValueTypeErr> {
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)
}
}
79 changes: 79 additions & 0 deletions lib/module-index-server/src/models/si_module/schema_id.rs
Original file line number Diff line number Diff line change
@@ -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<SchemaId> 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<String> for SchemaId {
type Error = sea_orm::DbErr;
fn try_from(s: String) -> Result<Self, Self::Error> {
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<Self, sea_orm::DbErr> {
Err(sea_orm::DbErr::Exec(sea_orm::RuntimeErr::Internal(
format!("{} cannot be converted from u64", stringify!(SchemaId)),
)))
}
}

impl From<SchemaId> 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<I: sea_orm::ColIdx>(res: &QueryResult, idx: I) -> Result<Self, TryGetError> {
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<Self, sea_query::ValueTypeErr> {
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)
}
}
5 changes: 4 additions & 1 deletion lib/module-index-server/src/routes/promote_builtin_route.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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?;

Expand Down
3 changes: 3 additions & 0 deletions lib/module-index-server/src/routes/reject_module_route.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)?;
Expand Down

0 comments on commit c1dc506

Please sign in to comment.