Skip to content

Port to diesel 2.0.3 #4892

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 10 commits into from
Feb 17, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ concurrency:

env:
# renovate: datasource=crate depName=diesel_cli versioning=semver
DIESEL_CLI_VERSION: 1.4.1
DIESEL_CLI_VERSION: 2.0.1
# renovate: datasource=npm depName=pnpm
PNPM_VERSION: 7.27.0
# renovate: datasource=github-releases depName=rust lookupName=rust-lang/rust
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/coverage.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ env:
# renovate: datasource=crate depName=cargo-tarpaulin versioning=semver
CARGO_TARPAULIN_VERSION: 0.25.0
# renovate: datasource=crate depName=diesel_cli versioning=semver
DIESEL_CLI_VERSION: 1.4.1
DIESEL_CLI_VERSION: 2.0.1
# renovate: datasource=github-releases depName=rust lookupName=rust-lang/rust
RUST_VERSION: 1.67.1

Expand Down
42 changes: 27 additions & 15 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 5 additions & 5 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,9 @@ cookie = { version = "=0.17.0", features = ["secure"] }
dashmap = { version = "=5.4.0", features = ["raw-api"] }
derive_deref = "=1.1.1"
dialoguer = "=0.10.3"
diesel = { version = "=1.4.8", features = ["postgres", "serde_json", "chrono", "r2d2"] }
diesel_full_text_search = "=1.0.1"
diesel_migrations = { version = "=1.4.0", features = ["postgres"] }
diesel = { version = "2", features = ["postgres", "serde_json", "chrono", "r2d2"] }
diesel_full_text_search = "2"
diesel_migrations = { version = "2", features = ["postgres"] }
dotenv = "=0.15.0"
flate2 = "=1.0.25"
futures-channel = { version = "=0.3.26", default-features = false }
Expand Down Expand Up @@ -90,6 +90,6 @@ tokio = "=1.25.0"
tower-service = "=0.3.2"

[build-dependencies]
diesel = { version = "=1.4.8", features = ["postgres"] }
diesel_migrations = { version = "=1.4.0", features = ["postgres"] }
diesel = { version = "2", features = ["postgres"] }
diesel_migrations = { version = "2", features = ["postgres"] }
dotenv = "=0.15.0"
2 changes: 1 addition & 1 deletion backend.Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ ARG RUST_VERSION=1.67.1
FROM rust:$RUST_VERSION

# renovate: datasource=crate depName=diesel_cli versioning=semver
ARG DIESEL_CLI_VERSION=1.4.1
ARG DIESEL_CLI_VERSION=2.0.1

RUN apt-get update \
&& apt-get install -y postgresql \
Expand Down
10 changes: 7 additions & 3 deletions build.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use diesel::prelude::*;
use diesel_migrations::run_pending_migrations;
use diesel_migrations::{FileBasedMigrations, MigrationHarness};
use std::env;

