diff --git a/src/entities/mod.rs b/src/entities/mod.rs index d5cb732..831dbab 100644 --- a/src/entities/mod.rs +++ b/src/entities/mod.rs @@ -2,7 +2,9 @@ pub mod prelude; +pub mod services; pub mod status; +pub mod tasks; pub mod users; pub mod video_upload; pub mod videos; diff --git a/src/entities/prelude.rs b/src/entities/prelude.rs index bcbf8e4..2270b50 100644 --- a/src/entities/prelude.rs +++ b/src/entities/prelude.rs @@ -11,3 +11,12 @@ pub use super::video_upload::{ pub use super::videos::{ Column as VideosColumn, Entity as Videos, Model as VideosModel, Relation as VideosRelation, }; + +pub use super::services::{ + Column as ServicesColumn, Entity as Services, Model as ServicesModel, + Relation as ServicesRelation, +}; + +pub use super::tasks::{ + Column as TasksColumn, Entity as Tasks, Model as TasksModel, Relation as TasksRelation, +}; diff --git a/src/entities/services.rs b/src/entities/services.rs new file mode 100644 index 0000000..a180925 --- /dev/null +++ b/src/entities/services.rs @@ -0,0 +1,23 @@ +use sea_orm::entity::prelude::*; + +#[derive(Clone, Debug, PartialEq, DeriveEntityModel, Eq)] +#[sea_orm(table_name = "services")] +pub struct Model { + #[sea_orm(primary_key)] + pub id: i32, + pub name: String, + pub last_update: Option, +} + +#[derive(Copy, Clone, Debug, EnumIter, DeriveRelation)] +pub enum Relation { + #[sea_orm(has_many = "super::tasks::Entity")] + Tasks, +} +impl Related for Entity { + fn to() -> RelationDef { + Relation::Tasks.def() + } +} + +impl ActiveModelBehavior for ActiveModel {} diff --git a/src/entities/tasks.rs b/src/entities/tasks.rs new file mode 100644 index 0000000..cd96564 --- /dev/null +++ b/src/entities/tasks.rs @@ -0,0 +1,29 @@ +use sea_orm::entity::prelude::*; + +#[derive(Clone, Debug, PartialEq, DeriveEntityModel, Eq)] +#[sea_orm(table_name = "tasks")] +pub struct Model { + #[sea_orm(primary_key)] + pub id: i32, + pub service_id: i32, + pub description: Option, + pub progress: i32, + pub max_progress: i32, +} + +#[derive(Copy, Clone, Debug, EnumIter, DeriveRelation)] +pub enum Relation { + #[sea_orm( + belongs_to = "super::services::Entity", + from = "Column::ServiceId", + to = "super::services::Column::Id" + )] + Services, +} +impl Related for Entity { + fn to() -> RelationDef { + Relation::Services.def() + } +} + +impl ActiveModelBehavior for ActiveModel {} diff --git a/src/lib.rs b/src/lib.rs index 9c680af..b9081cc 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -6,7 +6,7 @@ use sea_orm::{ use sea_orm_migration::MigratorTrait; use tracing::{info, instrument}; use { - entities::prelude::{Users, VideoUpload, Videos}, + entities::prelude::{Services, Tasks, Users, VideoUpload, Videos}, entities::status::Status, entities::video_upload::UploadStatus, entities::*, @@ -62,6 +62,19 @@ pub async fn print_db(db: &DatabaseConnection) -> Result<(), DbErr> { } } + let services = Services::find().all(db).await?; + + info!("Tasks:"); + + for services in services.iter() { + info!("{:?}", services); + for tasks in services.find_related(Tasks).all(db).await.iter() { + for task in tasks.iter() { + info!("{:?}", &task); + } + } + } + info!("Done!"); Ok(()) } diff --git a/src/migrator/m20241011_000007_add_service_tables.rs b/src/migrator/m20241011_000007_add_service_tables.rs new file mode 100644 index 0000000..cb2d352 --- /dev/null +++ b/src/migrator/m20241011_000007_add_service_tables.rs @@ -0,0 +1,75 @@ +use sea_orm_migration::prelude::*; + +#[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::create() + .table(Services::Table) + .col( + ColumnDef::new(Services::Id) + .integer() + .not_null() + .auto_increment() + .primary_key(), + ) + .col(ColumnDef::new(Services::Name).string().not_null()) + .col(ColumnDef::new(Services::LastUpdate).date_time().null()) + .to_owned(), + ) + .await?; + manager + .create_table( + Table::create() + .table(Tasks::Table) + .col(ColumnDef::new(Tasks::Id).integer().not_null()) + .col(ColumnDef::new(Tasks::ServiceId).integer().not_null()) + .col(ColumnDef::new(Tasks::Description).string().null()) + .col(ColumnDef::new(Tasks::Progress).big_integer().default(0)) + .col(ColumnDef::new(Tasks::MaxProgress).big_integer().default(0)) + .primary_key(Index::create().col(Tasks::Id).col(Tasks::ServiceId)) + .foreign_key( + ForeignKey::create() + .name("FK_task_service") + .from(Tasks::Table, Tasks::ServiceId) + .to(Services::Table, Services::Id) + .on_delete(ForeignKeyAction::Cascade) + .on_update(ForeignKeyAction::Cascade), + ) + .to_owned(), + ) + .await?; + + Ok(()) + } + async fn down(&self, _manager: &SchemaManager) -> Result<(), DbErr> { + _manager + .drop_table(Table::drop().table(Services::Table).to_owned()) + .await?; + _manager + .drop_table(Table::drop().table(Tasks::Table).to_owned()) + .await + } +} + +#[derive(Iden)] +pub enum Services { + Table, + Id, + Name, + LastUpdate, +} + +#[derive(Iden)] +pub enum Tasks { + Table, + Id, + ServiceId, + Description, + Progress, + MaxProgress, +} diff --git a/src/migrator/mod.rs b/src/migrator/mod.rs index c1f1b06..f987489 100644 --- a/src/migrator/mod.rs +++ b/src/migrator/mod.rs @@ -7,6 +7,7 @@ mod m20230916_000004_add_sample_data; mod m20231015_000005_alter_videos_table_for_errors; mod m20231029_000006_alter_video_uploads_table_for_youtube_id; mod m20240509_000006_alter_users_table_for_timezone; +mod m20241011_000007_add_service_tables; pub struct Migrator; #[async_trait::async_trait] @@ -20,6 +21,7 @@ impl MigratorTrait for Migrator { Box::new(m20231015_000005_alter_videos_table_for_errors::Migration), Box::new(m20231029_000006_alter_video_uploads_table_for_youtube_id::Migration), Box::new(m20240509_000006_alter_users_table_for_timezone::Migration), + Box::new(m20241011_000007_add_service_tables::Migration), ] } }