diff --git a/nixin_farm_ssr/migration/src/lib.rs b/nixin_farm_ssr/migration/src/lib.rs index 9355d87..5b28008 100644 --- a/nixin_farm_ssr/migration/src/lib.rs +++ b/nixin_farm_ssr/migration/src/lib.rs @@ -8,6 +8,7 @@ mod m20231103_114510_notes; mod m20241016_181828_servers; mod m20241021_121449_bundles; mod m20241021_121806_services; +mod m20241021_124238_bundles_services; pub struct Migrator; #[async_trait::async_trait] @@ -19,6 +20,7 @@ impl MigratorTrait for Migrator { Box::new(m20241016_181828_servers::Migration), Box::new(m20241021_121449_bundles::Migration), Box::new(m20241021_121806_services::Migration), + Box::new(m20241021_124238_bundles_services::Migration), ] } } \ No newline at end of file diff --git a/nixin_farm_ssr/migration/src/m20241021_124238_bundles_services.rs b/nixin_farm_ssr/migration/src/m20241021_124238_bundles_services.rs new file mode 100644 index 0000000..ca563ca --- /dev/null +++ b/nixin_farm_ssr/migration/src/m20241021_124238_bundles_services.rs @@ -0,0 +1,71 @@ +use loco_rs::schema::table_auto_tz; +use sea_orm_migration::{prelude::*, schema::*}; + +#[derive(DeriveMigrationName)] +pub struct Migration; + +#[async_trait::async_trait] +impl MigrationTrait for Migration { + async fn up(&self, manager: &SchemaManager) -> Result<(), DbErr> { + manager + .create_table( + table_auto_tz(BundlesServices::Table) + .primary_key( + Index::create() + .name("idx-bundles_services-refs-pk") + .table(BundlesServices::Table) + .col(BundlesServices::BundleId) + .col(BundlesServices::ServiceId) + , + ) + .col(integer(BundlesServices::BundleId)) + .col(integer(BundlesServices::ServiceId)) + .col(boolean_null(BundlesServices::Optional)) + .foreign_key( + ForeignKey::create() + .name("fk-bundles_services-bundles") + .from(BundlesServices::Table, BundlesServices::BundleId) + .to(Bundles::Table, Bundles::Id) + .on_delete(ForeignKeyAction::Cascade) + .on_update(ForeignKeyAction::Cascade), + ) + .foreign_key( + ForeignKey::create() + .name("fk-bundles_services-services") + .from(BundlesServices::Table, BundlesServices::ServiceId) + .to(Services::Table, Services::Id) + .on_delete(ForeignKeyAction::Cascade) + .on_update(ForeignKeyAction::Cascade), + ) + .to_owned(), + ) + .await + } + + async fn down(&self, manager: &SchemaManager) -> Result<(), DbErr> { + manager + .drop_table(Table::drop().table(BundlesServices::Table).to_owned()) + .await + } +} + +#[derive(DeriveIden)] +enum BundlesServices { + Table, + BundleId, + ServiceId, + Optional, + +} + + +#[derive(DeriveIden)] +enum Bundles { + Table, + Id, +} +#[derive(DeriveIden)] +enum Services { + Table, + Id, +} diff --git a/nixin_farm_ssr/src/models/_entities/bundles.rs b/nixin_farm_ssr/src/models/_entities/bundles.rs index 8829621..e3842a5 100644 --- a/nixin_farm_ssr/src/models/_entities/bundles.rs +++ b/nixin_farm_ssr/src/models/_entities/bundles.rs @@ -17,4 +17,22 @@ pub struct Model { } #[derive(Copy, Clone, Debug, EnumIter, DeriveRelation)] -pub enum Relation {} +pub enum Relation { + #[sea_orm(has_many = "super::bundles_services::Entity")] + BundlesServices, +} + +impl Related for Entity { + fn to() -> RelationDef { + Relation::BundlesServices.def() + } +} + +impl Related for Entity { + fn to() -> RelationDef { + super::bundles_services::Relation::Services.def() + } + fn via() -> Option { + Some(super::bundles_services::Relation::Bundles.def().rev()) + } +} diff --git a/nixin_farm_ssr/src/models/_entities/bundles_services.rs b/nixin_farm_ssr/src/models/_entities/bundles_services.rs new file mode 100644 index 0000000..1a85e09 --- /dev/null +++ b/nixin_farm_ssr/src/models/_entities/bundles_services.rs @@ -0,0 +1,48 @@ +//! `SeaORM` Entity, @generated by sea-orm-codegen 1.1.0 + +use sea_orm::entity::prelude::*; +use serde::{Deserialize, Serialize}; + +#[derive(Clone, Debug, PartialEq, DeriveEntityModel, Eq, Serialize, Deserialize)] +#[sea_orm(table_name = "bundles_services")] +pub struct Model { + pub created_at: DateTimeWithTimeZone, + pub updated_at: DateTimeWithTimeZone, + #[sea_orm(primary_key, auto_increment = false)] + pub bundle_id: i32, + #[sea_orm(primary_key, auto_increment = false)] + pub service_id: i32, + pub optional: Option, +} + +#[derive(Copy, Clone, Debug, EnumIter, DeriveRelation)] +pub enum Relation { + #[sea_orm( + belongs_to = "super::bundles::Entity", + from = "Column::BundleId", + to = "super::bundles::Column::Id", + on_update = "Cascade", + on_delete = "Cascade" + )] + Bundles, + #[sea_orm( + belongs_to = "super::services::Entity", + from = "Column::ServiceId", + to = "super::services::Column::Id", + on_update = "Cascade", + on_delete = "Cascade" + )] + Services, +} + +impl Related for Entity { + fn to() -> RelationDef { + Relation::Bundles.def() + } +} + +impl Related for Entity { + fn to() -> RelationDef { + Relation::Services.def() + } +} diff --git a/nixin_farm_ssr/src/models/_entities/mod.rs b/nixin_farm_ssr/src/models/_entities/mod.rs index 61b59ff..94770ef 100644 --- a/nixin_farm_ssr/src/models/_entities/mod.rs +++ b/nixin_farm_ssr/src/models/_entities/mod.rs @@ -3,6 +3,7 @@ pub mod prelude; pub mod bundles; +pub mod bundles_services; pub mod notes; pub mod servers; pub mod services; diff --git a/nixin_farm_ssr/src/models/_entities/prelude.rs b/nixin_farm_ssr/src/models/_entities/prelude.rs index d605e42..b3143e7 100644 --- a/nixin_farm_ssr/src/models/_entities/prelude.rs +++ b/nixin_farm_ssr/src/models/_entities/prelude.rs @@ -1,6 +1,7 @@ //! `SeaORM` Entity, @generated by sea-orm-codegen 1.1.0 pub use super::bundles::Entity as Bundles; +pub use super::bundles_services::Entity as BundlesServices; pub use super::notes::Entity as Notes; pub use super::servers::Entity as Servers; pub use super::services::Entity as Services; diff --git a/nixin_farm_ssr/src/models/_entities/services.rs b/nixin_farm_ssr/src/models/_entities/services.rs index 64c1b27..d355b07 100644 --- a/nixin_farm_ssr/src/models/_entities/services.rs +++ b/nixin_farm_ssr/src/models/_entities/services.rs @@ -19,4 +19,22 @@ pub struct Model { } #[derive(Copy, Clone, Debug, EnumIter, DeriveRelation)] -pub enum Relation {} +pub enum Relation { + #[sea_orm(has_many = "super::bundles_services::Entity")] + BundlesServices, +} + +impl Related for Entity { + fn to() -> RelationDef { + Relation::BundlesServices.def() + } +} + +impl Related for Entity { + fn to() -> RelationDef { + super::bundles_services::Relation::Bundles.def() + } + fn via() -> Option { + Some(super::bundles_services::Relation::Services.def().rev()) + } +} diff --git a/nixin_farm_ssr/src/models/bundles_services.rs b/nixin_farm_ssr/src/models/bundles_services.rs new file mode 100644 index 0000000..4095d76 --- /dev/null +++ b/nixin_farm_ssr/src/models/bundles_services.rs @@ -0,0 +1,7 @@ +use sea_orm::entity::prelude::*; +use super::_entities::bundles_services::{ActiveModel, Entity}; +pub type BundlesServices = Entity; + +impl ActiveModelBehavior for ActiveModel { + // extend activemodel below (keep comment for generators) +} diff --git a/nixin_farm_ssr/src/models/mod.rs b/nixin_farm_ssr/src/models/mod.rs index e2fdcc6..476b1fc 100644 --- a/nixin_farm_ssr/src/models/mod.rs +++ b/nixin_farm_ssr/src/models/mod.rs @@ -4,3 +4,4 @@ pub mod users; pub mod servers; pub mod bundles; pub mod services; +pub mod bundles_services; diff --git a/nixin_farm_ssr/tests/models/bundles_services.rs b/nixin_farm_ssr/tests/models/bundles_services.rs new file mode 100644 index 0000000..66cdb82 --- /dev/null +++ b/nixin_farm_ssr/tests/models/bundles_services.rs @@ -0,0 +1,31 @@ +use nixin_farm_ssr::app::App; +use loco_rs::testing; +use serial_test::serial; + +macro_rules! configure_insta { + ($($expr:expr),*) => { + let mut settings = insta::Settings::clone_current(); + settings.set_prepend_module_to_snapshot(false); + let _guard = settings.bind_to_scope(); + }; +} + +#[tokio::test] +#[serial] +async fn test_model() { + configure_insta!(); + + let boot = testing::boot_test::().await.unwrap(); + testing::seed::(&boot.app_context.db).await.unwrap(); + + // query your model, e.g.: + // + // let item = models::posts::Model::find_by_pid( + // &boot.app_context.db, + // "11111111-1111-1111-1111-111111111111", + // ) + // .await; + + // snapshot the result: + // assert_debug_snapshot!(item); +} diff --git a/nixin_farm_ssr/tests/models/mod.rs b/nixin_farm_ssr/tests/models/mod.rs index 3c18f56..1302ed4 100644 --- a/nixin_farm_ssr/tests/models/mod.rs +++ b/nixin_farm_ssr/tests/models/mod.rs @@ -2,4 +2,5 @@ mod users; mod servers; mod bundles; -mod services; \ No newline at end of file +mod services; +mod bundles_services; \ No newline at end of file