From 9963cff724c39fd92ddfb82b0992ee6cde46cdc9 Mon Sep 17 00:00:00 2001 From: Chewing_Bever Date: Sat, 18 May 2024 14:20:05 +0200 Subject: [PATCH] feat: simplify database scheme --- server/src/db/entities/mod.rs | 5 +- server/src/db/entities/package.rs | 34 +--- server/src/db/entities/package_conflicts.rs | 33 ---- server/src/db/entities/package_file.rs | 2 +- server/src/db/entities/package_group.rs | 2 +- server/src/db/entities/package_license.rs | 2 +- server/src/db/entities/package_provides.rs | 33 ---- ...{package_depends.rs => package_related.rs} | 6 +- server/src/db/entities/package_replaces.rs | 33 ---- server/src/db/entities/prelude.rs | 5 +- server/src/db/entities/repo.rs | 1 - .../m20230730_000001_create_repo_tables.rs | 165 +++--------------- server/src/db/mod.rs | 28 +-- server/src/db/query/package.rs | 115 +++++------- server/src/repo/package.rs | 2 +- 15 files changed, 96 insertions(+), 370 deletions(-) delete mode 100644 server/src/db/entities/package_conflicts.rs delete mode 100644 server/src/db/entities/package_provides.rs rename server/src/db/entities/{package_depends.rs => package_related.rs} (88%) delete mode 100644 server/src/db/entities/package_replaces.rs diff --git a/server/src/db/entities/mod.rs b/server/src/db/entities/mod.rs index 1111e7a..ab8f32a 100644 --- a/server/src/db/entities/mod.rs +++ b/server/src/db/entities/mod.rs @@ -3,11 +3,8 @@ pub mod prelude; pub mod package; -pub mod package_conflicts; -pub mod package_depends; pub mod package_file; pub mod package_group; pub mod package_license; -pub mod package_provides; -pub mod package_replaces; +pub mod package_related; pub mod repo; diff --git a/server/src/db/entities/package.rs b/server/src/db/entities/package.rs index b2e2b0b..8ea6797 100644 --- a/server/src/db/entities/package.rs +++ b/server/src/db/entities/package.rs @@ -17,7 +17,7 @@ pub struct Model { pub c_size: i64, pub description: Option, pub url: Option, - pub build_date: DateTime, + pub build_date: String, pub packager: Option, pub pgp_sig: Option, pub pgp_sig_size: Option, @@ -26,20 +26,14 @@ pub struct Model { #[derive(Copy, Clone, Debug, EnumIter, DeriveRelation)] pub enum Relation { - #[sea_orm(has_many = "super::package_conflicts::Entity")] - PackageConflicts, - #[sea_orm(has_many = "super::package_depends::Entity")] - PackageDepends, #[sea_orm(has_many = "super::package_file::Entity")] PackageFile, #[sea_orm(has_many = "super::package_group::Entity")] PackageGroup, #[sea_orm(has_many = "super::package_license::Entity")] PackageLicense, - #[sea_orm(has_many = "super::package_provides::Entity")] - PackageProvides, - #[sea_orm(has_many = "super::package_replaces::Entity")] - PackageReplaces, + #[sea_orm(has_many = "super::package_related::Entity")] + PackageRelated, #[sea_orm( belongs_to = "super::repo::Entity", from = "Column::RepoId", @@ -50,18 +44,6 @@ pub enum Relation { Repo, } -impl Related for Entity { - fn to() -> RelationDef { - Relation::PackageConflicts.def() - } -} - -impl Related for Entity { - fn to() -> RelationDef { - Relation::PackageDepends.def() - } -} - impl Related for Entity { fn to() -> RelationDef { Relation::PackageFile.def() @@ -80,15 +62,9 @@ impl Related for Entity { } } -impl Related for Entity { +impl Related for Entity { fn to() -> RelationDef { - Relation::PackageProvides.def() - } -} - -impl Related for Entity { - fn to() -> RelationDef { - Relation::PackageReplaces.def() + Relation::PackageRelated.def() } } diff --git a/server/src/db/entities/package_conflicts.rs b/server/src/db/entities/package_conflicts.rs deleted file mode 100644 index e9e8ba8..0000000 --- a/server/src/db/entities/package_conflicts.rs +++ /dev/null @@ -1,33 +0,0 @@ -//! `SeaORM` Entity. Generated by sea-orm-codegen 0.12.1 - -use sea_orm::entity::prelude::*; -use serde::{Deserialize, Serialize}; - -#[derive(Clone, Debug, PartialEq, DeriveEntityModel, Eq, Serialize, Deserialize)] -#[sea_orm(table_name = "package_conflicts")] -pub struct Model { - #[sea_orm(primary_key, auto_increment = false)] - pub package_id: i32, - #[sea_orm(primary_key, auto_increment = false)] - pub value: String, -} - -#[derive(Copy, Clone, Debug, EnumIter, DeriveRelation)] -pub enum Relation { - #[sea_orm( - belongs_to = "super::package::Entity", - from = "Column::PackageId", - to = "super::package::Column::Id", - on_update = "NoAction", - on_delete = "Cascade" - )] - Package, -} - -impl Related for Entity { - fn to() -> RelationDef { - Relation::Package.def() - } -} - -impl ActiveModelBehavior for ActiveModel {} diff --git a/server/src/db/entities/package_file.rs b/server/src/db/entities/package_file.rs index 6e994e0..8607cf8 100644 --- a/server/src/db/entities/package_file.rs +++ b/server/src/db/entities/package_file.rs @@ -9,7 +9,7 @@ pub struct Model { #[sea_orm(primary_key, auto_increment = false)] pub package_id: i32, #[sea_orm(primary_key, auto_increment = false)] - pub value: String, + pub path: String, } #[derive(Copy, Clone, Debug, EnumIter, DeriveRelation)] diff --git a/server/src/db/entities/package_group.rs b/server/src/db/entities/package_group.rs index 61e69f2..59948c3 100644 --- a/server/src/db/entities/package_group.rs +++ b/server/src/db/entities/package_group.rs @@ -9,7 +9,7 @@ pub struct Model { #[sea_orm(primary_key, auto_increment = false)] pub package_id: i32, #[sea_orm(primary_key, auto_increment = false)] - pub value: String, + pub name: String, } #[derive(Copy, Clone, Debug, EnumIter, DeriveRelation)] diff --git a/server/src/db/entities/package_license.rs b/server/src/db/entities/package_license.rs index 2920d3f..c4e0d71 100644 --- a/server/src/db/entities/package_license.rs +++ b/server/src/db/entities/package_license.rs @@ -9,7 +9,7 @@ pub struct Model { #[sea_orm(primary_key, auto_increment = false)] pub package_id: i32, #[sea_orm(primary_key, auto_increment = false)] - pub value: String, + pub name: String, } #[derive(Copy, Clone, Debug, EnumIter, DeriveRelation)] diff --git a/server/src/db/entities/package_provides.rs b/server/src/db/entities/package_provides.rs deleted file mode 100644 index 7fca6ee..0000000 --- a/server/src/db/entities/package_provides.rs +++ /dev/null @@ -1,33 +0,0 @@ -//! `SeaORM` Entity. Generated by sea-orm-codegen 0.12.1 - -use sea_orm::entity::prelude::*; -use serde::{Deserialize, Serialize}; - -#[derive(Clone, Debug, PartialEq, DeriveEntityModel, Eq, Serialize, Deserialize)] -#[sea_orm(table_name = "package_provides")] -pub struct Model { - #[sea_orm(primary_key, auto_increment = false)] - pub package_id: i32, - #[sea_orm(primary_key, auto_increment = false)] - pub value: String, -} - -#[derive(Copy, Clone, Debug, EnumIter, DeriveRelation)] -pub enum Relation { - #[sea_orm( - belongs_to = "super::package::Entity", - from = "Column::PackageId", - to = "super::package::Column::Id", - on_update = "NoAction", - on_delete = "Cascade" - )] - Package, -} - -impl Related for Entity { - fn to() -> RelationDef { - Relation::Package.def() - } -} - -impl ActiveModelBehavior for ActiveModel {} diff --git a/server/src/db/entities/package_depends.rs b/server/src/db/entities/package_related.rs similarity index 88% rename from server/src/db/entities/package_depends.rs rename to server/src/db/entities/package_related.rs index 7e94374..3e25ff3 100644 --- a/server/src/db/entities/package_depends.rs +++ b/server/src/db/entities/package_related.rs @@ -4,14 +4,14 @@ use sea_orm::entity::prelude::*; use serde::{Deserialize, Serialize}; #[derive(Clone, Debug, PartialEq, DeriveEntityModel, Eq, Serialize, Deserialize)] -#[sea_orm(table_name = "package_depends")] +#[sea_orm(table_name = "package_related")] pub struct Model { #[sea_orm(primary_key, auto_increment = false)] pub package_id: i32, #[sea_orm(primary_key, auto_increment = false)] - pub r#type: crate::db::PackageDepend, + pub r#type: crate::db::PackageRelatedEnum, #[sea_orm(primary_key, auto_increment = false)] - pub value: String, + pub name: String, } #[derive(Copy, Clone, Debug, EnumIter, DeriveRelation)] diff --git a/server/src/db/entities/package_replaces.rs b/server/src/db/entities/package_replaces.rs deleted file mode 100644 index 0946b2d..0000000 --- a/server/src/db/entities/package_replaces.rs +++ /dev/null @@ -1,33 +0,0 @@ -//! `SeaORM` Entity. Generated by sea-orm-codegen 0.12.1 - -use sea_orm::entity::prelude::*; -use serde::{Deserialize, Serialize}; - -#[derive(Clone, Debug, PartialEq, DeriveEntityModel, Eq, Serialize, Deserialize)] -#[sea_orm(table_name = "package_replaces")] -pub struct Model { - #[sea_orm(primary_key, auto_increment = false)] - pub package_id: i32, - #[sea_orm(primary_key, auto_increment = false)] - pub value: String, -} - -#[derive(Copy, Clone, Debug, EnumIter, DeriveRelation)] -pub enum Relation { - #[sea_orm( - belongs_to = "super::package::Entity", - from = "Column::PackageId", - to = "super::package::Column::Id", - on_update = "NoAction", - on_delete = "Cascade" - )] - Package, -} - -impl Related for Entity { - fn to() -> RelationDef { - Relation::Package.def() - } -} - -impl ActiveModelBehavior for ActiveModel {} diff --git a/server/src/db/entities/prelude.rs b/server/src/db/entities/prelude.rs index bee503c..1a6e503 100644 --- a/server/src/db/entities/prelude.rs +++ b/server/src/db/entities/prelude.rs @@ -1,11 +1,8 @@ //! `SeaORM` Entity. Generated by sea-orm-codegen 0.12.1 pub use super::package::Entity as Package; -pub use super::package_conflicts::Entity as PackageConflicts; -pub use super::package_depends::Entity as PackageDepends; pub use super::package_file::Entity as PackageFile; pub use super::package_group::Entity as PackageGroup; pub use super::package_license::Entity as PackageLicense; -pub use super::package_provides::Entity as PackageProvides; -pub use super::package_replaces::Entity as PackageReplaces; +pub use super::package_related::Entity as PackageRelated; pub use super::repo::Entity as Repo; diff --git a/server/src/db/entities/repo.rs b/server/src/db/entities/repo.rs index b7a1af1..25291da 100644 --- a/server/src/db/entities/repo.rs +++ b/server/src/db/entities/repo.rs @@ -8,7 +8,6 @@ use serde::{Deserialize, Serialize}; pub struct Model { #[sea_orm(primary_key)] pub id: i32, - #[sea_orm(unique)] pub name: String, pub description: Option, } diff --git a/server/src/db/migrator/m20230730_000001_create_repo_tables.rs b/server/src/db/migrator/m20230730_000001_create_repo_tables.rs index adefe56..866319e 100644 --- a/server/src/db/migrator/m20230730_000001_create_repo_tables.rs +++ b/server/src/db/migrator/m20230730_000001_create_repo_tables.rs @@ -72,14 +72,14 @@ impl MigrationTrait for Migration { .not_null(), ) .col( - ColumnDef::new(PackageLicense::Value) + ColumnDef::new(PackageLicense::Name) .string_len(255) .not_null(), ) .primary_key( Index::create() .col(PackageLicense::PackageId) - .col(PackageLicense::Value), + .col(PackageLicense::Name), ) .foreign_key( ForeignKey::create() @@ -97,14 +97,14 @@ impl MigrationTrait for Migration { .table(PackageGroup::Table) .col(ColumnDef::new(PackageGroup::PackageId).integer().not_null()) .col( - ColumnDef::new(PackageGroup::Value) + ColumnDef::new(PackageGroup::Name) .string_len(255) .not_null(), ) .primary_key( Index::create() .col(PackageGroup::PackageId) - .col(PackageGroup::Value), + .col(PackageGroup::Name), ) .foreign_key( ForeignKey::create() @@ -119,119 +119,28 @@ impl MigrationTrait for Migration { manager .create_table( Table::create() - .table(PackageReplaces::Table) + .table(PackageRelated::Table) .col( - ColumnDef::new(PackageReplaces::PackageId) + ColumnDef::new(PackageRelated::PackageId) .integer() .not_null(), ) + .col(ColumnDef::new(PackageRelated::Type).integer().not_null()) .col( - ColumnDef::new(PackageReplaces::Value) + ColumnDef::new(PackageRelated::Name) .string_len(255) .not_null(), ) .primary_key( Index::create() - .col(PackageReplaces::PackageId) - .col(PackageReplaces::Value), - ) - .foreign_key( - ForeignKey::create() - .name("fk-package_replaces-package_id") - .from(PackageReplaces::Table, PackageReplaces::PackageId) - .to(Package::Table, Package::Id) - .on_delete(ForeignKeyAction::Cascade), - ) - .to_owned(), - ) - .await?; - manager - .create_table( - Table::create() - .table(PackageConflicts::Table) - .col( - ColumnDef::new(PackageConflicts::PackageId) - .integer() - .not_null(), - ) - .col( - ColumnDef::new(PackageConflicts::Value) - .string_len(255) - .not_null(), - ) - .primary_key( - Index::create() - .col(PackageConflicts::PackageId) - .col(PackageConflicts::Value), - ) - .foreign_key( - ForeignKey::create() - .name("fk-package_conflicts-package_id") - .from(PackageConflicts::Table, PackageConflicts::PackageId) - .to(Package::Table, Package::Id) - .on_delete(ForeignKeyAction::Cascade), - ) - .to_owned(), - ) - .await?; - manager - .create_table( - Table::create() - .table(PackageProvides::Table) - .col( - ColumnDef::new(PackageProvides::PackageId) - .integer() - .not_null(), - ) - .col( - ColumnDef::new(PackageProvides::Value) - .string_len(255) - .not_null(), - ) - .primary_key( - Index::create() - .col(PackageProvides::PackageId) - .col(PackageProvides::Value), - ) - .foreign_key( - ForeignKey::create() - .name("fk-package_provides-package_id") - .from(PackageProvides::Table, PackageProvides::PackageId) - .to(Package::Table, Package::Id) - .on_delete(ForeignKeyAction::Cascade), - ) - .to_owned(), - ) - .await?; - manager - .create_table( - Table::create() - .table(PackageDepends::Table) - .col( - ColumnDef::new(PackageDepends::PackageId) - .integer() - .not_null(), - ) - .col( - ColumnDef::new(PackageDepends::Type) - .string_len(6) - .not_null(), - ) - .col( - ColumnDef::new(PackageDepends::Value) - .string_len(255) - .not_null(), - ) - .primary_key( - Index::create() - .col(PackageDepends::PackageId) - .col(PackageDepends::Type) - .col(PackageDepends::Value), + .col(PackageRelated::PackageId) + .col(PackageRelated::Type) + .col(PackageRelated::Name), ) .foreign_key( ForeignKey::create() .name("fk-package_depends-package_id") - .from(PackageDepends::Table, PackageDepends::PackageId) + .from(PackageRelated::Table, PackageRelated::PackageId) .to(Package::Table, Package::Id) .on_delete(ForeignKeyAction::Cascade), ) @@ -243,15 +152,11 @@ impl MigrationTrait for Migration { Table::create() .table(PackageFile::Table) .col(ColumnDef::new(PackageFile::PackageId).integer().not_null()) - .col( - ColumnDef::new(PackageFile::Value) - .string_len(255) - .not_null(), - ) + .col(ColumnDef::new(PackageFile::Path).string_len(255).not_null()) .primary_key( Index::create() .col(PackageFile::PackageId) - .col(PackageFile::Value), + .col(PackageFile::Path), ) .foreign_key( ForeignKey::create() @@ -276,16 +181,7 @@ impl MigrationTrait for Migration { .drop_table(Table::drop().table(PackageGroup::Table).to_owned()) .await?; manager - .drop_table(Table::drop().table(PackageReplaces::Table).to_owned()) - .await?; - manager - .drop_table(Table::drop().table(PackageConflicts::Table).to_owned()) - .await?; - manager - .drop_table(Table::drop().table(PackageProvides::Table).to_owned()) - .await?; - manager - .drop_table(Table::drop().table(PackageDepends::Table).to_owned()) + .drop_table(Table::drop().table(PackageRelated::Table).to_owned()) .await?; manager .drop_table(Table::drop().table(PackageFile::Table).to_owned()) @@ -331,48 +227,27 @@ pub enum Package { pub enum PackageLicense { Table, PackageId, - Value, + Name, } #[derive(Iden)] pub enum PackageGroup { Table, PackageId, - Value, + Name, } #[derive(Iden)] -pub enum PackageReplaces { - Table, - PackageId, - Value, -} - -#[derive(Iden)] -pub enum PackageConflicts { - Table, - PackageId, - Value, -} - -#[derive(Iden)] -pub enum PackageProvides { - Table, - PackageId, - Value, -} - -#[derive(Iden)] -pub enum PackageDepends { +pub enum PackageRelated { Table, PackageId, Type, - Value, + Name, } #[derive(Iden)] pub enum PackageFile { Table, PackageId, - Value, + Path, } diff --git a/server/src/db/mod.rs b/server/src/db/mod.rs index 6ff5a8f..587052d 100644 --- a/server/src/db/mod.rs +++ b/server/src/db/mod.rs @@ -13,16 +13,22 @@ use migrator::Migrator; type Result = std::result::Result; #[derive(EnumIter, DeriveActiveEnum, Serialize, Deserialize, PartialEq, Eq, Clone, Debug)] -#[sea_orm(rs_type = "String", db_type = "String(Some(6))")] -pub enum PackageDepend { - #[sea_orm(string_value = "depend")] +#[sea_orm(rs_type = "i32", db_type = "Integer")] +pub enum PackageRelatedEnum { + #[sea_orm(num_value = 0)] + Conflicts, + #[sea_orm(num_value = 1)] + Replaces, + #[sea_orm(num_value = 2)] + Provides, + #[sea_orm(num_value = 3)] Depend, - #[sea_orm(string_value = "make")] - Make, - #[sea_orm(string_value = "check")] - Check, - #[sea_orm(string_value = "opt")] - Opt, + #[sea_orm(num_value = 4)] + Makedepend, + #[sea_orm(num_value = 5)] + Checkdepend, + #[sea_orm(num_value = 6)] + Optdepend, } #[derive(Serialize)] @@ -31,9 +37,7 @@ pub struct FullPackage { entry: package::Model, licenses: Vec, groups: Vec, - replaces: Vec, - provides: Vec, - depends: Vec<(PackageDepend, String)>, + related: Vec<(PackageRelatedEnum, String)>, files: Vec, } diff --git a/server/src/db/query/package.rs b/server/src/db/query/package.rs index eed5ead..c977984 100644 --- a/server/src/db/query/package.rs +++ b/server/src/db/query/package.rs @@ -71,7 +71,7 @@ impl PackageQuery { c_size: Set(info.csize), description: Set(info.description), url: Set(info.url), - build_date: Set(info.build_date), + build_date: Set(info.build_date.to_string()), packager: Set(info.packager), pgp_sig: Set(info.pgpsig), pgp_sig_size: Set(info.pgpsigsize), @@ -83,7 +83,7 @@ impl PackageQuery { // Insert all the related tables PackageLicense::insert_many(info.licenses.iter().map(|s| package_license::ActiveModel { package_id: Set(pkg_entry.id), - value: Set(s.to_string()), + name: Set(s.to_string()), })) .on_empty_do_nothing() .exec(&self.conn) @@ -91,64 +91,57 @@ impl PackageQuery { PackageGroup::insert_many(info.groups.iter().map(|s| package_group::ActiveModel { package_id: Set(pkg_entry.id), - value: Set(s.to_string()), + name: Set(s.to_string()), })) .on_empty_do_nothing() .exec(&self.conn) .await?; - PackageReplaces::insert_many(info.replaces.iter().map(|s| package_replaces::ActiveModel { + let related = info + .conflicts + .iter() + .map(|s| (PackageRelatedEnum::Conflicts, s)) + .chain( + info.replaces + .iter() + .map(|s| (PackageRelatedEnum::Replaces, s)), + ) + .chain( + info.provides + .iter() + .map(|s| (PackageRelatedEnum::Provides, s)), + ) + .chain(info.depends.iter().map(|s| (PackageRelatedEnum::Depend, s))) + .chain( + info.makedepends + .iter() + .map(|s| (PackageRelatedEnum::Depend, s)), + ) + .chain( + info.checkdepends + .iter() + .map(|s| (PackageRelatedEnum::Checkdepend, s)), + ) + .chain( + info.optdepends + .iter() + .map(|s| (PackageRelatedEnum::Optdepend, s)), + ); + + PackageRelated::insert_many(related.map(|(t, s)| package_related::ActiveModel { package_id: Set(pkg_entry.id), - value: Set(s.to_string()), - })) - .on_empty_do_nothing() - .exec(&self.conn) - .await?; - - PackageConflicts::insert_many(info.conflicts.iter().map(|s| { - package_conflicts::ActiveModel { - package_id: Set(pkg_entry.id), - value: Set(s.to_string()), - } - })) - .on_empty_do_nothing() - .exec(&self.conn) - .await?; - - PackageProvides::insert_many(info.provides.iter().map(|s| package_provides::ActiveModel { - package_id: Set(pkg_entry.id), - value: Set(s.to_string()), - })) - .on_empty_do_nothing() - .exec(&self.conn) - .await?; + r#type: Set(t), + name: Set(s.to_string()), + })); PackageFile::insert_many(pkg.files.iter().map(|s| package_file::ActiveModel { package_id: Set(pkg_entry.id), - value: Set(s.display().to_string()), + path: Set(s.display().to_string()), })) .on_empty_do_nothing() .exec(&self.conn) .await?; - let deps = info - .depends - .iter() - .map(|d| (PackageDepend::Depend, d)) - .chain(info.makedepends.iter().map(|d| (PackageDepend::Make, d))) - .chain(info.checkdepends.iter().map(|d| (PackageDepend::Check, d))) - .chain(info.optdepends.iter().map(|d| (PackageDepend::Opt, d))) - .map(|(t, s)| package_depends::ActiveModel { - package_id: Set(pkg_entry.id), - r#type: Set(t), - value: Set(s.to_string()), - }); - - PackageDepends::insert_many(deps) - .on_empty_do_nothing() - .exec(&self.conn) - .await?; - Ok(()) } @@ -159,51 +152,35 @@ impl PackageQuery { .all(&self.conn) .await? .into_iter() - .map(|e| e.value) + .map(|e| e.name) .collect(); let groups = entry .find_related(PackageGroup) .all(&self.conn) .await? .into_iter() - .map(|e| e.value) + .map(|e| e.name) .collect(); - let replaces = entry - .find_related(PackageReplaces) + let related = entry + .find_related(PackageRelated) .all(&self.conn) .await? .into_iter() - .map(|e| e.value) - .collect(); - let provides = entry - .find_related(PackageProvides) - .all(&self.conn) - .await? - .into_iter() - .map(|e| e.value) - .collect(); - let depends = entry - .find_related(PackageDepends) - .all(&self.conn) - .await? - .into_iter() - .map(|e| (e.r#type, e.value)) + .map(|e| (e.r#type, e.name)) .collect(); let files = entry .find_related(PackageFile) .all(&self.conn) .await? .into_iter() - .map(|e| e.value) + .map(|e| e.path) .collect(); Ok(Some(FullPackage { entry, licenses, groups, - replaces, - provides, - depends, + related, files, })) } else { diff --git a/server/src/repo/package.rs b/server/src/repo/package.rs index 18c69c3..f519a91 100644 --- a/server/src/repo/package.rs +++ b/server/src/repo/package.rs @@ -281,7 +281,7 @@ impl From for package::ActiveModel { c_size: Set(info.csize), description: Set(info.description), url: Set(info.url), - build_date: Set(info.build_date), + build_date: Set(info.build_date.to_string()), packager: Set(info.packager), pgp_sig: Set(info.pgpsig), pgp_sig_size: Set(info.pgpsigsize),