Skip to content

Commit 7876309

Browse files
authored
Merge pull request #4892 from jtgeibel/diesel2
Port to diesel 2.0.3
2 parents ee268a8 + 1339e7a commit 7876309

Some content is hidden

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

86 files changed

+883
-892
lines changed

.github/workflows/ci.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ concurrency:
1414

1515
env:
1616
# renovate: datasource=crate depName=diesel_cli versioning=semver
17-
DIESEL_CLI_VERSION: 1.4.1
17+
DIESEL_CLI_VERSION: 2.0.1
1818
# renovate: datasource=npm depName=pnpm
1919
PNPM_VERSION: 7.27.0
2020
# renovate: datasource=github-releases depName=rust lookupName=rust-lang/rust

.github/workflows/coverage.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ env:
1010
# renovate: datasource=crate depName=cargo-tarpaulin versioning=semver
1111
CARGO_TARPAULIN_VERSION: 0.25.0
1212
# renovate: datasource=crate depName=diesel_cli versioning=semver
13-
DIESEL_CLI_VERSION: 1.4.1
13+
DIESEL_CLI_VERSION: 2.0.1
1414
# renovate: datasource=github-releases depName=rust lookupName=rust-lang/rust
1515
RUST_VERSION: 1.67.1
1616

Cargo.lock

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

Cargo.toml

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,9 @@ cookie = { version = "=0.17.0", features = ["secure"] }
3636
dashmap = { version = "=5.4.0", features = ["raw-api"] }
3737
derive_deref = "=1.1.1"
3838
dialoguer = "=0.10.3"
39-
diesel = { version = "=1.4.8", features = ["postgres", "serde_json", "chrono", "r2d2"] }
40-
diesel_full_text_search = "=1.0.1"
41-
diesel_migrations = { version = "=1.4.0", features = ["postgres"] }
39+
diesel = { version = "2", features = ["postgres", "serde_json", "chrono", "r2d2"] }
40+
diesel_full_text_search = "2"
41+
diesel_migrations = { version = "2", features = ["postgres"] }
4242
dotenv = "=0.15.0"
4343
flate2 = "=1.0.25"
4444
futures-channel = { version = "=0.3.26", default-features = false }
@@ -90,6 +90,6 @@ tokio = "=1.25.0"
9090
tower-service = "=0.3.2"
9191

9292
[build-dependencies]
93-
diesel = { version = "=1.4.8", features = ["postgres"] }
94-
diesel_migrations = { version = "=1.4.0", features = ["postgres"] }
93+
diesel = { version = "2", features = ["postgres"] }
94+
diesel_migrations = { version = "2", features = ["postgres"] }
9595
dotenv = "=0.15.0"

backend.Dockerfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ ARG RUST_VERSION=1.67.1
44
FROM rust:$RUST_VERSION
55

66
# renovate: datasource=crate depName=diesel_cli versioning=semver
7-
ARG DIESEL_CLI_VERSION=1.4.1
7+
ARG DIESEL_CLI_VERSION=2.0.1
88

99
RUN apt-get update \
1010
&& apt-get install -y postgresql \

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
}

diesel.toml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,4 @@
33
[print_schema]
44
file = "src/schema.rs"
55
with_docs = true
6-
import_types = ["diesel::sql_types::*", "diesel_full_text_search::{TsVector as Tsvector}"]
76
patch_file = "src/schema.patch"

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/enqueue_job.rs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -26,30 +26,30 @@ pub enum Command {
2626
}
2727

2828
pub fn run(command: Command) -> Result<()> {
29-
let conn = db::oneoff_connection()?;
29+
let conn = &mut db::oneoff_connection()?;
3030
println!("Enqueueing background job: {command:?}");
3131

3232
match command {
3333
Command::UpdateDownloads => {
3434
let count: i64 = background_jobs
3535
.filter(job_type.eq("update_downloads"))
3636
.count()
37-
.get_result(&conn)
37+
.get_result(conn)
3838
.unwrap();
3939

4040
if count > 0 {
4141
println!("Did not enqueue update_downloads, existing job already in progress");
4242
Ok(())
4343
} else {
44-
Ok(worker::update_downloads().enqueue(&conn)?)
44+
Ok(worker::update_downloads().enqueue(conn)?)
4545
}
4646
}
4747
Command::DumpDb {
4848
database_url,
4949
target_name,
50-
} => Ok(worker::dump_db(database_url, target_name).enqueue(&conn)?),
51-
Command::DailyDbMaintenance => Ok(worker::daily_db_maintenance().enqueue(&conn)?),
52-
Command::SquashIndex => Ok(worker::squash_index().enqueue(&conn)?),
53-
Command::NormalizeIndex { dry_run } => Ok(worker::normalize_index(dry_run).enqueue(&conn)?),
50+
} => Ok(worker::dump_db(database_url, target_name).enqueue(conn)?),
51+
Command::DailyDbMaintenance => Ok(worker::daily_db_maintenance().enqueue(conn)?),
52+
Command::SquashIndex => Ok(worker::squash_index().enqueue(conn)?),
53+
Command::NormalizeIndex { dry_run } => Ok(worker::normalize_index(dry_run).enqueue(conn)?),
5454
}
5555
}

src/admin/git_import.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ pub struct Opts {
2828
}
2929

3030
pub fn run(opts: Opts) -> anyhow::Result<()> {
31-
let conn = db::oneoff_connection().unwrap();
31+
let mut conn = db::oneoff_connection().unwrap();
3232
println!("fetching git repo");
3333
let config = RepositoryConfig::from_environment();
3434
let repo = Repository::open(&config)?;
@@ -53,10 +53,10 @@ pub fn run(opts: Opts) -> anyhow::Result<()> {
5353
}
5454
let file = File::open(path)?;
5555
let reader = BufReader::new(file);
56-
let result = conn.transaction(|| -> anyhow::Result<()> {
56+
let result = conn.transaction(|conn| -> anyhow::Result<()> {
5757
for line in reader.lines() {
5858
let krate: cargo_registry_index::Crate = serde_json::from_str(&line?)?;
59-
import_data(&conn, &krate).with_context(|| {
59+
import_data(conn, &krate).with_context(|| {
6060
format!("Failed to update crate {}#{}", krate.name, krate.vers)
6161
})?
6262
}
@@ -71,7 +71,7 @@ pub fn run(opts: Opts) -> anyhow::Result<()> {
7171
Ok(())
7272
}
7373

74-
fn import_data(conn: &PgConnection, krate: &cargo_registry_index::Crate) -> anyhow::Result<()> {
74+
fn import_data(conn: &mut PgConnection, krate: &cargo_registry_index::Crate) -> anyhow::Result<()> {
7575
let version_id: i32 = versions::table
7676
.inner_join(crates::table)
7777
.filter(crates::name.eq(&krate.name))

src/admin/migrate.rs

Lines changed: 12 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
#[command(
@@ -32,13 +36,17 @@ 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
info!("Migrating the database");
38-
embedded_migrations::run_with_output(&conn, &mut std::io::stdout())?;
42+
let mut stdout = std::io::stdout();
43+
let mut harness = HarnessWithOutput::new(conn, &mut stdout);
44+
harness
45+
.run_pending_migrations(MIGRATIONS)
46+
.expect("failed to run migrations");
3947

4048
info!("Synchronizing crate categories");
41-
crate::boot::categories::sync_with_connection(CATEGORIES_TOML, &conn).unwrap();
49+
crate::boot::categories::sync_with_connection(CATEGORIES_TOML, conn).unwrap();
4250

4351
Ok(())
4452
}

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);
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();

0 commit comments

Comments
 (0)