diff --git a/src/build_queue.rs b/src/build_queue.rs index b8cbe6116..14066ff57 100644 --- a/src/build_queue.rs +++ b/src/build_queue.rs @@ -283,8 +283,8 @@ mod tests { let queue = env.build_queue(); let test_crates = [ - ("foo", "1.0.0", -10), ("bar", "1.0.0", 0), + ("foo", "1.0.0", -10), ("baz", "1.0.0", 10), ]; for krate in &test_crates { @@ -293,26 +293,15 @@ mod tests { assert_eq!( vec![ - QueuedCrate { - id: 1, - name: "foo".into(), - version: "1.0.0".into(), - priority: -10, - }, - QueuedCrate { - id: 2, - name: "bar".into(), - version: "1.0.0".into(), - priority: 0, - }, - QueuedCrate { - id: 3, - name: "baz".into(), - version: "1.0.0".into(), - priority: 10, - }, + ("foo", "1.0.0", -10), + ("bar", "1.0.0", 0), + ("baz", "1.0.0", 10), ], - queue.queued_crates()? + queue + .queued_crates()? + .iter() + .map(|c| (c.name.as_str(), c.version.as_str(), c.priority)) + .collect::>() ); Ok(()) diff --git a/src/db/delete.rs b/src/db/delete.rs index d73210872..26c71137b 100644 --- a/src/db/delete.rs +++ b/src/db/delete.rs @@ -49,9 +49,8 @@ fn get_id(conn: &Connection, name: &str) -> Result { // metaprogramming! // WARNING: these must be hard-coded and NEVER user input. -const METADATA: [(&str, &str); 5] = [ +const METADATA: &[(&str, &str)] = &[ ("author_rels", "rid"), - ("owner_rels", "cid"), ("keyword_rels", "rid"), ("builds", "rid"), ("compression_rels", "release"), @@ -60,7 +59,7 @@ const METADATA: [(&str, &str); 5] = [ fn delete_version_from_database(conn: &Connection, name: &str, version: &str) -> Result<(), Error> { let crate_id = get_id(conn, name)?; let transaction = conn.transaction()?; - for &(table, column) in &METADATA { + for &(table, column) in METADATA { transaction.execute( &format!("DELETE FROM {} WHERE {} IN (SELECT id FROM releases WHERE crate_id = $1 AND version = $2)", table, column), &[&crate_id, &version], @@ -96,7 +95,7 @@ fn delete_crate_from_database(conn: &Connection, name: &str, crate_id: i32) -> R "DELETE FROM sandbox_overrides WHERE crate_name = $1", &[&name], )?; - for &(table, column) in &METADATA { + for &(table, column) in METADATA { transaction.execute( &format!( "DELETE FROM {} WHERE {} IN (SELECT id FROM releases WHERE crate_id = $1)", @@ -105,6 +104,7 @@ fn delete_crate_from_database(conn: &Connection, name: &str, crate_id: i32) -> R &[&crate_id], )?; } + transaction.execute("DELETE FROM owner_rels WHERE cid = $1;", &[&crate_id])?; transaction.execute("DELETE FROM releases WHERE crate_id = $1;", &[&crate_id])?; transaction.execute("DELETE FROM crates WHERE id = $1;", &[&crate_id])?; diff --git a/src/db/migrate.rs b/src/db/migrate.rs index 2937bb29f..e4c6f41ae 100644 --- a/src/db/migrate.rs +++ b/src/db/migrate.rs @@ -364,6 +364,22 @@ pub fn migrate(version: Option, conn: &Connection) -> CratesfyiResult<( "DROP TABLE compression_rels; ALTER TABLE files DROP COLUMN compression;" ), + migration!( + context, + // version + 15, + // description + "Fix owner_rels.cid foreign key reference", + // upgrade query + " + ALTER TABLE owner_rels DROP CONSTRAINT owner_rels_cid_fkey; + ALTER TABLE owner_rels ADD FOREIGN KEY (cid) REFERENCES crates(id); + ", + // downgrade query + " + -- Nope, this is a pure database fix, no going back. + " + ), ]; for migration in migrations { diff --git a/src/test/mod.rs b/src/test/mod.rs index ad96bd577..f2f315f72 100644 --- a/src/test/mod.rs +++ b/src/test/mod.rs @@ -239,6 +239,29 @@ impl TestDatabase { ))?; crate::db::migrate(None, &conn)?; + // Move all sequence start positions 10000 apart to avoid overlapping primary keys + let query: String = conn + .query( + " + SELECT relname + FROM pg_class + INNER JOIN pg_namespace + ON pg_class.relnamespace = pg_namespace.oid + WHERE pg_class.relkind = 'S' + AND pg_namespace.nspname = $1 + ", + &[&schema], + )? + .into_iter() + .map(|row| row.get(0)) + .enumerate() + .map(|(i, sequence): (_, String)| { + let offset = (i + 1) * 10000; + format!(r#"ALTER SEQUENCE "{}" RESTART WITH {};"#, sequence, offset) + }) + .collect(); + conn.batch_execute(&query)?; + Ok(TestDatabase { pool: Pool::new_with_schema(config, &schema)?, schema,