Skip to content

Commit aa2ac99

Browse files
committed
Upgrade to diesel 2.0 release candidate
1 parent c35ca0a commit aa2ac99

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

77 files changed

+655
-646
lines changed

Cargo.lock

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

Cargo.toml

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -43,9 +43,9 @@ cookie = { version = "=0.16.0", features = ["secure"] }
4343
dashmap = { version = "=5.3.4", features = ["raw-api"] }
4444
derive_deref = "=1.1.1"
4545
dialoguer = "=0.10.1"
46-
diesel = { version = "=1.4.8", features = ["postgres", "serde_json", "chrono", "r2d2"] }
47-
diesel_full_text_search = "=1.0.1"
48-
diesel_migrations = { version = "=1.4.0", features = ["postgres"] }
46+
diesel = { version = "2.0.0-rc.0", features = ["postgres", "serde_json", "chrono", "r2d2"] }
47+
diesel_full_text_search = { version = "2.0.0-rc.0", git = "https://github.com/Emulator000/diesel_full_text_search", rev = "06b3505a3c4fcdd931410c24f945d87f860cdfd6" }
48+
diesel_migrations = { version = "2.0.0-rc.0", features = ["postgres"] }
4949
dotenv = "=0.15.0"
5050
flate2 = "=1.0.24"
5151
futures-channel = { version = "=0.3.21", default-features = false }
@@ -74,7 +74,7 @@ serde = { version = "=1.0.137", features = ["derive"] }
7474
serde_json = "=1.0.81"
7575
sha2 = "=0.10.2"
7676
spdx = "=0.8.1"
77-
swirl = { git = "https://github.com/sgrif/swirl.git", rev = "e87cf37" }
77+
swirl = { git = "https://github.com/jtgeibel/swirl", branch = "diesel2" }
7878
tar = "=0.4.38"
7979
tempfile = "=3.3.0"
8080
thiserror = "=1.0.31"
@@ -94,6 +94,6 @@ tokio = "=1.19.2"
9494
tower-service = "=0.3.1"
9595

9696
[build-dependencies]
97-
diesel = { version = "=1.4.8", features = ["postgres"] }
98-
diesel_migrations = { version = "=1.4.0", features = ["postgres"] }
97+
diesel = { version = "2.0.0-rc.0", features = ["postgres"] }
98+
diesel_migrations = { version = "2.0.0-rc.0", features = ["postgres"] }
9999
dotenv = "=0.15.0"

build.rs

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use diesel::prelude::*;
2-
use diesel_migrations::run_pending_migrations;
2+
use diesel_migrations::{FileBasedMigrations, MigrationHarness};
33
use std::env;
44

