From 72c579631187ca02829490cd781bb371830c3d85 Mon Sep 17 00:00:00 2001 From: trivernis Date: Fri, 1 Oct 2021 20:24:29 +0200 Subject: [PATCH] Add remaining entities with relations Signed-off-by: trivernis --- .../mediarepo-database/Cargo.lock | 1 + .../mediarepo-database/Cargo.toml | 1 + .../20210930182448_rename-type-column.sql | 1 + .../mediarepo-database/src/entities/file.rs | 40 +++++++++++++++++++ .../mediarepo-database/src/entities/hash.rs | 35 ++++++++++++++++ .../src/entities/hash_source.rs | 32 +++++++++++++++ .../src/entities/hash_tag.rs | 32 +++++++++++++++ .../mediarepo-database/src/entities/mod.rs | 13 +++--- .../mediarepo-database/src/entities/source.rs | 25 ++++++++++++ .../src/entities/storage.rs | 24 +++++++++++ .../mediarepo-database/src/entities/tag.rs | 6 ++- 11 files changed, 204 insertions(+), 6 deletions(-) create mode 100644 mediarepo-daemon/mediarepo-database/migrations/20210930182448_rename-type-column.sql create mode 100644 mediarepo-daemon/mediarepo-database/src/entities/file.rs create mode 100644 mediarepo-daemon/mediarepo-database/src/entities/hash.rs create mode 100644 mediarepo-daemon/mediarepo-database/src/entities/hash_source.rs create mode 100644 mediarepo-daemon/mediarepo-database/src/entities/hash_tag.rs create mode 100644 mediarepo-daemon/mediarepo-database/src/entities/source.rs create mode 100644 mediarepo-daemon/mediarepo-database/src/entities/storage.rs diff --git a/mediarepo-daemon/mediarepo-database/Cargo.lock b/mediarepo-daemon/mediarepo-database/Cargo.lock index b2f2aef..6b498f3 100644 --- a/mediarepo-daemon/mediarepo-database/Cargo.lock +++ b/mediarepo-daemon/mediarepo-database/Cargo.lock @@ -541,6 +541,7 @@ dependencies = [ name = "mediarepo-database" version = "0.1.0" dependencies = [ + "chrono", "mediarepo-core", "sea-orm", "sqlx", diff --git a/mediarepo-daemon/mediarepo-database/Cargo.toml b/mediarepo-daemon/mediarepo-database/Cargo.toml index 2c6925f..bf3b28f 100644 --- a/mediarepo-daemon/mediarepo-database/Cargo.toml +++ b/mediarepo-daemon/mediarepo-database/Cargo.toml @@ -6,6 +6,7 @@ edition = "2018" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html [dependencies] +chrono = "0.4.19" [dependencies.mediarepo-core] path = "../mediarepo-core" diff --git a/mediarepo-daemon/mediarepo-database/migrations/20210930182448_rename-type-column.sql b/mediarepo-daemon/mediarepo-database/migrations/20210930182448_rename-type-column.sql new file mode 100644 index 0000000..66b4a9c --- /dev/null +++ b/mediarepo-daemon/mediarepo-database/migrations/20210930182448_rename-type-column.sql @@ -0,0 +1 @@ +ALTER TABLE files RENAME COLUMN type TO file_type; \ No newline at end of file diff --git a/mediarepo-daemon/mediarepo-database/src/entities/file.rs b/mediarepo-daemon/mediarepo-database/src/entities/file.rs new file mode 100644 index 0000000..b02c0e3 --- /dev/null +++ b/mediarepo-daemon/mediarepo-database/src/entities/file.rs @@ -0,0 +1,40 @@ +use sea_orm::prelude::*; +use chrono::NaiveDateTime; + +#[derive(Clone, Debug, PartialEq, DeriveEntityModel)] +#[sea_orm(table_name = "files")] +pub struct Model { + #[sea_orm(primary_key)] + pub id: u64, + pub file_type: u32, + pub name: Option, + pub comment: Option, + pub storage_id: u64, + pub hash_id: u64, + pub import_time: NaiveDateTime, + pub creation_time: NaiveDateTime, + pub change_time: NaiveDateTime, +} + +#[derive(Copy, Clone, Debug, EnumIter, DeriveRelation)] +pub enum Relation { + #[sea_orm(belongs_to = "super::hash::Entity", from = "Column::HashId", to = "super::hash::Column::Id")] + Hash, + + #[sea_orm(belongs_to = "super::storage::Entity", from = "Column::StorageId", to = "super::storage::Column::Id")] + Storage, +} + +impl Related for Entity { + fn to() -> RelationDef { + Relation::Hash.def() + } +} + +impl Related for Entity { + fn to() -> RelationDef { + Relation::Storage.def() + } +} + +impl ActiveModelBehavior for ActiveModel {} \ No newline at end of file diff --git a/mediarepo-daemon/mediarepo-database/src/entities/hash.rs b/mediarepo-daemon/mediarepo-database/src/entities/hash.rs new file mode 100644 index 0000000..f091d87 --- /dev/null +++ b/mediarepo-daemon/mediarepo-database/src/entities/hash.rs @@ -0,0 +1,35 @@ +use sea_orm::prelude::*; + +#[derive(Clone, Debug, PartialEq, DeriveEntityModel)] +#[sea_orm(table_name = "hashes")] +pub struct Model { + #[sea_orm(primary_key)] + pub id: u64, + pub value: String +} + +#[derive(Copy, Clone, Debug, EnumIter, DeriveRelation)] +pub enum Relation { +} + +impl Related for Entity { + fn to() -> RelationDef { + super::hash_tag::Relation::Tag.def() + } + + fn via() -> Option { + Some(super::hash_tag::Relation::Hash.def().rev()) + } +} + +impl Related for Entity { + fn to() -> RelationDef { + super::hash_source::Relation::Source.def() + } + + fn via() -> Option { + Some(super::hash_source::Relation::Hash.def().rev()) + } +} + +impl ActiveModelBehavior for ActiveModel {} \ No newline at end of file diff --git a/mediarepo-daemon/mediarepo-database/src/entities/hash_source.rs b/mediarepo-daemon/mediarepo-database/src/entities/hash_source.rs new file mode 100644 index 0000000..c54b7da --- /dev/null +++ b/mediarepo-daemon/mediarepo-database/src/entities/hash_source.rs @@ -0,0 +1,32 @@ +use sea_orm::prelude::*; + +#[derive(Clone, Debug, PartialEq, DeriveEntityModel)] +#[sea_orm(table_name = "hash_source_mappings")] +pub struct Model { + #[sea_orm(primary_key)] + pub hash_id: u64, + #[sea_orm(primary_key)] + pub source_id: u64, +} + +#[derive(Copy, Clone, Debug, EnumIter, DeriveRelation)] +pub enum Relation { + #[sea_orm(belongs_to = "super::hash::Entity", from = "Column::HashId", to = "super::hash::Column::Id")] + Hash, + #[sea_orm(belongs_to = "super::source::Entity", from = "Column::SourceId", to = "super::source::Column::Id")] + Source, +} + +impl Related for Entity { + fn to() -> RelationDef { + Relation::Hash.def() + } +} + +impl Related for Entity { + fn to() -> RelationDef { + Relation::Source.def() + } +} + +impl ActiveModelBehavior for ActiveModel {} \ No newline at end of file diff --git a/mediarepo-daemon/mediarepo-database/src/entities/hash_tag.rs b/mediarepo-daemon/mediarepo-database/src/entities/hash_tag.rs new file mode 100644 index 0000000..1bb2317 --- /dev/null +++ b/mediarepo-daemon/mediarepo-database/src/entities/hash_tag.rs @@ -0,0 +1,32 @@ +use sea_orm::prelude::*; + +#[derive(Clone, Debug, PartialEq, DeriveEntityModel)] +#[sea_orm(table_name = "hash_tag_mappings")] +pub struct Model { + #[sea_orm(primary_key)] + pub hash_id: u64, + #[sea_orm(primary_key)] + pub tag_id: u64, +} + +#[derive(Copy, Clone, Debug, EnumIter, DeriveRelation)] +pub enum Relation { + #[sea_orm(belongs_to = "super::hash::Entity", from = "Column::HashId", to = "super::hash::Column::Id")] + Hash, + #[sea_orm(belongs_to = "super::tag::Entity", from = "Column::TagId", to = "super::tag::Column::Id")] + Tag, +} + +impl Related for Entity { + fn to() -> RelationDef { + Relation::Hash.def() + } +} + +impl Related for Entity { + fn to() -> RelationDef { + Relation::Tag.def() + } +} + +impl ActiveModelBehavior for ActiveModel {} \ No newline at end of file diff --git a/mediarepo-daemon/mediarepo-database/src/entities/mod.rs b/mediarepo-daemon/mediarepo-database/src/entities/mod.rs index d2f1357..d6c3b7c 100644 --- a/mediarepo-daemon/mediarepo-database/src/entities/mod.rs +++ b/mediarepo-daemon/mediarepo-database/src/entities/mod.rs @@ -1,5 +1,8 @@ -mod tag; -mod namespace; - -pub use tag::*; -pub use namespace::*; \ No newline at end of file +pub mod tag; +pub mod namespace; +pub mod hash; +pub mod source; +pub mod file; +pub mod hash_tag; +pub mod storage; +pub mod hash_source; \ No newline at end of file diff --git a/mediarepo-daemon/mediarepo-database/src/entities/source.rs b/mediarepo-daemon/mediarepo-database/src/entities/source.rs new file mode 100644 index 0000000..f4adcbe --- /dev/null +++ b/mediarepo-daemon/mediarepo-database/src/entities/source.rs @@ -0,0 +1,25 @@ +use sea_orm::prelude::*; + +#[derive(Clone, Debug, PartialEq, DeriveEntityModel)] +#[sea_orm(table_name = "sources")] +pub struct Model { + #[sea_orm(primary_key)] + pub id: u64, + pub url: String +} + +#[derive(Copy, Clone, Debug, EnumIter, DeriveRelation)] +pub enum Relation { +} + +impl Related for Entity { + fn to() -> RelationDef { + super::hash_source::Relation::Hash.def() + } + + fn via() -> Option { + Some(super::hash_source::Relation::Source.def().rev()) + } +} + +impl ActiveModelBehavior for ActiveModel {} \ No newline at end of file diff --git a/mediarepo-daemon/mediarepo-database/src/entities/storage.rs b/mediarepo-daemon/mediarepo-database/src/entities/storage.rs new file mode 100644 index 0000000..8f8bbac --- /dev/null +++ b/mediarepo-daemon/mediarepo-database/src/entities/storage.rs @@ -0,0 +1,24 @@ +use sea_orm::prelude::*; + +#[derive(Clone, Debug, PartialEq, DeriveEntityModel)] +#[sea_orm(table_name = "storages")] +pub struct Model { + #[sea_orm(primary_key)] + pub id: u64, + pub name: String, + pub path: String +} + +#[derive(Copy, Clone, Debug, EnumIter, DeriveRelation)] +pub enum Relation { + #[sea_orm(has_many = "super::file::Entity")] + File, +} + +impl Related for Entity { + fn to() -> RelationDef { + Relation::File.def() + } +} + +impl ActiveModelBehavior for ActiveModel {} \ No newline at end of file diff --git a/mediarepo-daemon/mediarepo-database/src/entities/tag.rs b/mediarepo-daemon/mediarepo-database/src/entities/tag.rs index 756a965..eba6841 100644 --- a/mediarepo-daemon/mediarepo-database/src/entities/tag.rs +++ b/mediarepo-daemon/mediarepo-database/src/entities/tag.rs @@ -17,7 +17,11 @@ pub enum Relation { impl Related for Entity { fn to() -> RelationDef { - Relation::Namespace.def() + super::hash_tag::Relation::Hash.def() + } + + fn via() -> Option { + Some(super::hash_tag::Relation::Tag.def().rev()) } }