From cab349bafb3bb6ec2e55fe6cf2f1681232de7b2e Mon Sep 17 00:00:00 2001 From: trivernis Date: Sun, 15 May 2022 11:21:30 +0200 Subject: [PATCH] Add remaining migration statements Signed-off-by: trivernis --- .../mediarepo-database/migration/src/lib.rs | 1 + .../src/m20220101_000001_create_table.rs | 197 +++++++++++++++++- .../mediarepo-database/migration/src/utils.rs | 9 + 3 files changed, 197 insertions(+), 10 deletions(-) create mode 100644 mediarepo-daemon/mediarepo-database/migration/src/utils.rs diff --git a/mediarepo-daemon/mediarepo-database/migration/src/lib.rs b/mediarepo-daemon/mediarepo-database/migration/src/lib.rs index 3cabe22..7175f94 100644 --- a/mediarepo-daemon/mediarepo-database/migration/src/lib.rs +++ b/mediarepo-daemon/mediarepo-database/migration/src/lib.rs @@ -1,6 +1,7 @@ pub use sea_schema::migration::prelude::*; mod m20220101_000001_create_table; +mod utils; pub struct Migrator; diff --git a/mediarepo-daemon/mediarepo-database/migration/src/m20220101_000001_create_table.rs b/mediarepo-daemon/mediarepo-database/migration/src/m20220101_000001_create_table.rs index f9ae846..ff36bf3 100644 --- a/mediarepo-daemon/mediarepo-database/migration/src/m20220101_000001_create_table.rs +++ b/mediarepo-daemon/mediarepo-database/migration/src/m20220101_000001_create_table.rs @@ -1,5 +1,4 @@ -use crate::async_std::task::Task; -use crate::sea_orm::tests_cfg::cake_filling::PrimaryKey; +use crate::drop_tables; use sea_schema::migration::prelude::*; pub struct Migration; @@ -19,12 +18,34 @@ impl MigrationTrait for Migration { manager.create_table(create_namespaces()).await?; manager.create_table(create_tags()).await?; manager.create_table(create_cd_tag_mappings()).await?; + manager.create_table(create_sources()).await?; + manager.create_table(create_cd_sources_mappings()).await?; + manager.create_table(create_sorting_presets()).await?; + manager.create_table(create_sort_keys()).await?; + manager.create_table(create_sorting_preset_key()).await?; + manager.create_table(create_job_states()).await?; Ok(()) } async fn down(&self, manager: &SchemaManager) -> Result<(), DbErr> { - todo!() + drop_tables!( + manager, + ContentDescriptors::Table, + Files::Table, + FileMetadata::Table, + Namespaces::Table, + Tags::Table, + CdTagMappings::Table, + Sources::Table, + CdSourceMappings::Table, + SortingPresets::Table, + SortKeys::Table, + SortingPresetKeys::Table, + JobStates::Table + ); + + Ok(()) } } @@ -59,7 +80,8 @@ fn create_file_metadata() -> TableCreateStatement { .foreign_key( ForeignKey::create() .from(FileMetadata::Table, FileMetadata::FileId) - .to(Files::Table, Files::Id), + .to(Files::Table, Files::Id) + .on_delete(ForeignKeyAction::Cascade), ) .to_owned() } @@ -90,7 +112,8 @@ fn create_files() -> TableCreateStatement { .foreign_key( ForeignKey::create() .from(Files::Table, Files::CdId) - .to(ContentDescriptors::Table, ContentDescriptors::Id), + .to(ContentDescriptors::Table, ContentDescriptors::Id) + .on_delete(ForeignKeyAction::Cascade), ) .index(Index::create().table(Files::Table).col(Files::CdId)) .to_owned() @@ -166,12 +189,14 @@ fn create_cd_tag_mappings() -> TableCreateStatement { .foreign_key( ForeignKey::create() .from(CdTagMappings::Table, CdTagMappings::CdId) - .to(ContentDescriptors::Table, ContentDescriptors::Id), + .to(ContentDescriptors::Table, ContentDescriptors::Id) + .on_delete(ForeignKeyAction::Cascade), ) .foreign_key( ForeignKey::create() .from(CdTagMappings::Table, CdTagMappings::TagId) - .to(Tags::Table, Tags::Id), + .to(Tags::Table, Tags::Id) + .on_delete(ForeignKeyAction::Cascade), ) .index( Index::create() @@ -202,6 +227,157 @@ fn create_namespaces() -> TableCreateStatement { .to_owned() } +fn create_sources() -> TableCreateStatement { + Table::create() + .if_not_exists() + .table(Sources::Table) + .col( + ColumnDef::new(Sources::Id) + .big_integer() + .primary_key() + .auto_increment(), + ) + .col(ColumnDef::new(Sources::Url).string_len(512).not_null()) + .index( + Index::create() + .unique() + .table(Sources::Table) + .col(Sources::Url) + .full_text(), + ) + .to_owned() +} + +fn create_cd_sources_mappings() -> TableCreateStatement { + Table::create() + .if_not_exists() + .table(CdSourceMappings::Table) + .col( + ColumnDef::new(CdSourceMappings::CdId) + .big_integer() + .not_null(), + ) + .col( + ColumnDef::new(CdSourceMappings::SourceId) + .big_integer() + .not_null(), + ) + .foreign_key( + ForeignKey::create() + .from(CdSourceMappings::Table, CdSourceMappings::CdId) + .to(ContentDescriptors::Table, ContentDescriptors::Id) + .on_delete(ForeignKeyAction::Cascade), + ) + .foreign_key( + ForeignKey::create() + .from(CdSourceMappings::Table, CdSourceMappings::SourceId) + .to(Sources::Table, Sources::Id) + .on_delete(ForeignKeyAction::Cascade), + ) + .primary_key( + Index::create() + .table(CdSourceMappings::Table) + .col(CdSourceMappings::CdId) + .col(CdSourceMappings::SourceId) + .full_text(), + ) + .to_owned() +} + +fn create_sorting_presets() -> TableCreateStatement { + Table::create() + .if_not_exists() + .table(SortingPresets::Table) + .col( + ColumnDef::new(SortingPresets::Id) + .big_integer() + .primary_key() + .auto_increment(), + ) + .to_owned() +} + +fn create_sort_keys() -> TableCreateStatement { + Table::create() + .if_not_exists() + .table(SortKeys::Table) + .col( + ColumnDef::new(SortKeys::Id) + .big_integer() + .primary_key() + .auto_increment(), + ) + .col( + ColumnDef::new(SortKeys::KeyType) + .integer() + .not_null() + .default(0), + ) + .col( + ColumnDef::new(SortKeys::Ascending) + .boolean() + .not_null() + .default(false), + ) + .col(ColumnDef::new(SortKeys::Value).string_len(128)) + .to_owned() +} + +fn create_sorting_preset_key() -> TableCreateStatement { + Table::create() + .if_not_exists() + .table(SortingPresetKeys::Table) + .col( + ColumnDef::new(SortingPresetKeys::PresetId) + .big_integer() + .not_null(), + ) + .col( + ColumnDef::new(SortingPresetKeys::KeyId) + .big_integer() + .not_null(), + ) + .col( + ColumnDef::new(SortingPresetKeys::KeyIndex) + .integer() + .default(0) + .not_null(), + ) + .primary_key( + Index::create() + .table(SortingPresetKeys::Table) + .col(SortingPresetKeys::PresetId) + .col(SortingPresetKeys::KeyId), + ) + .foreign_key( + ForeignKey::create() + .from(SortingPresetKeys::Table, SortingPresetKeys::PresetId) + .to(SortingPresets::Table, SortingPresets::Id) + .on_delete(ForeignKeyAction::Cascade), + ) + .foreign_key( + ForeignKey::create() + .from(SortingPresetKeys::Table, SortingPresetKeys::KeyId) + .to(SortKeys::Table, SortKeys::Id) + .on_delete(ForeignKeyAction::Cascade), + ) + .to_owned() +} + +fn create_job_states() -> TableCreateStatement { + Table::create() + .if_not_exists() + .table(JobStates::Table) + .col( + ColumnDef::new(JobStates::JobType) + .integer() + .primary_key() + .not_null(), + ) + .col(ColumnDef::new(JobStates::Value).binary()) + .to_owned() +} + #[derive(Iden)] enum FileMetadata { Table, @@ -260,14 +436,14 @@ enum Sources { } #[derive(Iden)] -enum CdSourceMapping { +enum CdSourceMappings { Table, CdId, SourceId, } #[derive(Iden)] -enum SortingPreset { +enum SortingPresets { Table, Id, } @@ -275,13 +451,14 @@ enum SortingPreset { #[derive(Iden)] enum SortKeys { Table, + Id, KeyType, Ascending, Value, } #[derive(Iden)] -enum SortingPresetKey { +enum SortingPresetKeys { Table, PresetId, KeyId, diff --git a/mediarepo-daemon/mediarepo-database/migration/src/utils.rs b/mediarepo-daemon/mediarepo-database/migration/src/utils.rs new file mode 100644 index 0000000..a72d814 --- /dev/null +++ b/mediarepo-daemon/mediarepo-database/migration/src/utils.rs @@ -0,0 +1,9 @@ +#[macro_export] +macro_rules! drop_tables { + ($man:expr, $($tbl:expr),*) => { + use sea_schema::migration::prelude::*; + $( + $man.drop_table(TableDropStatement::new().table($tbl).to_owned()).await?; + )* + } +}