feat: added repo mirrors migration
ci/woodpecker/push/build Pipeline was successful Details
ci/woodpecker/push/lint Pipeline failed Details

feat/mirror-api
Jef Roosens 2024-07-18 22:40:31 +02:00
parent f761e3b36d
commit cbb04a40e0
Signed by: Jef Roosens
GPG Key ID: B75D4F293C7052DB
9 changed files with 135 additions and 1 deletions

View File

@ -9,3 +9,4 @@ pub mod package_group;
pub mod package_license;
pub mod package_related;
pub mod repo;
pub mod repo_mirror;

View File

@ -7,3 +7,4 @@ pub use super::package_group::Entity as PackageGroup;
pub use super::package_license::Entity as PackageLicense;
pub use super::package_related::Entity as PackageRelated;
pub use super::repo::Entity as Repo;
pub use super::repo_mirror::Entity as RepoMirror;

View File

@ -12,6 +12,7 @@ pub struct Model {
#[sea_orm(unique)]
pub name: String,
pub description: Option<String>,
pub r#type: crate::RepoType,
}
#[derive(Copy, Clone, Debug, EnumIter, DeriveRelation)]
@ -26,6 +27,8 @@ pub enum Relation {
Distro,
#[sea_orm(has_many = "super::package::Entity")]
Package,
#[sea_orm(has_many = "super::repo_mirror::Entity")]
RepoMirror,
}
impl Related<super::distro::Entity> for Entity {
@ -40,4 +43,10 @@ impl Related<super::package::Entity> for Entity {
}
}
impl Related<super::repo_mirror::Entity> for Entity {
fn to() -> RelationDef {
Relation::RepoMirror.def()
}
}
impl ActiveModelBehavior for ActiveModel {}

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 = "repo_mirror")]
pub struct Model {
#[sea_orm(primary_key)]
pub id: i32,
pub repo_id: i32,
pub url: String,
}
#[derive(Copy, Clone, Debug, EnumIter, DeriveRelation)]
pub enum Relation {
#[sea_orm(
belongs_to = "super::repo::Entity",
from = "Column::RepoId",
to = "super::repo::Column::Id",
on_update = "NoAction",
on_delete = "Cascade"
)]
Repo,
}
impl Related<super::repo::Entity> for Entity {
fn to() -> RelationDef {
Relation::Repo.def()
}
}
impl ActiveModelBehavior for ActiveModel {}

View File

@ -35,3 +35,12 @@ pub enum PackageState {
#[sea_orm(num_value = 2)]
PendingDeletion,
}
#[derive(EnumIter, DeriveActiveEnum, Deserialize, Serialize, PartialEq, Eq, Clone, Debug)]
#[sea_orm(rs_type = "i32", db_type = "Integer")]
pub enum RepoType {
#[sea_orm(num_value = 0)]
Regular,
#[sea_orm(num_value = 1)]
FullMirror,
}

View File

@ -1,12 +1,16 @@
pub use sea_orm_migration::prelude::*;
mod m20230730_000001_create_repo_tables;
mod m20240716_184104_repo_mirrors;
pub struct Migrator;
#[async_trait::async_trait]
impl MigratorTrait for Migrator {
fn migrations() -> Vec<Box<dyn MigrationTrait>> {
vec![Box::new(m20230730_000001_create_repo_tables::Migration)]
vec![
Box::new(m20230730_000001_create_repo_tables::Migration),
Box::new(m20240716_184104_repo_mirrors::Migration),
]
}
}

View File

@ -0,0 +1,74 @@
use sea_orm_migration::prelude::*;
#[derive(DeriveMigrationName)]
pub struct Migration;
#[async_trait::async_trait]
impl MigrationTrait for Migration {
async fn up(&self, manager: &SchemaManager) -> Result<(), DbErr> {
manager
.alter_table(
Table::alter()
.table(Repo::Table)
.add_column(
ColumnDef::new(Repo::Type)
.integer()
.not_null()
.default(0),
)
.to_owned(),
)
.await?;
manager
.create_table(
Table::create()
.table(RepoMirror::Table)
.col(
ColumnDef::new(RepoMirror::Id)
.integer()
.not_null()
.auto_increment()
.primary_key()
)
.col(ColumnDef::new(RepoMirror::RepoId).integer().not_null())
.col(ColumnDef::new(RepoMirror::Url).string().not_null())
.foreign_key(
ForeignKey::create()
.name("fk-repo_mirror-repo-id")
.from(RepoMirror::Table, RepoMirror::RepoId)
.to(Repo::Table, Repo::Id)
.on_delete(ForeignKeyAction::Cascade),
)
.to_owned(),
)
.await?;
Ok(())
}
async fn down(&self, manager: &SchemaManager) -> Result<(), DbErr> {
manager
.drop_table(Table::drop().table(RepoMirror::Table).to_owned())
.await?;
manager
.alter_table(Table::alter().drop_column(Repo::Type).to_owned())
.await?;
Ok(())
}
}
#[derive(Iden)]
pub enum Repo {
Table,
Id,
Type,
}
#[derive(Iden)]
pub enum RepoMirror {
Table,
Id,
RepoId,
Url,
}

View File

@ -52,12 +52,14 @@ pub async fn insert(
distro_id: i32,
name: &str,
description: Option<&str>,
r#type: entity::RepoType,
) -> Result<repo::Model> {
let model = repo::ActiveModel {
id: NotSet,
distro_id: Set(distro_id),
name: Set(String::from(name)),
description: Set(description.map(String::from)),
r#type: Set(r#type),
};
model.insert(conn).await

View File

@ -70,6 +70,7 @@ impl Handle {
distro_id: Set(distro_id),
name: Set(repo.to_string()),
description: NotSet,
r#type: Set(entity::RepoType::Regular),
};
let id = new_repo.insert(&self.state.conn).await?.id;