add services and tasks

This commit is contained in:
OMGeeky
2024-10-11 23:20:33 +02:00
parent 7717556feb
commit d2e5b26673
7 changed files with 154 additions and 1 deletions

View File

@@ -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;

View File

@@ -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,
};

23
src/entities/services.rs Normal file
View File

@@ -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<String>,
}
#[derive(Copy, Clone, Debug, EnumIter, DeriveRelation)]
pub enum Relation {
#[sea_orm(has_many = "super::tasks::Entity")]
Tasks,
}
impl Related<super::tasks::Entity> for Entity {
fn to() -> RelationDef {
Relation::Tasks.def()
}
}
impl ActiveModelBehavior for ActiveModel {}

29
src/entities/tasks.rs Normal file
View File

@@ -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<String>,
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<super::services::Entity> for Entity {
fn to() -> RelationDef {
Relation::Services.def()
}
}
impl ActiveModelBehavior for ActiveModel {}

View File

@@ -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(())
}

View File

@@ -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,
}

View File

@@ -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),
]
}
}