fn main() {
Expand All @@ -8,9 +8,13 @@ fn main() {
println!("cargo:rerun-if-changed=migrations/");
if env::var("PROFILE") == Ok("debug".into()) {
if let Ok(database_url) = dotenv::var("TEST_DATABASE_URL") {
let connection = PgConnection::establish(&database_url)
let connection = &mut PgConnection::establish(&database_url)
.expect("Could not connect to TEST_DATABASE_URL");
run_pending_migrations(&connection).expect("Error running migrations");
let migrations = FileBasedMigrations::find_migrations_directory()
.expect("Could not find migrations");
connection
.run_pending_migrations(migrations)
.expect("Error running migrations");
}
}
}
1 change: 0 additions & 1 deletion diesel.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,4 @@
[print_schema]
file = "src/schema.rs"
with_docs = true
import_types = ["diesel::sql_types::*", "diesel_full_text_search::{TsVector as Tsvector}"]
patch_file = "src/schema.patch"
8 changes: 4 additions & 4 deletions src/admin/delete_crate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,15 @@ pub struct Opts {
}

pub fn run(opts: Opts) {
let conn = db::oneoff_connection().unwrap();
conn.transaction::<_, diesel::result::Error, _>(|| {
delete(opts, &conn);
let conn = &mut db::oneoff_connection().unwrap();
conn.transaction::<_, diesel::result::Error, _>(|conn| {
delete(opts, conn);
Ok(())
})
.unwrap()
}

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

let config = config::Base::from_environment();
Expand Down
8 changes: 4 additions & 4 deletions src/admin/delete_version.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,15 @@ pub struct Opts {
}

pub fn run(opts: Opts) {
let conn = db::oneoff_connection().unwrap();
conn.transaction::<_, diesel::result::Error, _>(|| {
delete(opts, &conn);
let conn = &mut db::oneoff_connection().unwrap();
conn.transaction::<_, diesel::result::Error, _>(|conn| {
delete(opts, conn);
Ok(())
})
.unwrap()
}

fn delete(opts: Opts, conn: &PgConnection) {
fn delete(opts: Opts, conn: &mut PgConnection) {
let krate: Crate = Crate::by_name(&opts.crate_name).first(conn).unwrap();
let v: Version = Version::belonging_to(&krate)
.filter(versions::num.eq(&opts.version))
Expand Down
14 changes: 7 additions & 7 deletions src/admin/enqueue_job.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,30 +26,30 @@ pub enum Command {
}

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

match command {
Command::UpdateDownloads => {
let count: i64 = background_jobs
.filter(job_type.eq("update_downloads"))
.count()
.get_result(&conn)
.get_result(conn)
.unwrap();

if count > 0 {
println!("Did not enqueue update_downloads, existing job already in progress");
Ok(())
} else {
Ok(worker::update_downloads().enqueue(&conn)?)
Ok(worker::update_downloads().enqueue(conn)?)
}
}
Command::DumpDb {
database_url,
target_name,
} => Ok(worker::dump_db(database_url, target_name).enqueue(&conn)?),
Command::DailyDbMaintenance => Ok(worker::daily_db_maintenance().enqueue(&conn)?),
Command::SquashIndex => Ok(worker::squash_index().enqueue(&conn)?),
Command::NormalizeIndex { dry_run } => Ok(worker::normalize_index(dry_run).enqueue(&conn)?),
} => Ok(worker::dump_db(database_url, target_name).enqueue(conn)?),
Command::DailyDbMaintenance => Ok(worker::daily_db_maintenance().enqueue(conn)?),
Command::SquashIndex => Ok(worker::squash_index().enqueue(conn)?),
Command::NormalizeIndex { dry_run } => Ok(worker::normalize_index(dry_run).enqueue(conn)?),
}
}
8 changes: 4 additions & 4 deletions src/admin/git_import.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ pub struct Opts {
}

pub fn run(opts: Opts) -> anyhow::Result<()> {
let conn = db::oneoff_connection().unwrap();
let mut conn = db::oneoff_connection().unwrap();
println!("fetching git repo");
let config = RepositoryConfig::from_environment();
let repo = Repository::open(&config)?;
Expand All @@ -53,10 +53,10 @@ pub fn run(opts: Opts) -> anyhow::Result<()> {
}
let file = File::open(path)?;
let reader = BufReader::new(file);
let result = conn.transaction(|| -> anyhow::Result<()> {
let result = conn.transaction(|conn| -> anyhow::Result<()> {
for line in reader.lines() {
let krate: cargo_registry_index::Crate = serde_json::from_str(&line?)?;
import_data(&conn, &krate).with_context(|| {
import_data(conn, &krate).with_context(|| {
format!("Failed to update crate {}#{}", krate.name, krate.vers)
})?
}
Expand All @@ -71,7 +71,7 @@ pub fn run(opts: Opts) -> anyhow::Result<()> {
Ok(())
}

fn import_data(conn: &PgConnection, krate: &cargo_registry_index::Crate) -> anyhow::Result<()> {
fn import_data(conn: &mut PgConnection, krate: &cargo_registry_index::Crate) -> anyhow::Result<()> {
let version_id: i32 = versions::table
.inner_join(crates::table)
.filter(crates::name.eq(&krate.name))
Expand Down
16 changes: 12 additions & 4 deletions src/admin/migrate.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
use anyhow::Error;
use diesel_migrations::{
embed_migrations, EmbeddedMigrations, HarnessWithOutput, MigrationHarness,
};

static CATEGORIES_TOML: &str = include_str!("../boot/categories.toml");
diesel_migrations::embed_migrations!("./migrations");

pub const MIGRATIONS: EmbeddedMigrations = embed_migrations!("./migrations");

#[derive(clap::Parser, Debug, Copy, Clone)]
#[command(
Expand Down Expand Up @@ -32,13 +36,17 @@ pub fn run(_opts: Opts) -> Result<(), Error> {
}

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

info!("Migrating the database");
embedded_migrations::run_with_output(&conn, &mut std::io::stdout())?;
let mut stdout = std::io::stdout();
let mut harness = HarnessWithOutput::new(conn, &mut stdout);
harness
.run_pending_migrations(MIGRATIONS)
.expect("failed to run migrations");

info!("Synchronizing crate categories");
crate::boot::categories::sync_with_connection(CATEGORIES_TOML, &conn).unwrap();
crate::boot::categories::sync_with_connection(CATEGORIES_TOML, conn).unwrap();

Ok(())
}
6 changes: 3 additions & 3 deletions src/admin/populate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,11 @@ pub struct Opts {
}

pub fn run(opts: Opts) {
let conn = db::oneoff_connection().unwrap();
conn.transaction(|| update(opts, &conn)).unwrap();
let mut conn = db::oneoff_connection().unwrap();
conn.transaction(|conn| update(opts, conn)).unwrap();
}

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

for id in opts.version_ids {
Expand Down
12 changes: 6 additions & 6 deletions src/admin/render_readmes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use std::{io::Read, path::Path, sync::Arc, thread};

use cargo_registry_markdown::text_to_html;
use chrono::{TimeZone, Utc};
use diesel::{dsl::any, prelude::*};
use diesel::prelude::*;
use flate2::read::GzDecoder;
use reqwest::{blocking::Client, header};
use tar::{self, Archive};
Expand Down Expand Up @@ -39,7 +39,7 @@ pub struct Opts {

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

let start_time = Utc::now();

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

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

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

let versions: Vec<(Version, String)> = versions::table
.inner_join(crates::table)
.filter(versions::id.eq(any(version_ids_chunk)))
.filter(versions::id.eq_any(version_ids_chunk))
.select((versions::all_columns, crates::name))
.load(&conn)
.load(conn)
.expect("error loading versions");

let mut tasks = Vec::with_capacity(page_size);
for (version, krate_name) in versions {
Version::record_readme_rendering(version.id, &conn)
Version::record_readme_rendering(version.id, conn)
.context("Couldn't record rendering time")?;

let client = client.clone();
Expand Down
Loading