Skip to content

Commit

Permalink
Merge pull request #164 from kellnr/devel
Browse files Browse the repository at this point in the history
Version 5.1.0
  • Loading branch information
secana committed Jan 1, 2024
2 parents 83cfb90 + b4113ea commit d777b10
Show file tree
Hide file tree
Showing 45 changed files with 1,815 additions and 177 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ sysinfo = "0.29.11"
semver = "1.0.19"
regex = "1.8.3"
serde_json = "1.0.108"
mockall = "0.11.4"
mockall = "0.12.0"
rand = "0.8.5"
chrono = "0.4.31"
hex = "0.4.3"
Expand Down
2 changes: 1 addition & 1 deletion crates/common/src/original_name.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ impl OriginalName {
pub fn to_normalized(&self) -> NormalizedName {
NormalizedName::from(self)
}
pub fn unchecked(name: String) -> Self {
pub fn from_unchecked_str(name: String) -> Self {
Self(name)
}
}
Expand Down
9 changes: 9 additions & 0 deletions crates/db/entity/src/cratesio_crate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,23 @@ pub struct Model {
pub last_modified: String,
#[sea_orm(column_type = "Text", nullable)]
pub description: Option<String>,
pub total_downloads: i64,
}

#[derive(Copy, Clone, Debug, EnumIter, DeriveRelation)]
pub enum Relation {
#[sea_orm(has_many = "super::cratesio_meta::Entity")]
CratesioMeta,
#[sea_orm(has_many = "super::cratesio_index::Entity")]
CratesioIndex,
}

impl Related<super::cratesio_meta::Entity> for Entity {
fn to() -> RelationDef {
Relation::CratesioMeta.def()
}
}

impl Related<super::cratesio_index::Entity> for Entity {
fn to() -> RelationDef {
Relation::CratesioIndex.def()
Expand Down
34 changes: 34 additions & 0 deletions crates/db/entity/src/cratesio_meta.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
//! SeaORM Entity. Generated by sea-orm-codegen 0.9.1

use sea_orm::entity::prelude::*;

#[derive(Clone, Debug, PartialEq, DeriveEntityModel)]
#[sea_orm(table_name = "cratesio_meta")]
pub struct Model {
#[sea_orm(primary_key)]
pub id: i64,
#[sea_orm(column_type = "Text")]
pub version: String,
pub downloads: i64,
pub crates_io_fk: i64,
}

#[derive(Copy, Clone, Debug, EnumIter, DeriveRelation)]
pub enum Relation {
#[sea_orm(
belongs_to = "super::cratesio_crate::Entity",
from = "Column::CratesIoFk",
to = "super::cratesio_crate::Column::Id",
on_update = "NoAction",
on_delete = "Cascade"
)]
CratesioCrate,
}

impl Related<super::cratesio_crate::Entity> for Entity {
fn to() -> RelationDef {
Relation::CratesioCrate.def()
}
}

impl ActiveModelBehavior for ActiveModel {}
34 changes: 17 additions & 17 deletions crates/db/entity/src/krate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,48 +20,48 @@ pub struct Model {
pub homepage: Option<String>,
#[sea_orm(column_type = "Text", nullable)]
pub repository: Option<String>,
#[sea_orm(column_type = "Text", unique)]
#[sea_orm(column_type = "Text")]
pub original_name: String,
pub e_tag: String,
}

#[derive(Copy, Clone, Debug, EnumIter, DeriveRelation)]
pub enum Relation {
#[sea_orm(has_many = "super::crate_index::Entity")]
CrateIndex,
#[sea_orm(has_many = "super::owner::Entity")]
Owner,
#[sea_orm(has_many = "super::crate_meta::Entity")]
CrateMeta,
#[sea_orm(has_many = "super::crate_keyword_to_crate::Entity")]
CrateKeywordToCrate,
#[sea_orm(has_many = "super::crate_author_to_crate::Entity")]
CrateAuthorToCrate,
#[sea_orm(has_many = "super::owner::Entity")]
Owner,
#[sea_orm(has_many = "super::crate_category_to_crate::Entity")]
CrateCategoryToCrate,
#[sea_orm(has_many = "super::crate_index::Entity")]
CrateIndex,
#[sea_orm(has_many = "super::crate_keyword_to_crate::Entity")]
CrateKeywordToCrate,
}

impl Related<super::crate_meta::Entity> for Entity {
impl Related<super::crate_index::Entity> for Entity {
fn to() -> RelationDef {
Relation::CrateMeta.def()
Relation::CrateIndex.def()
}
}

impl Related<super::crate_keyword_to_crate::Entity> for Entity {
impl Related<super::owner::Entity> for Entity {
fn to() -> RelationDef {
Relation::CrateKeywordToCrate.def()
Relation::Owner.def()
}
}

impl Related<super::crate_author_to_crate::Entity> for Entity {
impl Related<super::crate_meta::Entity> for Entity {
fn to() -> RelationDef {
Relation::CrateAuthorToCrate.def()
Relation::CrateMeta.def()
}
}

impl Related<super::owner::Entity> for Entity {
impl Related<super::crate_author_to_crate::Entity> for Entity {
fn to() -> RelationDef {
Relation::Owner.def()
Relation::CrateAuthorToCrate.def()
}
}

Expand All @@ -71,9 +71,9 @@ impl Related<super::crate_category_to_crate::Entity> for Entity {
}
}

impl Related<super::crate_index::Entity> for Entity {
impl Related<super::crate_keyword_to_crate::Entity> for Entity {
fn to() -> RelationDef {
Relation::CrateIndex.def()
Relation::CrateKeywordToCrate.def()
}
}

Expand Down
1 change: 1 addition & 0 deletions crates/db/entity/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ pub mod crate_keyword_to_crate;
pub mod crate_meta;
pub mod cratesio_crate;
pub mod cratesio_index;
pub mod cratesio_meta;
pub mod doc_queue;
pub mod krate;
pub mod owner;
Expand Down
1 change: 1 addition & 0 deletions crates/db/entity/src/prelude.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ pub use super::crate_keyword_to_crate::Entity as CrateKeywordToCrate;
pub use super::crate_meta::Entity as CrateMeta;
pub use super::cratesio_crate::Entity as CratesioCrate;
pub use super::cratesio_index::Entity as CratesioIndex;
pub use super::cratesio_meta::Entity as CratesioMeta;
pub use super::doc_queue::Entity as DocQueue;
pub use super::krate::Entity as Krate;
pub use super::owner::Entity as Owner;
Expand Down
12 changes: 6 additions & 6 deletions crates/db/entity/src/user.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,10 @@ pub struct Model {
pub enum Relation {
#[sea_orm(has_many = "super::session::Entity")]
Session,
#[sea_orm(has_many = "super::auth_token::Entity")]
AuthToken,
#[sea_orm(has_many = "super::owner::Entity")]
Owner,
#[sea_orm(has_many = "super::auth_token::Entity")]
AuthToken,
}

impl Related<super::session::Entity> for Entity {
Expand All @@ -32,15 +32,15 @@ impl Related<super::session::Entity> for Entity {
}
}

impl Related<super::auth_token::Entity> for Entity {
impl Related<super::owner::Entity> for Entity {
fn to() -> RelationDef {
Relation::AuthToken.def()
Relation::Owner.def()
}
}

impl Related<super::owner::Entity> for Entity {
impl Related<super::auth_token::Entity> for Entity {
fn to() -> RelationDef {
Relation::Owner.def()
Relation::AuthToken.def()
}
}

Expand Down
10 changes: 10 additions & 0 deletions crates/db/migration/src/iden.rs
Original file line number Diff line number Diff line change
Expand Up @@ -176,3 +176,13 @@ pub enum CratesIoIndexIden {
V,
CratesIoFk,
}

#[derive(Iden)]
pub enum CratesIoMetaIden {
#[iden = "cratesio_meta"]
Table,
Id,
Version,
Downloads,
CratesIoFk,
}
3 changes: 3 additions & 0 deletions crates/db/migration/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ mod m20220101_000006_create_table;
mod m20220101_000006_create_table_entities;
mod m20220101_000007_create_table;
mod m20220101_000007_create_table_entities;
mod m20220101_000008_create_table;
mod m20220101_000008_create_table_entities;
mod old_index_metadata;

pub struct Migrator;
Expand All @@ -29,6 +31,7 @@ impl MigratorTrait for Migrator {
Box::new(m20220101_000005_create_table::Migration),
Box::new(m20220101_000006_create_table::Migration),
Box::new(m20220101_000007_create_table::Migration),
Box::new(m20220101_000008_create_table::Migration),
]
}
}
135 changes: 135 additions & 0 deletions crates/db/migration/src/m20220101_000008_create_table.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,135 @@
use sea_orm_migration::prelude::*;
use crate::sea_orm::ActiveValue::Set;
use crate::sea_orm::{ActiveModelTrait, EntityTrait};
use tracing::debug;

#[derive(DeriveMigrationName)]
pub struct Migration;

#[async_trait::async_trait]
impl MigrationTrait for Migration {
async fn up(&self, manager: &SchemaManager) -> Result<(), DbErr> {
// Manual check if the column exists is needed, as Sqlite does not support
// ALTER TABLE IF COLUMN EXISTS. Without the check, the migration would fail
// on Sqlite with an "duplicate column" error.

if !manager
.has_column("cratesio_crate", "total_downloads")
.await?
{
manager
.alter_table(
Table::alter()
.table(CratesIoIden::Table)
.add_column_if_not_exists(
ColumnDef::new(CratesIoIden::TotalDownloads)
.big_integer()
.not_null()
.default(0),
)
.to_owned(),
)
.await?;
}

manager
.create_table(
Table::create()
.table(CratesIoMetaIden::Table)
.if_not_exists()
.col(
ColumnDef::new(CratesIoMetaIden::Id)
.big_integer()
.not_null()
.primary_key()
.auto_increment(),
)
.col(ColumnDef::new(CratesIoMetaIden::Version).text().not_null())
.col(
ColumnDef::new(CratesIoMetaIden::Downloads)
.big_integer()
.not_null()
.default(0),
)
.col(
ColumnDef::new(CratesIoMetaIden::CratesIoFk)
.big_integer()
.not_null(),
)
.foreign_key(
ForeignKey::create()
.name("cratesio_fk")
.from(CratesIoMetaIden::Table, CratesIoMetaIden::CratesIoFk)
.to(CratesIoIden::Table, CratesIoIden::Id)
.on_delete(ForeignKeyAction::Cascade)
.on_update(ForeignKeyAction::NoAction),
)
.to_owned(),
)
.await?;

fill_new_columns(manager.get_connection()).await
}

async fn down(&self, manager: &SchemaManager) -> Result<(), DbErr> {
// Drop tables in reverse order of creation
manager
.drop_table(Table::drop().table(CratesIoMetaIden::Table).to_owned())
.await?;
manager
.alter_table(
Table::alter()
.table(CratesIoIden::Table)
.drop_column(CratesIoIden::TotalDownloads)
.to_owned(),
)
.await
}
}


async fn fill_new_columns(db: &SchemaManagerConnection<'_>) -> Result<(), DbErr> {
use crate::m20220101_000008_create_table_entities::{cratesio_index, cratesio_meta};

// Get all cached crate versions
let cached_versions = cratesio_index::Entity::find()
.all(db)
.await?
.into_iter()
.collect::<Vec<_>>();

// Create a cossepsonding cratesio_meta entry for each cached crate version
for cached_version in cached_versions {
let crate_id = cached_version.crates_io_fk;
let version = cached_version.vers;

let meta = cratesio_meta::ActiveModel {
id: Default::default(),
version: Set(version),
downloads: Default::default(),
crates_io_fk: Set(crate_id),
};

cratesio_meta::Entity::insert(meta).exec(db).await?;
}

Ok(())
}

#[derive(Iden)]
pub enum CratesIoIden {
#[iden = "cratesio_crate"]
Table,
Id,
TotalDownloads,
}

#[derive(Iden)]
pub enum CratesIoMetaIden {
#[iden = "cratesio_meta"]
Table,
Id,
Version,
Downloads,
CratesIoFk,
}
Loading

0 comments on commit d777b10

Please sign in to comment.