refactor(server): separate database logic into modules

db-centric-repos
Jef Roosens 2023-08-07 17:45:57 +02:00
parent e2c676dcbb
commit 3b2d6dec87
Signed by: Jef Roosens
GPG Key ID: B75D4F293C7052DB
6 changed files with 298 additions and 261 deletions

View File

@ -23,7 +23,8 @@ async fn get_repos(
) -> crate::Result<Json<PaginatedResponse<db::repo::Model>>> {
let (total_pages, repos) = global
.db
.repos(
.repo
.page(
pagination.per_page.unwrap_or(25),
pagination.page.unwrap_or(1) - 1,
)
@ -37,7 +38,8 @@ async fn get_single_repo(
) -> crate::Result<Json<db::repo::Model>> {
let repo = global
.db
.repo(id)
.repo
.by_id(id)
.await?
.ok_or(axum::http::StatusCode::NOT_FOUND)?;
@ -50,7 +52,8 @@ async fn get_packages(
) -> crate::Result<Json<PaginatedResponse<db::package::Model>>> {
let (total_pages, pkgs) = global
.db
.packages(
.pkg
.page(
pagination.per_page.unwrap_or(25),
pagination.page.unwrap_or(1) - 1,
)
@ -65,7 +68,8 @@ async fn get_single_package(
) -> crate::Result<Json<crate::db::FullPackage>> {
let entry = global
.db
.full_package(id)
.pkg
.full(id)
.await?
.ok_or(axum::http::StatusCode::NOT_FOUND)?;

View File

@ -1,12 +1,9 @@
mod conn;
pub mod entities;
mod migrator;
mod query;
use sea_orm::{
ActiveModelTrait, ColumnTrait, ConnectOptions, Database, DatabaseConnection, DeleteResult,
DeriveActiveEnum, EntityTrait, EnumIter, InsertResult, ModelTrait, NotSet, PaginatorTrait,
QueryFilter, QueryOrder, Set,
};
use sea_orm::{ConnectOptions, Database, DatabaseConnection, DeriveActiveEnum, EnumIter};
use sea_orm_migration::MigratorTrait;
use serde::{Deserialize, Serialize};
@ -42,7 +39,9 @@ pub struct FullPackage {
#[derive(Clone, Debug)]
pub struct RieterDb {
pub conn: DatabaseConnection,
conn: DatabaseConnection,
pub pkg: query::PackageQuery,
pub repo: query::RepoQuery,
}
impl RieterDb {
@ -51,244 +50,10 @@ impl RieterDb {
Migrator::up(&db, None).await?;
Ok(Self { conn: db })
}
pub async fn repos(&self, per_page: u64, page: u64) -> Result<(u64, Vec<repo::Model>)> {
let paginator = Repo::find()
.order_by_asc(repo::Column::Id)
.paginate(&self.conn, per_page);
let repos = paginator.fetch_page(page).await?;
let total_pages = paginator.num_pages().await?;
Ok((total_pages, repos))
}
pub async fn repo(&self, id: i32) -> Result<Option<repo::Model>> {
repo::Entity::find_by_id(id).one(&self.conn).await
}
pub async fn repo_by_name(&self, name: &str) -> Result<Option<repo::Model>> {
Repo::find()
.filter(repo::Column::Name.eq(name))
.one(&self.conn)
.await
}
pub async fn insert_repo(
&self,
name: &str,
description: Option<&str>,
) -> Result<InsertResult<repo::ActiveModel>> {
let model = repo::ActiveModel {
id: NotSet,
name: Set(String::from(name)),
description: Set(description.map(String::from)),
};
Repo::insert(model).exec(&self.conn).await
}
pub async fn packages(&self, per_page: u64, page: u64) -> Result<(u64, Vec<package::Model>)> {
let paginator = Package::find()
.order_by_asc(package::Column::Id)
.paginate(&self.conn, per_page);
let packages = paginator.fetch_page(page).await?;
let total_pages = paginator.num_pages().await?;
Ok((total_pages, packages))
}
pub async fn package(&self, id: i32) -> Result<Option<package::Model>> {
package::Entity::find_by_id(id).one(&self.conn).await
}
pub async fn package_by_fields(
&self,
repo_id: i32,
name: &str,
version: Option<&str>,
arch: &str,
) -> Result<Option<package::Model>> {
let mut query = Package::find()
.filter(package::Column::RepoId.eq(repo_id))
.filter(package::Column::Name.eq(name))
.filter(package::Column::Arch.eq(arch));
if let Some(version) = version {
query = query.filter(package::Column::Version.eq(version));
}
query.one(&self.conn).await
}
pub async fn delete_packages_with_arch(
&self,
repo_id: i32,
arch: &str,
) -> Result<DeleteResult> {
Package::delete_many()
.filter(package::Column::RepoId.eq(repo_id))
.filter(package::Column::Arch.eq(arch))
.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(())
}
pub async fn full_package(&self, id: i32) -> Result<Option<FullPackage>> {
if let Some(entry) = self.package(id).await? {
let licenses = entry
.find_related(PackageLicense)
.all(self)
.await?
.into_iter()
.map(|e| e.value)
.collect();
let groups = entry
.find_related(PackageGroup)
.all(self)
.await?
.into_iter()
.map(|e| e.value)
.collect();
let replaces = entry
.find_related(PackageReplaces)
.all(self)
.await?
.into_iter()
.map(|e| e.value)
.collect();
let provides = entry
.find_related(PackageProvides)
.all(self)
.await?
.into_iter()
.map(|e| e.value)
.collect();
let depends = entry
.find_related(PackageDepends)
.all(self)
.await?
.into_iter()
.map(|e| (e.r#type, e.value))
.collect();
let files = entry
.find_related(PackageFile)
.all(self)
.await?
.into_iter()
.map(|e| e.value)
.collect();
Ok(Some(FullPackage {
entry,
licenses,
groups,
replaces,
provides,
depends,
files,
}))
} else {
Ok(None)
}
Ok(Self {
conn: db.clone(),
pkg: query::PackageQuery::new(db.clone()),
repo: query::RepoQuery::new(db.clone()),
})
}
}

View File

@ -0,0 +1,7 @@
mod package;
mod repo;
pub use package::PackageQuery;
pub use repo::RepoQuery;
type Result<T> = std::result::Result<T, sea_orm::DbErr>;

View File

@ -0,0 +1,213 @@
use sea_orm::*;
use crate::db::*;
#[derive(Clone, Debug)]
pub struct PackageQuery {
conn: DatabaseConnection,
}
impl PackageQuery {
pub fn new(conn: DatabaseConnection) -> Self {
Self { conn }
}
pub async fn page(
&self,
per_page: u64,
page: u64,
) -> super::Result<(u64, Vec<package::Model>)> {
let paginator = Package::find()
.order_by_asc(package::Column::Id)
.paginate(&self.conn, per_page);
let packages = paginator.fetch_page(page).await?;
let total_pages = paginator.num_pages().await?;
Ok((total_pages, packages))
}
pub async fn by_id(&self, id: i32) -> Result<Option<package::Model>> {
package::Entity::find_by_id(id).one(&self.conn).await
}
pub async fn by_fields(
&self,
repo_id: i32,
name: &str,
version: Option<&str>,
arch: &str,
) -> Result<Option<package::Model>> {
let mut query = Package::find()
.filter(package::Column::RepoId.eq(repo_id))
.filter(package::Column::Name.eq(name))
.filter(package::Column::Arch.eq(arch));
if let Some(version) = version {
query = query.filter(package::Column::Version.eq(version));
}
query.one(&self.conn).await
}
pub async fn delete_with_arch(&self, repo_id: i32, arch: &str) -> Result<DeleteResult> {
Package::delete_many()
.filter(package::Column::RepoId.eq(repo_id))
.filter(package::Column::Arch.eq(arch))
.exec(&self.conn)
.await
}
pub async fn insert(&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.conn)
.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.conn)
.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.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?;
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.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(())
}
pub async fn full(&self, id: i32) -> Result<Option<FullPackage>> {
if let Some(entry) = self.by_id(id).await? {
let licenses = entry
.find_related(PackageLicense)
.all(&self.conn)
.await?
.into_iter()
.map(|e| e.value)
.collect();
let groups = entry
.find_related(PackageGroup)
.all(&self.conn)
.await?
.into_iter()
.map(|e| e.value)
.collect();
let replaces = entry
.find_related(PackageReplaces)
.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))
.collect();
let files = entry
.find_related(PackageFile)
.all(&self.conn)
.await?
.into_iter()
.map(|e| e.value)
.collect();
Ok(Some(FullPackage {
entry,
licenses,
groups,
replaces,
provides,
depends,
files,
}))
} else {
Ok(None)
}
}
}

View File

@ -0,0 +1,49 @@
use sea_orm::*;
use crate::db::*;
#[derive(Clone, Debug)]
pub struct RepoQuery {
conn: DatabaseConnection,
}
impl RepoQuery {
pub fn new(conn: DatabaseConnection) -> Self {
Self { conn }
}
pub async fn page(&self, per_page: u64, page: u64) -> Result<(u64, Vec<repo::Model>)> {
let paginator = Repo::find()
.order_by_asc(repo::Column::Id)
.paginate(&self.conn, per_page);
let repos = paginator.fetch_page(page).await?;
let total_pages = paginator.num_pages().await?;
Ok((total_pages, repos))
}
pub async fn by_id(&self, id: i32) -> Result<Option<repo::Model>> {
repo::Entity::find_by_id(id).one(&self.conn).await
}
pub async fn by_name(&self, name: &str) -> Result<Option<repo::Model>> {
Repo::find()
.filter(repo::Column::Name.eq(name))
.one(&self.conn)
.await
}
pub async fn insert(
&self,
name: &str,
description: Option<&str>,
) -> Result<InsertResult<repo::ActiveModel>> {
let model = repo::ActiveModel {
id: NotSet,
name: Set(String::from(name)),
description: Set(description.map(String::from)),
};
Repo::insert(model).exec(&self.conn).await
}
}

View File

@ -126,25 +126,26 @@ async fn post_package_archive(
tracing::info!("Added '{}' to repository '{}'", pkg.file_name(), repo);
// Query the repo for its ID, or create it if it does not already exist
let res = global.db.repo_by_name(&repo).await?;
let res = global.db.repo.by_name(&repo).await?;
let repo_id = if let Some(repo_entity) = res {
repo_entity.id
} else {
global.db.insert_repo(&repo, None).await?.last_insert_id
global.db.repo.insert(&repo, None).await?.last_insert_id
};
// If the package already exists in the database, we remove it first
let res = global
.db
.package_by_fields(repo_id, &pkg.info.name, None, &pkg.info.arch)
.pkg
.by_fields(repo_id, &pkg.info.name, None, &pkg.info.arch)
.await?;
if let Some(entry) = res {
entry.delete(&global.db).await?;
}
global.db.insert_package(repo_id, pkg).await?;
global.db.pkg.insert(repo_id, pkg).await?;
Ok(())
}
@ -169,7 +170,7 @@ async fn delete_repo(
.await??;
if repo_removed {
let res = global.db.repo_by_name(&repo).await?;
let res = global.db.repo.by_name(&repo).await?;
if let Some(repo_entry) = res {
repo_entry.delete(&global.db).await?;
@ -200,13 +201,10 @@ async fn delete_arch_repo(
.await??;
if repo_removed {
let res = global.db.repo_by_name(&repo).await?;
let res = global.db.repo.by_name(&repo).await?;
if let Some(repo_entry) = res {
global
.db
.delete_packages_with_arch(repo_entry.id, &arch)
.await?;
global.db.pkg.delete_with_arch(repo_entry.id, &arch).await?;
}
tracing::info!("Removed architecture '{}' from repository '{}'", arch, repo);
@ -229,12 +227,13 @@ async fn delete_package(
.await??;
if let Some((name, version, release, arch)) = res {
let res = global.db.repo_by_name(&repo).await?;
let res = global.db.repo.by_name(&repo).await?;
if let Some(repo_entry) = res {
let res = global
.db
.package_by_fields(
.pkg
.by_fields(
repo_entry.id,
&name,
Some(&format!("{}-{}", version, release)),