feat(server): store all package info in database

repo-db
Jef Roosens 2023-08-04 18:40:17 +02:00
parent 0ff225dddb
commit 2df52320d1
Signed by: Jef Roosens
GPG Key ID: B75D4F293C7052DB
8 changed files with 203 additions and 20 deletions

View File

@ -5,6 +5,7 @@ pub mod prelude;
pub mod package; pub mod package;
pub mod package_conflicts; pub mod package_conflicts;
pub mod package_depends; pub mod package_depends;
pub mod package_file;
pub mod package_group; pub mod package_group;
pub mod package_license; pub mod package_license;
pub mod package_provides; pub mod package_provides;

View File

@ -30,6 +30,8 @@ pub enum Relation {
PackageConflicts, PackageConflicts,
#[sea_orm(has_many = "super::package_depends::Entity")] #[sea_orm(has_many = "super::package_depends::Entity")]
PackageDepends, PackageDepends,
#[sea_orm(has_many = "super::package_file::Entity")]
PackageFile,
#[sea_orm(has_many = "super::package_group::Entity")] #[sea_orm(has_many = "super::package_group::Entity")]
PackageGroup, PackageGroup,
#[sea_orm(has_many = "super::package_license::Entity")] #[sea_orm(has_many = "super::package_license::Entity")]
@ -60,6 +62,12 @@ impl Related<super::package_depends::Entity> for Entity {
} }
} }
impl Related<super::package_file::Entity> for Entity {
fn to() -> RelationDef {
Relation::PackageFile.def()
}
}
impl Related<super::package_group::Entity> for Entity { impl Related<super::package_group::Entity> for Entity {
fn to() -> RelationDef { fn to() -> RelationDef {
Relation::PackageGroup.def() Relation::PackageGroup.def()

View File

@ -9,7 +9,7 @@ pub struct Model {
#[sea_orm(primary_key, auto_increment = false)] #[sea_orm(primary_key, auto_increment = false)]
pub package_id: i32, pub package_id: i32,
#[sea_orm(primary_key, auto_increment = false)] #[sea_orm(primary_key, auto_increment = false)]
pub r#type: i32, pub r#type: crate::db::PackageDepend,
#[sea_orm(primary_key, auto_increment = false)] #[sea_orm(primary_key, auto_increment = false)]
pub value: String, pub value: String,
} }

View File

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

View File

@ -3,6 +3,7 @@
pub use super::package::Entity as Package; pub use super::package::Entity as Package;
pub use super::package_conflicts::Entity as PackageConflicts; pub use super::package_conflicts::Entity as PackageConflicts;
pub use super::package_depends::Entity as PackageDepends; 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_group::Entity as PackageGroup;
pub use super::package_license::Entity as PackageLicense; pub use super::package_license::Entity as PackageLicense;
pub use super::package_provides::Entity as PackageProvides; pub use super::package_provides::Entity as PackageProvides;

View File

@ -212,7 +212,11 @@ impl MigrationTrait for Migration {
.integer() .integer()
.not_null(), .not_null(),
) )
.col(ColumnDef::new(PackageDepends::Type).integer().not_null()) .col(
ColumnDef::new(PackageDepends::Type)
.string_len(6)
.not_null(),
)
.col( .col(
ColumnDef::new(PackageDepends::Value) ColumnDef::new(PackageDepends::Value)
.string_len(255) .string_len(255)
@ -234,6 +238,31 @@ impl MigrationTrait for Migration {
.to_owned(), .to_owned(),
) )
.await?; .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(()) Ok(())
} }
@ -258,6 +287,9 @@ impl MigrationTrait for Migration {
manager manager
.drop_table(Table::drop().table(PackageDepends::Table).to_owned()) .drop_table(Table::drop().table(PackageDepends::Table).to_owned())
.await?; .await?;
manager
.drop_table(Table::drop().table(PackageFile::Table).to_owned())
.await?;
manager manager
.drop_table(Table::drop().table(Package::Table).to_owned()) .drop_table(Table::drop().table(Package::Table).to_owned())
.await?; .await?;
@ -337,3 +369,10 @@ pub enum PackageDepends {
Type, Type,
Value, Value,
} }
#[derive(Iden)]
pub enum PackageFile {
Table,
PackageId,
Value,
}

View File

@ -3,16 +3,31 @@ pub mod entities;
mod migrator; mod migrator;
use sea_orm::{ use sea_orm::{
ColumnTrait, ConnectOptions, Database, DatabaseConnection, DeleteResult, EntityTrait, ActiveModelTrait, ColumnTrait, ConnectOptions, Database, DatabaseConnection, DeleteResult,
InsertResult, NotSet, PaginatorTrait, QueryFilter, QueryOrder, Set, DeriveActiveEnum, EntityTrait, EnumIter, InsertResult, NotSet, PaginatorTrait, QueryFilter,
QueryOrder, Set,
}; };
use sea_orm_migration::MigratorTrait; use sea_orm_migration::MigratorTrait;
use serde::{Deserialize, Serialize};
pub use entities::{prelude::*, *}; pub use entities::{prelude::*, *};
use migrator::Migrator; use migrator::Migrator;
type Result<T> = std::result::Result<T, sea_orm::DbErr>; type Result<T> = std::result::Result<T, sea_orm::DbErr>;
#[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)] #[derive(Clone, Debug)]
pub struct RieterDb { pub struct RieterDb {
pub conn: DatabaseConnection, pub conn: DatabaseConnection,
@ -106,4 +121,103 @@ impl RieterDb {
.exec(&self.conn) .exec(&self.conn)
.await .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(())
}
} }

View File

@ -1,11 +1,10 @@
mod manager; mod manager;
mod package; pub mod package;
pub use manager::RepoGroupManager; pub use manager::RepoGroupManager;
use std::path::PathBuf; use std::path::PathBuf;
use crate::db;
use axum::body::Body; use axum::body::Body;
use axum::extract::{BodyStream, Path, State}; use axum::extract::{BodyStream, Path, State};
use axum::http::Request; use axum::http::Request;
@ -14,7 +13,7 @@ use axum::response::IntoResponse;
use axum::routing::{delete, post}; use axum::routing::{delete, post};
use axum::Router; use axum::Router;
use futures::StreamExt; use futures::StreamExt;
use sea_orm::{ActiveModelTrait, EntityTrait, ModelTrait}; use sea_orm::ModelTrait;
use std::sync::Arc; use std::sync::Arc;
use tokio::{fs, io::AsyncWriteExt}; use tokio::{fs, io::AsyncWriteExt};
use tower::util::ServiceExt; use tower::util::ServiceExt;
@ -145,19 +144,7 @@ async fn post_package_archive(
entry.delete(&global.db).await?; entry.delete(&global.db).await?;
} }
// Insert the package's data into the database global.db.insert_package(repo_id, pkg).await?;
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?;
Ok(()) Ok(())
} }