Skip to content

Commit b24270e

Browse files
committed
More fixes
1 parent 68a8814 commit b24270e

File tree

14 files changed

+34
-35
lines changed

14 files changed

+34
-35
lines changed

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: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,9 @@ pub fn run(_opts: Opts) -> Result<(), Error> {
4242
// let migrations = ;
4343
let mut stdout = std::io::stdout();
4444
let mut harness = HarnessWithOutput::new(conn, &mut stdout);
45-
harness.run_pending_migrations(MIGRATIONS);
45+
harness
46+
.run_pending_migrations(MIGRATIONS)
47+
.expect("failed to run migrations");
4648

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

src/admin/yank_version.rs

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

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

34-
fn yank(opts: Opts, conn: &PgConnection) {
34+
fn yank(opts: Opts, conn: &mut PgConnection) {
3535
let Opts {
3636
crate_name,
3737
version,

src/bin/enqueue-job.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ use cargo_registry::{db, env, worker};
66
use diesel::prelude::*;
77

88
fn main() -> Result<()> {
9-
let conn = db::oneoff_connection()?;
9+
let conn = &mut db::oneoff_connection()?;
1010
let mut args = std::env::args().skip(1);
1111

1212
let job = args.next().unwrap_or_default();

src/bin/monitor.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ use cargo_registry::{admin::on_call, db, schema::*};
1111
use diesel::prelude::*;
1212

1313
fn main() -> Result<()> {
14-
let conn = db::oneoff_connection()?;
14+
let conn = &mut db::oneoff_connection()?;
1515

1616
check_failing_background_jobs(conn)?;
1717
check_stalled_update_downloads(conn)?;
@@ -108,7 +108,6 @@ fn check_stalled_update_downloads(conn: &mut PgConnection) -> Result<()> {
108108
/// Check for known spam patterns
109109
fn check_spam_attack(conn: &mut PgConnection) -> Result<()> {
110110
use cargo_registry::sql::canon_crate_name;
111-
use diesel::dsl::*;
112111

113112
const EVENT_KEY: &str = "spam_attack";
114113

@@ -123,7 +122,7 @@ fn check_spam_attack(conn: &mut PgConnection) -> Result<()> {
123122
let mut event_description = None;
124123

125124
let bad_crate: Option<String> = crates::table
126-
.filter(canon_crate_name(crates::name).eq(any(bad_crate_names)))
125+
.filter(canon_crate_name(crates::name).eq_any(bad_crate_names))
127126
.select(crates::name)
128127
.first(conn)
129128
.optional()?;

src/controllers/category.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,7 @@ pub fn index(req: &mut dyn RequestExt) -> EndpointResult {
1616
let sort = query.get("sort").map_or("alpha", String::as_str);
1717

1818
let conn = &mut req.app().db_read()?;
19-
let categories =
20-
Category::toplevel(conn, sort, i64::from(options.per_page), i64::from(offset))?;
19+
let categories = Category::toplevel(conn, sort, options.per_page, offset)?;
2120
let categories = categories
2221
.into_iter()
2322
.map(Category::into)

src/models/krate.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -426,8 +426,8 @@ impl Crate {
426426
let rows: Vec<WithCount<ReverseDependency>> =
427427
sql_query(include_str!("krate_reverse_dependencies.sql"))
428428
.bind::<Integer, _>(self.id)
429-
.bind::<BigInt, _>(i64::from(offset))
430-
.bind::<BigInt, _>(i64::from(options.per_page))
429+
.bind::<BigInt, _>(offset)
430+
.bind::<BigInt, _>(options.per_page)
431431
.load(conn)?;
432432

433433
Ok(rows.records_and_total())

src/tests/all.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,6 @@
33
#[macro_use]
44
extern crate claims;
55
#[macro_use]
6-
extern crate diesel;
7-
#[macro_use]
86
extern crate serde;
97
#[macro_use]
108
extern crate serde_json;

src/tests/builders/krate.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ impl<'a> CrateBuilder<'a> {
111111
self
112112
}
113113

114-
pub fn build(mut self, connection: &PgConnection) -> AppResult<Crate> {
114+
pub fn build(mut self, connection: &mut PgConnection) -> AppResult<Crate> {
115115
use diesel::{insert_into, select, update};
116116

117117
let mut krate = self
@@ -147,8 +147,8 @@ impl<'a> CrateBuilder<'a> {
147147
))
148148
.execute(connection)?;
149149

150-
no_arg_sql_function!(refresh_recent_crate_downloads, ());
151-
select(refresh_recent_crate_downloads).execute(connection)?;
150+
sql_function!(fn refresh_recent_crate_downloads());
151+
select(refresh_recent_crate_downloads()).execute(connection)?;
152152
}
153153

154154
if !self.categories.is_empty() {
@@ -175,7 +175,7 @@ impl<'a> CrateBuilder<'a> {
175175
///
176176
/// Panics (and fails the test) if any part of inserting the crate record fails.
177177
#[track_caller]
178-
pub fn expect_build(self, connection: &PgConnection) -> Crate {
178+
pub fn expect_build(self, connection: &mut PgConnection) -> Crate {
179179
let name = self.krate.name;
180180
self.build(connection).unwrap_or_else(|e| {
181181
panic!("Unable to create crate {name}: {e:?}");

src/tests/builders/version.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ impl<'a> VersionBuilder<'a> {
8787
self,
8888
crate_id: i32,
8989
published_by: i32,
90-
connection: &PgConnection,
90+
connection: &mut PgConnection,
9191
) -> AppResult<Version> {
9292
use diesel::{insert_into, update};
9393

@@ -150,7 +150,7 @@ impl<'a> VersionBuilder<'a> {
150150
self,
151151
crate_id: i32,
152152
published_by: i32,
153-
connection: &PgConnection,
153+
connection: &mut PgConnection,
154154
) -> Version {
155155
self.build(crate_id, published_by, connection)
156156
.unwrap_or_else(|e| {

src/tests/categories.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ description = "Another category ho hum"
4040
fn pg_connection() -> PgConnection {
4141
let database_url =
4242
dotenv::var("TEST_DATABASE_URL").expect("TEST_DATABASE_URL must be set to run tests");
43-
let conn = PgConnection::establish(&database_url).unwrap();
43+
let mut conn = PgConnection::establish(&database_url).unwrap();
4444
conn.begin_test_transaction().unwrap();
4545
conn
4646
}

src/tests/server_binary.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ const SERVER_BOOT_TIMEOUT_SECONDS: u64 = 30;
1616
#[test]
1717
fn normal_startup() {
1818
let server_bin = ServerBin::prepare().unwrap();
19-
initialize_dummy_crate(&server_bin.db().unwrap());
19+
initialize_dummy_crate(&mut server_bin.db().unwrap());
2020

2121
let running_server = server_bin.start().unwrap();
2222

@@ -37,7 +37,7 @@ fn normal_startup() {
3737
#[test]
3838
fn startup_without_database() {
3939
let server_bin = ServerBin::prepare().unwrap();
40-
initialize_dummy_crate(&server_bin.db().unwrap());
40+
initialize_dummy_crate(&mut server_bin.db().unwrap());
4141

4242
// Break the networking *before* starting the binary, to ensure the binary can fully startup
4343
// without a database connection. Most of crates.io should not work when started without a

src/tests/util/fresh_schema.rs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use diesel::connection::SimpleConnection;
22
use diesel::prelude::*;
3-
use diesel_migrations::{find_migrations_directory, run_pending_migrations_in_directory};
3+
use diesel_migrations::{FileBasedMigrations, MigrationHarness};
44
use rand::Rng;
55

66
pub(crate) struct FreshSchema {
@@ -13,7 +13,7 @@ impl FreshSchema {
1313
pub(crate) fn new(database_url: &str) -> Self {
1414
let schema_name = generate_schema_name();
1515

16-
let conn = PgConnection::establish(database_url).expect("can't connect to the test db");
16+
let mut conn = PgConnection::establish(database_url).expect("can't connect to the test db");
1717
conn.batch_execute(&format!(
1818
"
1919
DROP SCHEMA IF EXISTS {schema_name} CASCADE;
@@ -23,8 +23,9 @@ impl FreshSchema {
2323
))
2424
.expect("failed to initialize schema");
2525

26-
let migrations_dir = find_migrations_directory().unwrap();
27-
run_pending_migrations_in_directory(conn, &migrations_dir, &mut std::io::sink())
26+
let migrations =
27+
FileBasedMigrations::find_migrations_directory().expect("Could not find migrations");
28+
conn.run_pending_migrations(migrations)
2829
.expect("failed to run migrations on the test schema");
2930

3031
let database_url = url::Url::parse_with_params(

src/tests/util/test_app.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ impl Drop for TestAppInner {
4747

4848
// Manually verify that all jobs have completed successfully
4949
// This will catch any tests that enqueued a job but forgot to initialize the runner
50-
let conn = self.app.primary_database.get().unwrap();
50+
let conn = &mut *self.app.primary_database.get().unwrap();
5151
let job_count: i64 = background_jobs.count().get_result(conn).unwrap();
5252
assert_eq!(
5353
0, job_count,
@@ -93,8 +93,8 @@ impl TestApp {
9393
/// connection before making any API calls. Once the closure returns, the connection is
9494
/// dropped, ensuring it is returned to the pool and available for any future API calls.
9595
pub fn db<T, F: FnOnce(&mut PgConnection) -> T>(&self, f: F) -> T {
96-
let conn = self.0.app.primary_database.get().unwrap();
97-
f(&mut conn)
96+
let conn = &mut self.0.app.primary_database.get().unwrap();
97+
f(conn)
9898
}
9999

100100
/// Create a new user with a verified email address in the database and return a mock user

0 commit comments

Comments
 (0)