diff --git a/entity/src/entity/mod.rs b/entity/src/entity/mod.rs index b38dbb1..6c0dd97 100644 --- a/entity/src/entity/mod.rs +++ b/entity/src/entity/mod.rs @@ -9,3 +9,4 @@ pub mod package_group; pub mod package_license; pub mod package_related; pub mod repo; +pub mod repo_mirror; diff --git a/entity/src/entity/prelude.rs b/entity/src/entity/prelude.rs index 8ebe873..0b438f5 100644 --- a/entity/src/entity/prelude.rs +++ b/entity/src/entity/prelude.rs @@ -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; diff --git a/entity/src/entity/repo.rs b/entity/src/entity/repo.rs index d68e226..0947dfc 100644 --- a/entity/src/entity/repo.rs +++ b/entity/src/entity/repo.rs @@ -12,6 +12,7 @@ pub struct Model { #[sea_orm(unique)] pub name: String, pub description: Option, + 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 for Entity { @@ -40,4 +43,10 @@ impl Related for Entity { } } +impl Related for Entity { + fn to() -> RelationDef { + Relation::RepoMirror.def() + } +} + impl ActiveModelBehavior for ActiveModel {} diff --git a/entity/src/entity/repo_mirror.rs b/entity/src/entity/repo_mirror.rs new file mode 100644 index 0000000..54f7163 --- /dev/null +++ b/entity/src/entity/repo_mirror.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 = "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 for Entity { + fn to() -> RelationDef { + Relation::Repo.def() + } +} + +impl ActiveModelBehavior for ActiveModel {} diff --git a/entity/src/lib.rs b/entity/src/lib.rs index 5c531cc..6c97883 100644 --- a/entity/src/lib.rs +++ b/entity/src/lib.rs @@ -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, +} diff --git a/migration/src/lib.rs b/migration/src/lib.rs index 691d8f1..73527fc 100644 --- a/migration/src/lib.rs +++ b/migration/src/lib.rs @@ -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> { - 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), + ] } } diff --git a/migration/src/m20240716_184104_repo_mirrors.rs b/migration/src/m20240716_184104_repo_mirrors.rs new file mode 100644 index 0000000..c76eb94 --- /dev/null +++ b/migration/src/m20240716_184104_repo_mirrors.rs @@ -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, +} diff --git a/server/src/db/query/repo.rs b/server/src/db/query/repo.rs index 3d1a952..6961c16 100644 --- a/server/src/db/query/repo.rs +++ b/server/src/db/query/repo.rs @@ -52,12 +52,14 @@ pub async fn insert( distro_id: i32, name: &str, description: Option<&str>, + r#type: entity::RepoType, ) -> Result { 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 diff --git a/server/src/repo/handle.rs b/server/src/repo/handle.rs index 99c8401..1dd34cb 100644 --- a/server/src/repo/handle.rs +++ b/server/src/repo/handle.rs @@ -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;