Radish alpha
h
Radicle Heartwood Protocol & Stack
Radicle
Git (anonymous pull)
Log in to clone via SSH
radicle: Add foreign-key constraint for routing
cloudhead committed 2 years ago
commit af095d53ca79d7cc6355d147e8c3990d53c8fde0
parent 6b04eff34ce4a77aa577703b0e6fa1efa9a62a19
3 files changed +25 -16
modified 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();
modified 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.
modified 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::<Id>(5..10);
        let nodes = arbitrary::set::<NodeId>(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::<Id>(5..10);
        let nodes = arbitrary::set::<NodeId>(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::<Id>(6..9);
        let nodes = arbitrary::set::<NodeId>(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::<Id>(5..10);
        let nodes = arbitrary::set::<NodeId>(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::<Id>(1);
        let node = arbitrary::gen::<NodeId>(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::<Id>(1);
        let node = arbitrary::gen::<NodeId>(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::<Id>(1);
        let id2 = arbitrary::gen::<Id>(1);
        let node = arbitrary::gen::<NodeId>(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::<Id>(1);
        let node = arbitrary::gen::<NodeId>(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::<Id>(10);
        let node = arbitrary::gen(1);

@@ -406,7 +414,7 @@ mod test {
        let now = LocalTime::now();
        let ids = arbitrary::vec::<Id>(10);
        let nodes = arbitrary::vec::<NodeId>(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::<Id>(1);
        let nodes = arbitrary::set::<NodeId>(5..10);
-
        let mut db = Database::open(":memory:").unwrap();
+
        let mut db = database(":memory:");

        for node in &nodes {
            db.insert([&id], *node, 0).unwrap();