Skip to content

Commit

Permalink
Panic if existing query redefined
Browse files Browse the repository at this point in the history
Be more careful about how we handle repeated user-specified query names.
They're fine as long as the query is identical, but not if it is not --
the name would map to the new query and leave the existing one orphaned.
We therefore disallow redefining queries without a migration that first
removes the old query, at least for the moment.
  • Loading branch information
ms705 committed Nov 28, 2018
1 parent b6e237a commit 4b11795
Showing 1 changed file with 31 additions and 0 deletions.
31 changes: 31 additions & 0 deletions noria-server/src/controller/recipe/mod.rs
Expand Up @@ -245,6 +245,11 @@ impl Recipe {
match n {
None => (),
Some(ref name) => {
assert!(
!aliases.contains_key(name) || aliases[name] == qid,
"Query name exists but existing query is different: {}",
name
);
aliases.insert(name.clone(), qid);
}
}
Expand Down Expand Up @@ -534,6 +539,13 @@ impl Recipe {
new.expression_order.push(qid);
}

for (n, qid) in &add_rp.aliases {
assert!(
!new.aliases.contains_key(n) || new.aliases[n] == *qid,
"Query name exists but existing query is different: {}",
n
);
}
new.aliases.extend(add_rp.aliases);

// return new recipe as replacement for self
Expand Down Expand Up @@ -769,4 +781,23 @@ mod tests {
assert_eq!(r2.expressions.len(), 2);
assert_eq!(r2.prior, Some(Box::new(r1_copy)));
}

#[test]
#[should_panic(expected = "Query name exists but existing query is different")]
fn it_avoids_spurious_aliasing() {
let r0 = Recipe::blank(None);

let r1_txt = "q_0: SELECT a FROM b;\nq_1: SELECT a, c FROM b WHERE x = 42;";
let r1_t = Recipe::from_str(r1_txt, None).unwrap();
let r1 = r0.replace(r1_t).unwrap();
assert_eq!(r1.version, 1);
assert_eq!(r1.expressions.len(), 2);

let r2_txt = "q_0: SELECT a, c FROM b WHERE x = 21;\nq_1: SELECT c FROM b;";
// we expect this to panic, since both q_0 and q_1 already exist with a different
// definition
let r2 = r1.extend(r2_txt).unwrap();
assert_eq!(r2.version, 2);
assert_eq!(r2.expressions.len(), 4);
}
}

0 comments on commit 4b11795

Please sign in to comment.