From 2df52320d1b3934b2165aa627bf300b9cc3e2cb5 Mon Sep 17 00:00:00 2001 From: Chewing_Bever Date: Fri, 4 Aug 2023 18:40:17 +0200 Subject: [PATCH] feat(server): store all package info in database --- server/src/db/entities/mod.rs | 1 + server/src/db/entities/package.rs | 8 ++ server/src/db/entities/package_depends.rs | 2 +- server/src/db/entities/package_file.rs | 33 +++++ server/src/db/entities/prelude.rs | 1 + .../m20230730_000001_create_repo_tables.rs | 41 +++++- server/src/db/mod.rs | 118 +++++++++++++++++- server/src/repo/mod.rs | 19 +-- 8 files changed, 203 insertions(+), 20 deletions(-) create mode 100644 server/src/db/entities/package_file.rs diff --git a/server/src/db/entities/mod.rs b/server/src/db/entities/mod.rs index 828fec2..1111e7a 100644 --- a/server/src/db/entities/mod.rs +++ b/server/src/db/entities/mod.rs @@ -5,6 +5,7 @@ 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; diff --git a/server/src/db/entities/package.rs b/server/src/db/entities/package.rs index c34abb1..b2e2b0b 100644 --- a/server/src/db/entities/package.rs +++ b/server/src/db/entities/package.rs @@ -30,6 +30,8 @@ pub enum Relation { 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")] @@ -60,6 +62,12 @@ impl Related for Entity { } } +impl Related for Entity { + fn to() -> RelationDef { + Relation::PackageFile.def() + } +} + impl Related for Entity { fn to() -> RelationDef { Relation::PackageGroup.def() diff --git a/server/src/db/entities/package_depends.rs b/server/src/db/entities/package_depends.rs index b22f717..7e94374 100644 --- a/server/src/db/entities/package_depends.rs +++ b/server/src/db/entities/package_depends.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 r#type: i32, + pub r#type: crate::db::PackageDepend, #[sea_orm(primary_key, auto_increment = false)] pub value: String, } diff --git a/server/src/db/entities/package_file.rs b/server/src/db/entities/package_file.rs new file mode 100644 index 0000000..6e994e0 --- /dev/null +++ b/server/src/db/entities/package_file.rs @@ -0,0 +1,33 @@ +//! `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_file")] +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 9eae171..bee503c 100644 --- a/server/src/db/entities/prelude.rs +++ b/server/src/db/entities/prelude.rs @@ -3,6 +3,7 @@ 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; 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 aceae78..adefe56 100644 --- a/server/src/db/migrator/m20230730_000001_create_repo_tables.rs +++ b/server/src/db/migrator/m20230730_000001_create_repo_tables.rs @@ -212,7 +212,11 @@ impl MigrationTrait for Migration { .integer() .not_null(), ) - .col(ColumnDef::new(PackageDepends::Type).integer().not_null()) + .col( + ColumnDef::new(PackageDepends::Type) + .string_len(6) + .not_null(), + ) .col( ColumnDef::new(PackageDepends::Value) .string_len(255) @@ -234,6 +238,31 @@ impl MigrationTrait for Migration { .to_owned(), ) .await?; + manager + .create_table( + Table::create() + .table(PackageFile::Table) + .col(ColumnDef::new(PackageFile::PackageId).integer().not_null()) + .col( + ColumnDef::new(PackageFile::Value) + .string_len(255) + .not_null(), + ) + .primary_key( + Index::create() + .col(PackageFile::PackageId) + .col(PackageFile::Value), + ) + .foreign_key( + ForeignKey::create() + .name("fk-package_file-package_id") + .from(PackageFile::Table, PackageFile::PackageId) + .to(Package::Table, Package::Id) + .on_delete(ForeignKeyAction::Cascade), + ) + .to_owned(), + ) + .await?; Ok(()) } @@ -258,6 +287,9 @@ impl MigrationTrait for Migration { manager .drop_table(Table::drop().table(PackageDepends::Table).to_owned()) .await?; + manager + .drop_table(Table::drop().table(PackageFile::Table).to_owned()) + .await?; manager .drop_table(Table::drop().table(Package::Table).to_owned()) .await?; @@ -337,3 +369,10 @@ pub enum PackageDepends { Type, Value, } + +#[derive(Iden)] +pub enum PackageFile { + Table, + PackageId, + Value, +} diff --git a/server/src/db/mod.rs b/server/src/db/mod.rs index 1dbaba5..bc5204c 100644 --- a/server/src/db/mod.rs +++ b/server/src/db/mod.rs @@ -3,16 +3,31 @@ pub mod entities; mod migrator; use sea_orm::{ - ColumnTrait, ConnectOptions, Database, DatabaseConnection, DeleteResult, EntityTrait, - InsertResult, NotSet, PaginatorTrait, QueryFilter, QueryOrder, Set, + ActiveModelTrait, ColumnTrait, ConnectOptions, Database, DatabaseConnection, DeleteResult, + DeriveActiveEnum, EntityTrait, EnumIter, InsertResult, NotSet, PaginatorTrait, QueryFilter, + QueryOrder, Set, }; use sea_orm_migration::MigratorTrait; +use serde::{Deserialize, Serialize}; pub use entities::{prelude::*, *}; 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")] + Depend, + #[sea_orm(string_value = "make")] + Make, + #[sea_orm(string_value = "check")] + Check, + #[sea_orm(string_value = "opt")] + Opt, +} + #[derive(Clone, Debug)] pub struct RieterDb { pub conn: DatabaseConnection, @@ -106,4 +121,103 @@ impl RieterDb { .exec(&self.conn) .await } + + pub async fn insert_package( + &self, + repo_id: i32, + pkg: crate::repo::package::Package, + ) -> Result<()> { + let info = pkg.info; + + let model = package::ActiveModel { + id: NotSet, + repo_id: Set(repo_id), + base: Set(info.base), + name: Set(info.name), + version: Set(info.version), + arch: Set(info.arch), + size: Set(info.size), + c_size: Set(info.csize), + description: Set(info.description), + url: Set(info.url), + build_date: Set(info.build_date), + packager: Set(info.packager), + pgp_sig: Set(info.pgpsig), + pgp_sig_size: Set(info.pgpsigsize), + sha256_sum: Set(info.sha256sum), + }; + + let pkg_entry = model.insert(&self.conn).await?; + + // 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()), + })) + .on_empty_do_nothing() + .exec(self) + .await?; + + PackageGroup::insert_many(info.groups.iter().map(|s| package_group::ActiveModel { + package_id: Set(pkg_entry.id), + value: Set(s.to_string()), + })) + .on_empty_do_nothing() + .exec(self) + .await?; + + PackageReplaces::insert_many(info.replaces.iter().map(|s| package_replaces::ActiveModel { + package_id: Set(pkg_entry.id), + value: Set(s.to_string()), + })) + .on_empty_do_nothing() + .exec(self) + .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) + .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) + .await?; + + PackageFile::insert_many(pkg.files.iter().map(|s| package_file::ActiveModel { + package_id: Set(pkg_entry.id), + value: Set(s.display().to_string()), + })) + .on_empty_do_nothing() + .exec(self) + .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) + .await?; + + Ok(()) + } } diff --git a/server/src/repo/mod.rs b/server/src/repo/mod.rs index f87d572..ba8fbf7 100644 --- a/server/src/repo/mod.rs +++ b/server/src/repo/mod.rs @@ -1,11 +1,10 @@ mod manager; -mod package; +pub mod package; pub use manager::RepoGroupManager; use std::path::PathBuf; -use crate::db; use axum::body::Body; use axum::extract::{BodyStream, Path, State}; use axum::http::Request; @@ -14,7 +13,7 @@ use axum::response::IntoResponse; use axum::routing::{delete, post}; use axum::Router; use futures::StreamExt; -use sea_orm::{ActiveModelTrait, EntityTrait, ModelTrait}; +use sea_orm::ModelTrait; use std::sync::Arc; use tokio::{fs, io::AsyncWriteExt}; use tower::util::ServiceExt; @@ -145,19 +144,7 @@ async fn post_package_archive( entry.delete(&global.db).await?; } - // Insert the package's data into the database - let mut model: db::package::ActiveModel = pkg.clone().into(); - model.repo_id = sea_orm::Set(repo_id); - - let pkg_entry = model.insert(&global.db).await?; - db::PackageLicense::insert_many(pkg.info.licenses.iter().map(|s| { - db::package_license::ActiveModel { - package_id: sea_orm::Set(pkg_entry.id), - value: sea_orm::Set(s.to_string()), - } - })) - .exec(&global.db) - .await?; + global.db.insert_package(repo_id, pkg).await?; Ok(()) }