diff --git a/radicle-cli/tests/commands.rs b/radicle-cli/tests/commands.rs index aff89a8c..c76e29b0 100644 --- a/radicle-cli/tests/commands.rs +++ b/radicle-cli/tests/commands.rs @@ -854,8 +854,6 @@ fn rad_clone_connect() { let mut bob = bob.spawn(); // Let Eve know about Alice and Bob having the repo. - eve.db.routing_mut().insert([&acme], alice.id, now).unwrap(); - eve.db.routing_mut().insert([&acme], bob.id, now).unwrap(); eve.db .addresses_mut() .insert( @@ -884,6 +882,8 @@ fn rad_clone_connect() { )], ) .unwrap(); + eve.db.routing_mut().insert([&acme], alice.id, now).unwrap(); + eve.db.routing_mut().insert([&acme], bob.id, now).unwrap(); eve.config.peers = node::config::PeerConfig::Static; let eve = eve.spawn(); diff --git a/radicle/src/node/db/schema.sql b/radicle/src/node/db/schema.sql index 2655eb84..d1a62f1f 100644 --- a/radicle/src/node/db/schema.sql +++ b/radicle/src/node/db/schema.sql @@ -46,8 +46,7 @@ create table if not exists "routing" ( -- Repository being seeded. "repo" text not null, -- Node ID. - -- TODO: Add foreign-key constraint. - "node" text not null, + "node" text not null references "nodes" ("id") on delete cascade, -- UNIX time at which this entry was added or refreshed. "timestamp" integer not null, @@ -57,7 +56,9 @@ create table if not exists "routing" ( -- Gossip message store. create table if not exists "announcements" ( -- Node ID. - -- TODO: Add foreign-key constraint. + -- + -- Nb. We don't use a foreign key constraint here, because announcements are + -- currently added to the database before nodes. "node" text not null, -- Repo ID, if any, for example in ref announcements. -- For other announcement types, this should be an empty string. diff --git a/radicle/src/node/routing.rs b/radicle/src/node/routing.rs index 1324c3ca..91ab5ef3 100644 --- a/radicle/src/node/routing.rs +++ b/radicle/src/node/routing.rs @@ -228,11 +228,19 @@ mod test { use super::*; use crate::test::arbitrary; + fn database(path: &str) -> Database { + let db = Database::open(path).unwrap(); + + // We don't want to test foreign key constraints here. + db.db.execute("PRAGMA foreign_keys = OFF").unwrap(); + db + } + #[test] fn test_insert_and_get() { let ids = arbitrary::set::(5..10); let nodes = arbitrary::set::(5..10); - let mut db = Database::open(":memory:").unwrap(); + let mut db = database(":memory:"); for node in &nodes { assert_eq!( @@ -255,7 +263,7 @@ mod test { fn test_insert_and_get_resources() { let ids = arbitrary::set::(5..10); let nodes = arbitrary::set::(5..10); - let mut db = Database::open(":memory:").unwrap(); + let mut db = database(":memory:"); for node in &nodes { db.insert(&ids, *node, 0).unwrap(); @@ -273,7 +281,7 @@ mod test { fn test_entries() { let ids = arbitrary::set::(6..9); let nodes = arbitrary::set::(6..9); - let mut db = Database::open(":memory:").unwrap(); + let mut db = database(":memory:"); for node in &nodes { assert!(db @@ -296,7 +304,7 @@ mod test { fn test_insert_and_remove() { let ids = arbitrary::set::(5..10); let nodes = arbitrary::set::(5..10); - let mut db = Database::open(":memory:").unwrap(); + let mut db = database(":memory:"); for node in &nodes { db.insert(&ids, *node, 0).unwrap(); @@ -315,7 +323,7 @@ mod test { fn test_insert_duplicate() { let id = arbitrary::gen::(1); let node = arbitrary::gen::(1); - let mut db = Database::open(":memory:").unwrap(); + let mut db = database(":memory:"); assert_eq!( db.insert([&id], node, 0).unwrap(), @@ -335,7 +343,7 @@ mod test { fn test_insert_existing_updated_time() { let id = arbitrary::gen::(1); let node = arbitrary::gen::(1); - let mut db = Database::open(":memory:").unwrap(); + let mut db = database(":memory:"); assert_eq!( db.insert([&id], node, 0).unwrap(), @@ -353,7 +361,7 @@ mod test { let id1 = arbitrary::gen::(1); let id2 = arbitrary::gen::(1); let node = arbitrary::gen::(1); - let mut db = Database::open(":memory:").unwrap(); + let mut db = database(":memory:"); assert_eq!( db.insert([&id1], node, 0).unwrap(), @@ -379,7 +387,7 @@ mod test { fn test_remove_redundant() { let id = arbitrary::gen::(1); let node = arbitrary::gen::(1); - let mut db = Database::open(":memory:").unwrap(); + let mut db = database(":memory:"); assert_eq!( db.insert([&id], node, 0).unwrap(), @@ -391,7 +399,7 @@ mod test { #[test] fn test_len() { - let mut db = Database::open(":memory:").unwrap(); + let mut db = database(":memory:"); let ids = arbitrary::vec::(10); let node = arbitrary::gen(1); @@ -406,7 +414,7 @@ mod test { let now = LocalTime::now(); let ids = arbitrary::vec::(10); let nodes = arbitrary::vec::(10); - let mut db = Database::open(":memory:").unwrap(); + let mut db = database(":memory:"); for node in &nodes { let time = rng.u64(..now.as_millis()); @@ -436,7 +444,7 @@ mod test { fn test_count() { let id = arbitrary::gen::(1); let nodes = arbitrary::set::(5..10); - let mut db = Database::open(":memory:").unwrap(); + let mut db = database(":memory:"); for node in &nodes { db.insert([&id], *node, 0).unwrap();