Skip to content

Commit ed50086

Browse files
authored
Move models into crates_io_database crate (#10597)
... and some `utils` for now until we can separate them properly πŸ˜…
1 parent acc1ede commit ed50086

33 files changed

+93
-48
lines changed

β€ŽCargo.lock

Lines changed: 17 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

β€ŽCargo.toml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,6 @@ tracing = "=0.1.41"
132132
tracing-subscriber = { version = "=0.3.19", features = ["env-filter", "json"] }
133133
typomania = { version = "=0.1.2", default-features = false }
134134
url = "=2.5.4"
135-
unicode-xid = "=0.2.6"
136135
utoipa = { version = "=5.3.1", features = ["chrono"] }
137136
utoipa-axum = "=0.2.0"
138137

β€Žcrates/crates_io_database/Cargo.toml

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,28 @@ edition = "2021"
88
workspace = true
99

1010
[dependencies]
11-
diesel = "=2.2.7"
11+
bon = "=3.3.2"
12+
chrono = { version = "=0.4.39", default-features = false, features = ["serde"] }
13+
crates_io_diesel_helpers = { path = "../crates_io_diesel_helpers" }
14+
crates_io_index = { path = "../crates_io_index" }
15+
diesel = { version = "=2.2.7", features = ["serde_json", "chrono", "numeric"] }
16+
diesel-async = "=0.5.2"
1217
diesel_full_text_search = "=2.2.0"
18+
futures-util = "=0.3.31"
19+
rand = "=0.9.0"
20+
secrecy = "=0.10.3"
21+
semver = { version = "=1.0.25", features = ["serde"] }
22+
serde = { version = "=1.0.217", features = ["derive"] }
23+
serde_json = "=1.0.138"
24+
sha2 = "=0.10.8"
25+
thiserror = "=2.0.11"
26+
tracing = "=0.1.41"
27+
unicode-xid = "=0.2.6"
1328

1429
[dev-dependencies]
30+
claims = "=0.8.0"
1531
crates_io_test_db = { path = "../crates_io_test_db" }
1632
diesel-async = { version = "=0.5.2", features = ["postgres"] }
33+
googletest = "=0.13.0"
34+
insta = "=1.42.1"
1735
tokio = { version = "=1.43.0", features = ["macros", "rt"] }
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
11
#![doc = include_str!("../README.md")]
22

3+
pub mod models;
34
pub mod schema;
5+
pub mod utils;

β€Žsrc/models/category.rs renamed to β€Žcrates/crates_io_database/src/models/category.rs

Lines changed: 16 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,12 @@
1+
use crate::models::Crate;
2+
use crate::schema::*;
13
use chrono::NaiveDateTime;
2-
use diesel::{
3-
delete, dsl, insert_into, sql_query, ExpressionMethods, QueryDsl, QueryResult,
4-
TextExpressionMethods,
5-
};
4+
use diesel::dsl;
5+
use diesel::prelude::*;
66
use diesel_async::scoped_futures::ScopedFutureExt;
77
use diesel_async::{AsyncConnection, AsyncPgConnection, RunQueryDsl};
88
use std::future::Future;
99

10-
use crate::models::Crate;
11-
use crate::schema::*;
12-
1310
#[derive(Clone, Identifiable, Queryable, QueryableByName, Debug, Selectable)]
1411
#[diesel(table_name = categories, check_for_backend(diesel::pg::Pg))]
1512
pub struct Category {
@@ -21,7 +18,7 @@ pub struct Category {
2118
pub created_at: NaiveDateTime,
2219
}
2320

24-
type WithSlug<'a> = diesel::dsl::Eq<categories::slug, crates_io_diesel_helpers::lower<&'a str>>;
21+
type WithSlug<'a> = dsl::Eq<categories::slug, crates_io_diesel_helpers::lower<&'a str>>;
2522

2623
#[derive(Associations, Insertable, Identifiable, Debug, Clone, Copy)]
2724
#[diesel(
@@ -73,12 +70,12 @@ impl Category {
7370
})
7471
.collect::<Vec<_>>();
7572

76-
delete(crates_categories::table)
73+
diesel::delete(crates_categories::table)
7774
.filter(crates_categories::crate_id.eq(crate_id))
7875
.execute(conn)
7976
.await?;
8077

81-
insert_into(crates_categories::table)
78+
diesel::insert_into(crates_categories::table)
8279
.values(&crate_categories)
8380
.execute(conn)
8481
.await?;
@@ -113,7 +110,7 @@ impl Category {
113110

114111
// Collect all the top-level categories and sum up the crates_cnt of
115112
// the crates in all subcategories
116-
sql_query(format!(include_str!("toplevel.sql"), sort_sql))
113+
diesel::sql_query(format!(include_str!("toplevel.sql"), sort_sql))
117114
.bind::<Int8, _>(limit)
118115
.bind::<Int8, _>(offset)
119116
.load(conn)
@@ -122,7 +119,7 @@ impl Category {
122119
pub async fn subcategories(&self, conn: &mut AsyncPgConnection) -> QueryResult<Vec<Category>> {
123120
use diesel::sql_types::Text;
124121

125-
sql_query(include_str!("../subcategories.sql"))
122+
diesel::sql_query(include_str!("subcategories.sql"))
126123
.bind::<Text, _>(&self.category)
127124
.load(conn)
128125
.await
@@ -138,7 +135,7 @@ impl Category {
138135
) -> QueryResult<Vec<Category>> {
139136
use diesel::sql_types::Text;
140137

141-
sql_query(include_str!("../parent_categories.sql"))
138+
diesel::sql_query(include_str!("parent_categories.sql"))
142139
.bind::<Text, _>(&self.slug)
143140
.load(conn)
144141
.await
@@ -168,7 +165,7 @@ mod tests {
168165
let test_db = TestDatabase::new();
169166
let mut conn = test_db.async_connect().await;
170167

171-
insert_into(categories::table)
168+
diesel::insert_into(categories::table)
172169
.values(&vec![
173170
(
174171
categories::category.eq("Cat 2"),
@@ -212,7 +209,7 @@ mod tests {
212209
let test_db = TestDatabase::new();
213210
let mut conn = test_db.async_connect().await;
214211

215-
insert_into(categories::table)
212+
diesel::insert_into(categories::table)
216213
.values(&vec![
217214
new_cat("Cat 1", "cat1", 0),
218215
new_cat("Cat 2", "cat2", 2),
@@ -243,7 +240,7 @@ mod tests {
243240
let test_db = TestDatabase::new();
244241
let mut conn = test_db.async_connect().await;
245242

246-
insert_into(categories::table)
243+
diesel::insert_into(categories::table)
247244
.values(&vec![
248245
(
249246
categories::category.eq("Cat 1"),
@@ -292,7 +289,7 @@ mod tests {
292289
let test_db = TestDatabase::new();
293290
let mut conn = test_db.async_connect().await;
294291

295-
insert_into(categories::table)
292+
diesel::insert_into(categories::table)
296293
.values(&vec![
297294
new_cat("Cat 1", "cat1", 1),
298295
new_cat("Cat 1::sub", "cat1::sub", 2),
@@ -334,7 +331,7 @@ mod tests {
334331
let test_db = TestDatabase::new();
335332
let mut conn = test_db.async_connect().await;
336333

337-
insert_into(categories::table)
334+
diesel::insert_into(categories::table)
338335
.values(&vec![
339336
new_cat("Cat 1", "cat1", 1),
340337
new_cat("Cat 1::sub", "cat1::sub", 2),
@@ -381,7 +378,7 @@ mod tests {
381378
let test_db = TestDatabase::new();
382379
let mut conn = test_db.async_connect().await;
383380

384-
insert_into(categories::table)
381+
diesel::insert_into(categories::table)
385382
.values(&vec![
386383
new_cat("Cat 1", "cat1", 1),
387384
new_cat("Cat 1::sub1", "cat1::sub1", 2),

β€Žsrc/models/default_versions.rs renamed to β€Žcrates/crates_io_database/src/models/default_versions.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ use crate::schema::{default_versions, versions};
22
use crates_io_diesel_helpers::SemverVersion;
33
use diesel::prelude::*;
44
use diesel_async::{AsyncPgConnection, RunQueryDsl};
5+
use tracing::{debug, instrument, warn};
56

67
/// A subset of the columns of the `versions` table.
78
///
@@ -137,6 +138,7 @@ async fn calculate_default_version(
137138
mod tests {
138139
use super::*;
139140
use crate::schema::crates;
141+
use claims::assert_some;
140142
use crates_io_test_db::TestDatabase;
141143
use insta::assert_snapshot;
142144
use std::fmt::Write;

β€Žsrc/models/deleted_crate.rs renamed to β€Žcrates/crates_io_database/src/models/deleted_crate.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1+
use crate::schema::deleted_crates;
12
use bon::Builder;
23
use chrono::{DateTime, Utc};
3-
use crates_io_database::schema::deleted_crates;
44

55
/// Struct used to `INSERT` a new `deleted_crates` record into the database.
6-
#[derive(Insertable, Debug, Builder)]
6+
#[derive(diesel::Insertable, Debug, Builder)]
77
#[diesel(table_name = deleted_crates, check_for_backend(diesel::pg::Pg))]
88
pub struct NewDeletedCrate<'a> {
99
#[builder(start_fn)]

β€Žsrc/models/dependency.rs renamed to β€Žcrates/crates_io_database/src/models/dependency.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
use diesel::sql_types::{BigInt, Text};
2-
31
use crate::models::{Crate, Version};
42
use crate::schema::*;
53
use crates_io_diesel_helpers::pg_enum;
64
use crates_io_index::DependencyKind as IndexDependencyKind;
5+
use diesel::prelude::*;
6+
use diesel::sql_types::{BigInt, Text};
77

88
#[derive(Identifiable, Associations, Debug, Queryable, QueryableByName, Selectable)]
99
#[diesel(

0 commit comments

Comments
Β (0)