55
fn main() {
@@ -8,9 +8,13 @@ fn main() {
88
println!("cargo:rerun-if-changed=migrations/");
99
if env::var("PROFILE") == Ok("debug".into()) {
1010
if let Ok(database_url) = dotenv::var("TEST_DATABASE_URL") {
11-
let connection = PgConnection::establish(&database_url)
11+
let connection = &mut PgConnection::establish(&database_url)
1212
.expect("Could not connect to TEST_DATABASE_URL");
13-
run_pending_migrations(&connection).expect("Error running migrations");
13+
let migrations = FileBasedMigrations::find_migrations_directory()
14+
.expect("Could not find migrations");
15+
connection
16+
.run_pending_migrations(migrations)
17+
.expect("Error running migrations");
1418
}
1519
}
1620
}

src/admin/delete_crate.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,15 +19,15 @@ pub struct Opts {
1919
}
2020

2121
pub fn run(opts: Opts) {
22-
let conn = db::oneoff_connection().unwrap();
23-
conn.transaction::<_, diesel::result::Error, _>(|| {
24-
delete(opts, &conn);
22+
let conn = &mut db::oneoff_connection().unwrap();
23+
conn.transaction::<_, diesel::result::Error, _>(|conn| {
24+
delete(opts, conn);
2525
Ok(())
2626
})
2727
.unwrap()
2828
}
2929

30-
fn delete(opts: Opts, conn: &PgConnection) {
30+
fn delete(opts: Opts, conn: &mut PgConnection) {
3131
let krate: Crate = Crate::by_name(&opts.crate_name).first(conn).unwrap();
3232

3333
let config = config::Base::from_environment();

src/admin/delete_version.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,15 +24,15 @@ pub struct Opts {
2424
}
2525

2626
pub fn run(opts: Opts) {
27-
let conn = db::oneoff_connection().unwrap();
28-
conn.transaction::<_, diesel::result::Error, _>(|| {
29-
delete(opts, &conn);
27+
let conn = &mut db::oneoff_connection().unwrap();
28+
conn.transaction::<_, diesel::result::Error, _>(|conn| {
29+
delete(opts, conn);
3030
Ok(())
3131
})
3232
.unwrap()
3333
}
3434

35-
fn delete(opts: Opts, conn: &PgConnection) {
35+
fn delete(opts: Opts, conn: &mut PgConnection) {
3636
let krate: Crate = Crate::by_name(&opts.crate_name).first(conn).unwrap();
3737
let v: Version = Version::belonging_to(&krate)
3838
.filter(versions::num.eq(&opts.version))

src/admin/migrate.rs

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
11
use anyhow::Error;
2+
use diesel_migrations::{
3+
embed_migrations, EmbeddedMigrations, HarnessWithOutput, MigrationHarness,
4+
};
25

36
static CATEGORIES_TOML: &str = include_str!("../boot/categories.toml");
4-
diesel_migrations::embed_migrations!("./migrations");
7+
8+
pub const MIGRATIONS: EmbeddedMigrations = embed_migrations!("./migrations");
59

610
#[derive(clap::Parser, Debug, Copy, Clone)]
711
#[clap(
@@ -32,13 +36,16 @@ pub fn run(_opts: Opts) -> Result<(), Error> {
3236
}
3337

3438
// The primary is online, access directly via `DATABASE_URL`.
35-
let conn = crate::db::oneoff_connection_with_config(&config)?;
39+
let conn = &mut crate::db::oneoff_connection_with_config(&config)?;
3640

3741
println!("==> migrating the database");
38-
embedded_migrations::run_with_output(&conn, &mut std::io::stdout())?;
42+
// let migrations = ;
43+
let mut stdout = std::io::stdout();
44+
let mut harness = HarnessWithOutput::new(conn, &mut stdout);
45+
harness.run_pending_migrations(MIGRATIONS);
3946

4047
println!("==> synchronizing crate categories");
41-
crate::boot::categories::sync_with_connection(CATEGORIES_TOML, &conn).unwrap();
48+
crate::boot::categories::sync_with_connection(CATEGORIES_TOML, conn).unwrap();
4249

4350
Ok(())
4451
}

src/admin/populate.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,11 @@ pub struct Opts {
1414
}
1515

1616
pub fn run(opts: Opts) {
17-
let conn = db::oneoff_connection().unwrap();
18-
conn.transaction(|| update(opts, &conn)).unwrap();
17+
let mut conn = db::oneoff_connection().unwrap();
18+
conn.transaction(|conn| update(opts, conn)).unwrap();
1919
}
2020

21-
fn update(opts: Opts, conn: &PgConnection) -> QueryResult<()> {
21+
fn update(opts: Opts, conn: &mut PgConnection) -> QueryResult<()> {
2222
use diesel::dsl::*;
2323

2424
for id in opts.version_ids {

src/admin/render_readmes.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ use std::{io::Read, path::Path, sync::Arc, thread};
99

1010
use cargo_registry_markdown::text_to_html;
1111
use chrono::{TimeZone, Utc};
12-
use diesel::{dsl::any, prelude::*};
12+
use diesel::prelude::*;
1313
use flate2::read::GzDecoder;
1414
use reqwest::{blocking::Client, header};
1515
use tar::{self, Archive};
@@ -39,7 +39,7 @@ pub struct Opts {
3939

4040
pub fn run(opts: Opts) -> anyhow::Result<()> {
4141
let base_config = Arc::new(config::Base::from_environment());
42-
let conn = db::oneoff_connection().unwrap();
42+
let conn = &mut db::oneoff_connection().unwrap();
4343

4444
let start_time = Utc::now();
4545

@@ -70,7 +70,7 @@ pub fn run(opts: Opts) -> anyhow::Result<()> {
7070
query = query.filter(crates::name.eq(crate_name));
7171
}
7272

73-
let version_ids: Vec<i32> = query.load(&conn).expect("error loading version ids");
73+
let version_ids: Vec<i32> = query.load(conn).expect("error loading version ids");
7474

7575
let total_versions = version_ids.len();
7676
println!("Rendering {total_versions} versions");
@@ -95,14 +95,14 @@ pub fn run(opts: Opts) -> anyhow::Result<()> {
9595

9696
let versions: Vec<(Version, String)> = versions::table
9797
.inner_join(crates::table)
98-
.filter(versions::id.eq(any(version_ids_chunk)))
98+
.filter(versions::id.eq_any(version_ids_chunk))
9999
.select((versions::all_columns, crates::name))
100-
.load(&conn)
100+
.load(conn)
101101
.expect("error loading versions");
102102

103103
let mut tasks = Vec::with_capacity(page_size as usize);
104104
for (version, krate_name) in versions {
105-
Version::record_readme_rendering(version.id, &conn)
105+
Version::record_readme_rendering(version.id, conn)
106106
.context("Couldn't record rendering time")?;
107107

108108
let client = client.clone();

src/admin/transfer_crates.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,15 +21,15 @@ pub struct Opts {
2121
}
2222

2323
pub fn run(opts: Opts) {
24-
let conn = db::oneoff_connection().unwrap();
25-
conn.transaction::<_, diesel::result::Error, _>(|| {
26-
transfer(opts, &conn);
24+
let conn = &mut db::oneoff_connection().unwrap();
25+
conn.transaction::<_, diesel::result::Error, _>(|conn| {
26+
transfer(opts, conn);
2727
Ok(())
2828
})
2929
.unwrap()
3030
}
3131

32-
fn transfer(opts: Opts, conn: &PgConnection) {
32+
fn transfer(opts: Opts, conn: &mut PgConnection) {
3333
let from: User = users::table
3434
.filter(users::gh_login.eq(opts.from_user))
3535
.first(conn)

src/admin/verify_token.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@ pub struct Opts {
1313
}
1414

1515
pub fn run(opts: Opts) -> AppResult<()> {
16-
let conn = db::oneoff_connection()?;
17-
let user = User::find_by_api_token(&conn, &opts.api_token)?;
16+
let conn = &mut db::oneoff_connection()?;
17+
let user = User::find_by_api_token(conn, &opts.api_token)?;
1818
println!("The token belongs to user {}", user.gh_login);
1919
Ok(())
2020
}

src/bin/enqueue-job.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,25 +18,25 @@ fn main() -> Result<()> {
1818
let count: i64 = background_jobs
1919
.filter(job_type.eq("update_downloads"))
2020
.count()
21-
.get_result(&conn)
21+
.get_result(conn)
2222
.unwrap();
2323

2424
if count > 0 {
2525
println!("Did not enqueue update_downloads, existing job already in progress");
2626
Ok(())
2727
} else {
28-
Ok(worker::update_downloads().enqueue(&conn)?)
28+
Ok(worker::update_downloads().enqueue(conn)?)
2929
}
3030
}
3131
"dump_db" => {
3232
let database_url = args.next().unwrap_or_else(|| env("READ_ONLY_REPLICA_URL"));
3333
let target_name = args
3434
.next()
3535
.unwrap_or_else(|| String::from("db-dump.tar.gz"));
36-
Ok(worker::dump_db(database_url, target_name).enqueue(&conn)?)
36+
Ok(worker::dump_db(database_url, target_name).enqueue(conn)?)
3737
}
38-
"daily_db_maintenance" => Ok(worker::daily_db_maintenance().enqueue(&conn)?),
39-
"squash_index" => Ok(worker::squash_index().enqueue(&conn)?),
38+
"daily_db_maintenance" => Ok(worker::daily_db_maintenance().enqueue(conn)?),
39+
"squash_index" => Ok(worker::squash_index().enqueue(conn)?),
4040
other => Err(anyhow!("Unrecognized job type `{}`", other)),
4141
}
4242
}

src/bin/monitor.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,9 @@ use diesel::prelude::*;
1313
fn main() -> Result<()> {
1414
let conn = db::oneoff_connection()?;
1515

16-
check_failing_background_jobs(&conn)?;
17-
check_stalled_update_downloads(&conn)?;
18-
check_spam_attack(&conn)?;
16+
check_failing_background_jobs(conn)?;
17+
check_stalled_update_downloads(conn)?;
18+
check_spam_attack(conn)?;
1919
Ok(())
2020
}
2121

@@ -27,7 +27,7 @@ fn main() -> Result<()> {
2727
///
2828
/// Within the default 15 minute time, a job should have already had several
2929
/// failed retry attempts.
30-
fn check_failing_background_jobs(conn: &PgConnection) -> Result<()> {
30+
fn check_failing_background_jobs(conn: &mut PgConnection) -> Result<()> {
3131
use cargo_registry::schema::background_jobs::dsl::*;
3232
use diesel::dsl::*;
3333
use diesel::sql_types::Integer;
@@ -69,7 +69,7 @@ fn check_failing_background_jobs(conn: &PgConnection) -> Result<()> {
6969
}
7070

7171
/// Check for an `update_downloads` job that has run longer than expected
72-
fn check_stalled_update_downloads(conn: &PgConnection) -> Result<()> {
72+
fn check_stalled_update_downloads(conn: &mut PgConnection) -> Result<()> {
7373
use cargo_registry::schema::background_jobs::dsl::*;
7474
use chrono::{DateTime, NaiveDateTime, Utc};
7575

@@ -106,7 +106,7 @@ fn check_stalled_update_downloads(conn: &PgConnection) -> Result<()> {
106106
}
107107

108108
/// Check for known spam patterns
109-
fn check_spam_attack(conn: &PgConnection) -> Result<()> {
109+
fn check_spam_attack(conn: &mut PgConnection) -> Result<()> {
110110
use cargo_registry::sql::canon_crate_name;
111111
use diesel::dsl::*;
112112

0 commit comments

Comments
 (0)