feat: ADR-0035 4b — foreign keys in CREATE TABLE
Add foreign keys to advanced-mode SQL CREATE TABLE — the SQL spelling of an ADR-0013 named relationship, created in the same transaction as the table (one undo step). - Grammar: inline `<col> … REFERENCES <parent>[(<col>)] [ON DELETE/UPDATE …]` (a new column constraint) and table-level `[CONSTRAINT <name>] FOREIGN KEY (<col>) REFERENCES …` (two new element branches — both start on a concrete keyword, never a leading Optional, which would abort the element Choice). Referential clauses reuse shared::REFERENTIAL_CLAUSES. - Builder: greedy FK-clause consumption (parens consumed internally so they don't perturb the 4a.3 element-boundary depth tracker); inline FK auto-named, table FK takes an optional CONSTRAINT name. - Worker: do_create_table resolves + validates each FK before building the DDL (self-ref validates against the in-statement columns/PK; bare REFERENCES resolves to the parent's single-column PK, composite -> error; PK-target + Type::fk_target_type compatibility), emits the FOREIGN KEY clause identically to schema_to_ddl, and writes the relationship metadata in the create transaction. - Reuse: name/uniqueness/metadata-insert/type-compat factored into shared helpers; do_add_relationship refactored to use them. - FKs round-trip via the existing relationship plumbing (no new persistence structures); describe surfaces the relationship. Self-references and bare `REFERENCES <parent>` supported (user-confirmed). Self-ref pre-submit indicator wrinkle deferred to 4i (tracked in ADR §13, a code comment, and the plan). DA/runda round added cross-cutting probes (FK survives the add-column rebuild + a later rebuild_from_text; referential actions survive rebuild; drop-child clears the relationship; drop-parent refused; bare self-ref resolves to own PK) — all green, no fixes needed. 27 new tests (grammar/builder + Tier-3). Docs: ADR-0035 Status/§13, README, requirements.md Q1. Tests: 1795 passing, 0 failing, 1 ignored. Clippy clean.
This commit is contained in:
+565
-1
@@ -18,7 +18,7 @@
|
||||
//! tests drive the worker directly, mirroring `tests/sql_insert.rs`.
|
||||
|
||||
use rdbms_playground::db::{CreateOutcome, Database};
|
||||
use rdbms_playground::dsl::{ColumnSpec, Type, Value};
|
||||
use rdbms_playground::dsl::{ColumnSpec, ReferentialAction, RowFilter, SqlForeignKey, Type, Value};
|
||||
use rdbms_playground::persistence::Persistence;
|
||||
use rdbms_playground::project;
|
||||
|
||||
@@ -53,6 +53,7 @@ fn created_table_appears_with_playground_types() {
|
||||
vec!["id".to_string()],
|
||||
vec![], // no composite UNIQUE
|
||||
vec![], // no table CHECK
|
||||
vec![], // no FK
|
||||
false,
|
||||
Some("create table Widget (id int primary key, name text)".to_string()),
|
||||
))
|
||||
@@ -91,6 +92,7 @@ fn integer_primary_key_is_plain_int() {
|
||||
vec!["id".to_string()],
|
||||
vec![], // no composite UNIQUE
|
||||
vec![], // no table CHECK
|
||||
vec![], // no FK
|
||||
false,
|
||||
Some("create table T (id integer primary key)".to_string()),
|
||||
))
|
||||
@@ -117,6 +119,7 @@ fn serial_pk_autoincrements_in_multi_column_table() {
|
||||
vec!["id".to_string()],
|
||||
vec![], // no composite UNIQUE
|
||||
vec![], // no table CHECK
|
||||
vec![], // no FK
|
||||
false,
|
||||
Some("create table T (id serial primary key, name text)".to_string()),
|
||||
))
|
||||
@@ -161,6 +164,7 @@ fn if_not_exists_is_a_noop_when_table_exists() {
|
||||
vec!["id".to_string()],
|
||||
vec![], // no composite UNIQUE
|
||||
vec![], // no table CHECK
|
||||
vec![], // no FK
|
||||
false,
|
||||
Some("create table T (id int)".to_string()),
|
||||
))
|
||||
@@ -173,6 +177,7 @@ fn if_not_exists_is_a_noop_when_table_exists() {
|
||||
vec!["id".to_string()],
|
||||
vec![], // no composite UNIQUE
|
||||
vec![], // no table CHECK
|
||||
vec![], // no FK
|
||||
true, // IF NOT EXISTS
|
||||
Some("create table if not exists T (id int)".to_string()),
|
||||
))
|
||||
@@ -200,6 +205,7 @@ fn table_without_primary_key_is_allowed() {
|
||||
vec![], // no primary key
|
||||
vec![], // no composite UNIQUE
|
||||
vec![], // no table CHECK
|
||||
vec![], // no FK
|
||||
false,
|
||||
Some("create table Notes (body text)".to_string()),
|
||||
))
|
||||
@@ -243,6 +249,7 @@ fn check_constraint_is_enforced() {
|
||||
vec!["id".to_string()],
|
||||
vec![],
|
||||
vec![], // no table CHECK
|
||||
vec![], // no FK
|
||||
false,
|
||||
Some("create table T (id serial primary key, price real check (price >= 0))".to_string()),
|
||||
))
|
||||
@@ -278,6 +285,7 @@ fn default_is_applied_when_column_omitted() {
|
||||
vec!["id".to_string()],
|
||||
vec![],
|
||||
vec![], // no table CHECK
|
||||
vec![], // no FK
|
||||
false,
|
||||
Some("create table T (id serial primary key, label text, n int default 7)".to_string()),
|
||||
))
|
||||
@@ -307,6 +315,7 @@ fn composite_unique_is_enforced() {
|
||||
vec![],
|
||||
vec![vec!["a".to_string(), "b".to_string()]],
|
||||
vec![], // no table CHECK
|
||||
vec![], // no FK
|
||||
false,
|
||||
Some("create table T (a int, b int, unique (a, b))".to_string()),
|
||||
))
|
||||
@@ -342,6 +351,7 @@ fn check_default_and_composite_unique_survive_rebuild() {
|
||||
vec![],
|
||||
vec![vec!["a".to_string(), "b".to_string()]],
|
||||
vec![], // no table CHECK
|
||||
vec![], // no FK
|
||||
false,
|
||||
Some(
|
||||
"create table T (a int, b int, price real check (price >= 0), \
|
||||
@@ -392,6 +402,7 @@ fn table_level_check_is_enforced() {
|
||||
vec![],
|
||||
vec![], // no composite UNIQUE
|
||||
vec!["a < b".to_string()], // table-level CHECK
|
||||
vec![], // no FK
|
||||
false,
|
||||
Some("create table T (a int, b int, check (a < b))".to_string()),
|
||||
))
|
||||
@@ -423,6 +434,7 @@ fn multiple_table_level_checks_all_enforced() {
|
||||
vec![],
|
||||
vec![], // no composite UNIQUE
|
||||
vec!["a < b".to_string(), "b < c".to_string()],
|
||||
vec![], // no FK
|
||||
false,
|
||||
Some("create table T (a int, b int, c int, check (a < b), check (b < c))".to_string()),
|
||||
))
|
||||
@@ -459,6 +471,7 @@ fn dropping_a_table_clears_its_table_check_metadata() {
|
||||
vec![],
|
||||
vec![], // no composite UNIQUE
|
||||
vec!["a < b".to_string()],
|
||||
vec![], // no FK
|
||||
false,
|
||||
Some("create table T (a int, b int, check (a < b))".to_string()),
|
||||
)
|
||||
@@ -496,6 +509,7 @@ fn table_level_check_survives_a_rebuild_triggering_column_add() {
|
||||
vec![],
|
||||
vec![], // no composite UNIQUE
|
||||
vec!["a < b".to_string()],
|
||||
vec![], // no FK
|
||||
false,
|
||||
Some("create table T (a int, b int, check (a < b))".to_string()),
|
||||
))
|
||||
@@ -547,6 +561,7 @@ fn table_level_check_survives_rebuild() {
|
||||
vec![],
|
||||
vec![], // no composite UNIQUE
|
||||
vec!["a < b".to_string()],
|
||||
vec![], // no FK
|
||||
false,
|
||||
Some("create table T (a int, b int, check (a < b))".to_string()),
|
||||
))
|
||||
@@ -583,6 +598,7 @@ fn if_not_exists_noop_is_journalled() {
|
||||
vec!["id".to_string()],
|
||||
vec![], // no composite UNIQUE
|
||||
vec![], // no table CHECK
|
||||
vec![], // no FK
|
||||
false,
|
||||
Some("create table T (id int)".to_string()),
|
||||
))
|
||||
@@ -595,6 +611,7 @@ fn if_not_exists_noop_is_journalled() {
|
||||
vec!["id".to_string()],
|
||||
vec![], // no composite UNIQUE
|
||||
vec![], // no table CHECK
|
||||
vec![], // no FK
|
||||
true,
|
||||
Some(noop.to_string()),
|
||||
))
|
||||
@@ -615,6 +632,7 @@ fn plain_create_errors_when_table_exists() {
|
||||
vec!["id".to_string()],
|
||||
vec![], // no composite UNIQUE
|
||||
vec![], // no table CHECK
|
||||
vec![], // no FK
|
||||
false,
|
||||
Some("create table T (id int)".to_string()),
|
||||
))
|
||||
@@ -626,6 +644,7 @@ fn plain_create_errors_when_table_exists() {
|
||||
vec!["id".to_string()],
|
||||
vec![], // no composite UNIQUE
|
||||
vec![], // no table CHECK
|
||||
vec![], // no FK
|
||||
false, // no IF NOT EXISTS
|
||||
Some("create table T (id int)".to_string()),
|
||||
));
|
||||
@@ -642,6 +661,7 @@ fn sql_create_table_is_one_undo_step() {
|
||||
vec!["id".to_string()],
|
||||
vec![], // no composite UNIQUE
|
||||
vec![], // no table CHECK
|
||||
vec![], // no FK
|
||||
false,
|
||||
Some("create table T (id int)".to_string()),
|
||||
))
|
||||
@@ -694,6 +714,7 @@ fn serial_pk_first_column_autoincrements_after_rebuild() {
|
||||
vec!["id".to_string()],
|
||||
vec![], // no composite UNIQUE
|
||||
vec![], // no table CHECK
|
||||
vec![], // no FK
|
||||
false,
|
||||
Some("create table T (id serial primary key, name text)".to_string()),
|
||||
))
|
||||
@@ -727,6 +748,7 @@ fn serial_pk_non_first_column_autoincrements_after_rebuild() {
|
||||
vec!["id".to_string()],
|
||||
vec![], // no composite UNIQUE
|
||||
vec![], // no table CHECK
|
||||
vec![], // no FK
|
||||
false,
|
||||
Some("create table T (name text, id serial primary key)".to_string()),
|
||||
))
|
||||
@@ -763,6 +785,7 @@ fn dropping_a_column_a_table_check_references_fails_cleanly() {
|
||||
vec![],
|
||||
vec![], // no composite UNIQUE
|
||||
vec!["a < b".to_string()],
|
||||
vec![], // no FK
|
||||
false,
|
||||
Some("create table T (a int, b int, check (a < b))".to_string()),
|
||||
))
|
||||
@@ -797,3 +820,544 @@ fn dropping_a_column_a_table_check_references_fails_cleanly() {
|
||||
r.block_on(ins("1", "2")).expect("(1,2) valid — table survived intact");
|
||||
assert!(r.block_on(ins("2", "1")).is_err(), "CHECK still enforced after the failed drop");
|
||||
}
|
||||
|
||||
// =================================================================
|
||||
// Sub-phase 4b — foreign keys in CREATE TABLE (ADR-0035 §5).
|
||||
//
|
||||
// These drive the worker directly with `SqlForeignKey` specs; the
|
||||
// grammar (text -> Command) is covered by `builder_tests`. An FK is
|
||||
// the SQL spelling of an ADR-0013 named relationship, created in the
|
||||
// same transaction as the table (one undo step).
|
||||
// =================================================================
|
||||
|
||||
/// A simple FK spec with default (no action) referential actions.
|
||||
fn fk(child_column: &str, parent_table: &str, parent_column: Option<&str>) -> SqlForeignKey {
|
||||
SqlForeignKey {
|
||||
name: None,
|
||||
child_column: child_column.to_string(),
|
||||
parent_table: parent_table.to_string(),
|
||||
parent_column: parent_column.map(str::to_string),
|
||||
on_delete: ReferentialAction::NoAction,
|
||||
on_update: ReferentialAction::NoAction,
|
||||
}
|
||||
}
|
||||
|
||||
/// Create `parent (id serial primary key, label text)` — the extra
|
||||
/// column lets a row be inserted (a sole auto-fill serial has nothing
|
||||
/// to bind). `id` auto-fills 1, 2, … on each label insert.
|
||||
fn make_parent(db: &Database, r: &tokio::runtime::Runtime) {
|
||||
r.block_on(db.sql_create_table(
|
||||
"parent".to_string(),
|
||||
vec![ColumnSpec::new("id", Type::Serial), ColumnSpec::new("label", Type::Text)],
|
||||
vec!["id".to_string()],
|
||||
vec![],
|
||||
vec![],
|
||||
vec![], // no FK
|
||||
false,
|
||||
Some("create table parent (id serial primary key, label text)".to_string()),
|
||||
))
|
||||
.expect("create parent");
|
||||
}
|
||||
|
||||
/// Insert a parent row (label only); its serial `id` auto-fills.
|
||||
fn insert_parent_row(db: &Database, r: &tokio::runtime::Runtime) {
|
||||
r.block_on(db.insert(
|
||||
"parent".to_string(),
|
||||
Some(vec!["label".to_string()]),
|
||||
vec![Value::Text("x".to_string())],
|
||||
Some("insert".to_string()),
|
||||
))
|
||||
.expect("parent row");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn foreign_key_is_enforced() {
|
||||
let (_p, db, _d) = open(false);
|
||||
let r = rt();
|
||||
make_parent(&db, &r);
|
||||
r.block_on(db.sql_create_table(
|
||||
"child".to_string(),
|
||||
vec![ColumnSpec::new("id", Type::Serial), ColumnSpec::new("pid", Type::Int)],
|
||||
vec!["id".to_string()],
|
||||
vec![],
|
||||
vec![],
|
||||
vec![fk("pid", "parent", Some("id"))],
|
||||
false,
|
||||
Some("create table child (id serial primary key, pid int references parent(id))".to_string()),
|
||||
))
|
||||
.expect("create child with FK");
|
||||
|
||||
// A parent row, then a valid child referencing it.
|
||||
insert_parent_row(&db, &r); // id=1
|
||||
r.block_on(db.insert(
|
||||
"child".to_string(),
|
||||
Some(vec!["pid".to_string()]),
|
||||
vec![Value::Number("1".to_string())],
|
||||
Some("insert".to_string()),
|
||||
))
|
||||
.expect("child pid=1 references an existing parent");
|
||||
// A child referencing a non-existent parent is rejected.
|
||||
let bad = r.block_on(db.insert(
|
||||
"child".to_string(),
|
||||
Some(vec!["pid".to_string()]),
|
||||
vec![Value::Number("999".to_string())],
|
||||
Some("insert".to_string()),
|
||||
));
|
||||
assert!(bad.is_err(), "FK rejects pid=999 (no such parent)");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn foreign_key_creates_named_relationship_visible_in_describe() {
|
||||
let (_p, db, _d) = open(false);
|
||||
let r = rt();
|
||||
make_parent(&db, &r);
|
||||
r.block_on(db.sql_create_table(
|
||||
"child".to_string(),
|
||||
vec![ColumnSpec::new("id", Type::Serial), ColumnSpec::new("pid", Type::Int)],
|
||||
vec!["id".to_string()],
|
||||
vec![],
|
||||
vec![],
|
||||
vec![fk("pid", "parent", Some("id"))],
|
||||
false,
|
||||
Some("create table child (id serial primary key, pid int references parent(id))".to_string()),
|
||||
))
|
||||
.expect("create child with FK");
|
||||
|
||||
// The child has an outbound relationship; the parent an inbound one.
|
||||
let child = r.block_on(db.describe_table("child".to_string(), None)).expect("describe child");
|
||||
assert_eq!(child.outbound_relationships.len(), 1, "child references parent");
|
||||
let rel = &child.outbound_relationships[0];
|
||||
assert_eq!(rel.name, "parent_id_to_child_pid", "auto-named per ADR-0013");
|
||||
assert_eq!(rel.other_table, "parent");
|
||||
assert_eq!(rel.local_column, "pid");
|
||||
|
||||
let parent = r.block_on(db.describe_table("parent".to_string(), None)).expect("describe parent");
|
||||
assert_eq!(parent.inbound_relationships.len(), 1, "parent is referenced by child");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn explicit_constraint_name_is_used() {
|
||||
let (_p, db, _d) = open(false);
|
||||
let r = rt();
|
||||
make_parent(&db, &r);
|
||||
let mut spec = fk("pid", "parent", Some("id"));
|
||||
spec.name = Some("child_to_parent".to_string());
|
||||
r.block_on(db.sql_create_table(
|
||||
"child".to_string(),
|
||||
vec![ColumnSpec::new("id", Type::Serial), ColumnSpec::new("pid", Type::Int)],
|
||||
vec!["id".to_string()],
|
||||
vec![],
|
||||
vec![],
|
||||
vec![spec],
|
||||
false,
|
||||
Some("create table child (id serial primary key, pid int, constraint child_to_parent foreign key (pid) references parent(id))".to_string()),
|
||||
))
|
||||
.expect("create child with named FK");
|
||||
let child = r.block_on(db.describe_table("child".to_string(), None)).expect("describe");
|
||||
assert_eq!(child.outbound_relationships[0].name, "child_to_parent");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn bare_references_resolves_to_parent_single_column_pk() {
|
||||
let (_p, db, _d) = open(false);
|
||||
let r = rt();
|
||||
make_parent(&db, &r);
|
||||
r.block_on(db.sql_create_table(
|
||||
"child".to_string(),
|
||||
vec![ColumnSpec::new("id", Type::Serial), ColumnSpec::new("pid", Type::Int)],
|
||||
vec!["id".to_string()],
|
||||
vec![],
|
||||
vec![],
|
||||
vec![fk("pid", "parent", None)], // bare REFERENCES parent
|
||||
false,
|
||||
Some("create table child (id serial primary key, pid int references parent)".to_string()),
|
||||
))
|
||||
.expect("create child with bare REFERENCES");
|
||||
let child = r.block_on(db.describe_table("child".to_string(), None)).expect("describe");
|
||||
assert_eq!(child.outbound_relationships[0].other_column, "id", "resolved to parent PK");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn self_referencing_foreign_key_is_enforced() {
|
||||
let (_p, db, _d) = open(false);
|
||||
let r = rt();
|
||||
r.block_on(db.sql_create_table(
|
||||
"emp".to_string(),
|
||||
vec![ColumnSpec::new("id", Type::Int), ColumnSpec::new("mgr", Type::Int)],
|
||||
vec!["id".to_string()],
|
||||
vec![],
|
||||
vec![],
|
||||
vec![fk("mgr", "emp", Some("id"))], // self-reference
|
||||
false,
|
||||
Some("create table emp (id int primary key, mgr int references emp(id))".to_string()),
|
||||
))
|
||||
.expect("create self-referencing emp");
|
||||
let ins = |id: &str, mgr: Option<&str>| {
|
||||
let (cols, vals) = mgr.map_or_else(
|
||||
|| (vec!["id".to_string()], vec![Value::Number(id.to_string())]),
|
||||
|m| {
|
||||
(
|
||||
vec!["id".to_string(), "mgr".to_string()],
|
||||
vec![Value::Number(id.to_string()), Value::Number(m.to_string())],
|
||||
)
|
||||
},
|
||||
);
|
||||
db.insert("emp".to_string(), Some(cols), vals, Some("insert".to_string()))
|
||||
};
|
||||
r.block_on(ins("1", None)).expect("root (mgr NULL)");
|
||||
r.block_on(ins("2", Some("1"))).expect("emp 2 reports to 1");
|
||||
assert!(r.block_on(ins("3", Some("99"))).is_err(), "self-FK rejects mgr=99");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn foreign_key_type_mismatch_is_rejected() {
|
||||
let (_p, db, _d) = open(false);
|
||||
let r = rt();
|
||||
make_parent(&db, &r); // parent.id is serial -> fk_target_type int
|
||||
// child.pid declared text -> incompatible with int.
|
||||
let res = r.block_on(db.sql_create_table(
|
||||
"child".to_string(),
|
||||
vec![ColumnSpec::new("id", Type::Serial), ColumnSpec::new("pid", Type::Text)],
|
||||
vec!["id".to_string()],
|
||||
vec![],
|
||||
vec![],
|
||||
vec![fk("pid", "parent", Some("id"))],
|
||||
false,
|
||||
Some("create table child (id serial primary key, pid text references parent(id))".to_string()),
|
||||
));
|
||||
assert!(res.is_err(), "FK column type must match the parent's fk_target_type");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn foreign_key_to_non_pk_column_is_rejected() {
|
||||
let (_p, db, _d) = open(false);
|
||||
let r = rt();
|
||||
r.block_on(db.sql_create_table(
|
||||
"parent".to_string(),
|
||||
vec![ColumnSpec::new("id", Type::Serial), ColumnSpec::new("label", Type::Text)],
|
||||
vec!["id".to_string()],
|
||||
vec![],
|
||||
vec![],
|
||||
vec![],
|
||||
false,
|
||||
Some("create table parent (id serial primary key, label text)".to_string()),
|
||||
))
|
||||
.expect("create parent");
|
||||
let res = r.block_on(db.sql_create_table(
|
||||
"child".to_string(),
|
||||
vec![ColumnSpec::new("id", Type::Serial), ColumnSpec::new("plabel", Type::Text)],
|
||||
vec!["id".to_string()],
|
||||
vec![],
|
||||
vec![],
|
||||
vec![fk("plabel", "parent", Some("label"))], // label is not a PK
|
||||
false,
|
||||
Some("create table child (id serial primary key, plabel text references parent(label))".to_string()),
|
||||
));
|
||||
assert!(res.is_err(), "FK must target a primary key");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn foreign_key_survives_rebuild() {
|
||||
let (p, db, _d) = open(false);
|
||||
let r = rt();
|
||||
make_parent(&db, &r);
|
||||
r.block_on(db.sql_create_table(
|
||||
"child".to_string(),
|
||||
vec![ColumnSpec::new("id", Type::Serial), ColumnSpec::new("pid", Type::Int)],
|
||||
vec!["id".to_string()],
|
||||
vec![],
|
||||
vec![],
|
||||
vec![fk("pid", "parent", Some("id"))],
|
||||
false,
|
||||
Some("create table child (id serial primary key, pid int references parent(id))".to_string()),
|
||||
))
|
||||
.expect("create child with FK");
|
||||
r.block_on(db.rebuild_from_text(p.path().to_path_buf(), None)).expect("rebuild");
|
||||
|
||||
insert_parent_row(&db, &r);
|
||||
assert!(
|
||||
r.block_on(db.insert(
|
||||
"child".to_string(),
|
||||
Some(vec!["pid".to_string()]),
|
||||
vec![Value::Number("999".to_string())],
|
||||
Some("insert".to_string()),
|
||||
))
|
||||
.is_err(),
|
||||
"FK still enforced after rebuild"
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn create_table_with_fk_is_one_undo_step() {
|
||||
let (_p, db, _d) = open(true); // undo enabled
|
||||
let r = rt();
|
||||
make_parent(&db, &r);
|
||||
r.block_on(db.sql_create_table(
|
||||
"child".to_string(),
|
||||
vec![ColumnSpec::new("id", Type::Serial), ColumnSpec::new("pid", Type::Int)],
|
||||
vec!["id".to_string()],
|
||||
vec![],
|
||||
vec![],
|
||||
vec![fk("pid", "parent", Some("id"))],
|
||||
false,
|
||||
Some("create table child (id serial primary key, pid int references parent(id))".to_string()),
|
||||
))
|
||||
.expect("create child with FK");
|
||||
// One undo removes the child table AND its relationship row, so the
|
||||
// parent (now un-referenced) can be described without a dangling rel.
|
||||
r.block_on(db.undo()).expect("undo").expect("a step was undone");
|
||||
assert!(!r.block_on(db.list_tables()).unwrap().contains(&"child".to_string()));
|
||||
let parent = r.block_on(db.describe_table("parent".to_string(), None)).expect("describe parent");
|
||||
assert!(parent.inbound_relationships.is_empty(), "the relationship was undone with the table");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn foreign_key_on_delete_cascade_takes_effect() {
|
||||
// Proves the referential action reaches the engine DDL (not just
|
||||
// the metadata): deleting a parent row cascades to the children.
|
||||
let (_p, db, _d) = open(false);
|
||||
let r = rt();
|
||||
make_parent(&db, &r);
|
||||
let mut spec = fk("pid", "parent", Some("id"));
|
||||
spec.on_delete = ReferentialAction::Cascade;
|
||||
r.block_on(db.sql_create_table(
|
||||
"child".to_string(),
|
||||
vec![ColumnSpec::new("id", Type::Serial), ColumnSpec::new("pid", Type::Int)],
|
||||
vec!["id".to_string()],
|
||||
vec![],
|
||||
vec![],
|
||||
vec![spec],
|
||||
false,
|
||||
Some(
|
||||
"create table child (id serial primary key, pid int references parent(id) \
|
||||
on delete cascade)"
|
||||
.to_string(),
|
||||
),
|
||||
))
|
||||
.expect("create child with ON DELETE CASCADE");
|
||||
insert_parent_row(&db, &r); // id=1
|
||||
r.block_on(db.insert(
|
||||
"child".to_string(),
|
||||
Some(vec!["pid".to_string()]),
|
||||
vec![Value::Number("1".to_string())],
|
||||
Some("insert".to_string()),
|
||||
))
|
||||
.expect("child referencing parent 1");
|
||||
// Delete the parent row; the child should cascade away.
|
||||
r.block_on(db.delete(
|
||||
"parent".to_string(),
|
||||
RowFilter::eq("id", Value::Number("1".to_string())),
|
||||
Some("delete".to_string()),
|
||||
))
|
||||
.expect("delete parent");
|
||||
let child_rows = r
|
||||
.block_on(db.query_data("child".to_string(), None, None, None))
|
||||
.expect("query child");
|
||||
assert!(child_rows.rows.is_empty(), "ON DELETE CASCADE removed the child row");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn foreign_key_to_unknown_parent_is_rejected() {
|
||||
let (_p, db, _d) = open(false);
|
||||
let r = rt();
|
||||
let res = r.block_on(db.sql_create_table(
|
||||
"child".to_string(),
|
||||
vec![ColumnSpec::new("id", Type::Serial), ColumnSpec::new("pid", Type::Int)],
|
||||
vec!["id".to_string()],
|
||||
vec![],
|
||||
vec![],
|
||||
vec![fk("pid", "ghost", Some("id"))], // no such table
|
||||
false,
|
||||
Some("create table child (id serial primary key, pid int references ghost(id))".to_string()),
|
||||
));
|
||||
assert!(res.is_err(), "a FK to a non-existent parent table is rejected");
|
||||
// And the failed create left nothing behind.
|
||||
assert!(!r.block_on(db.list_tables()).unwrap().contains(&"child".to_string()));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn composite_pk_bare_reference_is_rejected() {
|
||||
let (_p, db, _d) = open(false);
|
||||
let r = rt();
|
||||
r.block_on(db.sql_create_table(
|
||||
"cp".to_string(),
|
||||
vec![ColumnSpec::new("a", Type::Int), ColumnSpec::new("b", Type::Int)],
|
||||
vec!["a".to_string(), "b".to_string()], // composite PK
|
||||
vec![],
|
||||
vec![],
|
||||
vec![],
|
||||
false,
|
||||
Some("create table cp (a int, b int, primary key (a, b))".to_string()),
|
||||
))
|
||||
.expect("create composite-PK parent");
|
||||
// A bare `REFERENCES cp` cannot disambiguate which PK column.
|
||||
let res = r.block_on(db.sql_create_table(
|
||||
"child".to_string(),
|
||||
vec![ColumnSpec::new("id", Type::Serial), ColumnSpec::new("ref", Type::Int)],
|
||||
vec!["id".to_string()],
|
||||
vec![],
|
||||
vec![],
|
||||
vec![fk("ref", "cp", None)], // bare reference to a composite-PK parent
|
||||
false,
|
||||
Some("create table child (id serial primary key, ref int references cp)".to_string()),
|
||||
));
|
||||
assert!(res.is_err(), "bare REFERENCES to a composite-PK parent must be rejected");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn fk_survives_a_rebuild_triggering_column_add() {
|
||||
// Cross-cutting (ADR-0013 rebuild primitive × 4b): adding a
|
||||
// constrained column to a child that has an FK rebuilds the table
|
||||
// via schema_to_ddl. The FK must survive in the engine AND the
|
||||
// relationship metadata (so a later rebuild_from_text re-emits it).
|
||||
let (p, db, _d) = open(false);
|
||||
let r = rt();
|
||||
make_parent(&db, &r);
|
||||
r.block_on(db.sql_create_table(
|
||||
"child".to_string(),
|
||||
vec![ColumnSpec::new("id", Type::Serial), ColumnSpec::new("pid", Type::Int)],
|
||||
vec!["id".to_string()],
|
||||
vec![],
|
||||
vec![],
|
||||
vec![fk("pid", "parent", Some("id"))],
|
||||
false,
|
||||
Some("create table child (id serial primary key, pid int references parent(id))".to_string()),
|
||||
))
|
||||
.expect("create child with FK");
|
||||
// A UNIQUE column forces the rebuild path.
|
||||
let mut c = ColumnSpec::new("code", Type::Int);
|
||||
c.unique = true;
|
||||
r.block_on(db.add_column("child".to_string(), c, Some("add column child: code(int) unique".to_string())))
|
||||
.expect("add column via rebuild");
|
||||
|
||||
// The relationship still exists after the rebuild.
|
||||
let child = r.block_on(db.describe_table("child".to_string(), None)).expect("describe");
|
||||
assert_eq!(child.outbound_relationships.len(), 1, "FK survived the column-add rebuild");
|
||||
// And the engine still enforces it (now and after a fresh rebuild).
|
||||
insert_parent_row(&db, &r);
|
||||
r.block_on(db.rebuild_from_text(p.path().to_path_buf(), None)).expect("rebuild");
|
||||
assert!(
|
||||
r.block_on(db.insert(
|
||||
"child".to_string(),
|
||||
Some(vec!["pid".to_string()]),
|
||||
vec![Value::Number("999".to_string())],
|
||||
Some("insert".to_string()),
|
||||
))
|
||||
.is_err(),
|
||||
"FK still enforced after the column-add rebuild and a later rebuild_from_text"
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn fk_referential_actions_survive_rebuild() {
|
||||
// The actions (not just the FK's existence) must round-trip through
|
||||
// pragma -> REL_TABLE -> project.yaml -> rebuild.
|
||||
let (p, db, _d) = open(false);
|
||||
let r = rt();
|
||||
make_parent(&db, &r);
|
||||
let mut spec = fk("pid", "parent", Some("id"));
|
||||
spec.on_delete = ReferentialAction::Cascade;
|
||||
spec.on_update = ReferentialAction::SetNull;
|
||||
r.block_on(db.sql_create_table(
|
||||
"child".to_string(),
|
||||
vec![ColumnSpec::new("id", Type::Serial), ColumnSpec::new("pid", Type::Int)],
|
||||
vec!["id".to_string()],
|
||||
vec![],
|
||||
vec![],
|
||||
vec![spec],
|
||||
false,
|
||||
Some(
|
||||
"create table child (id serial primary key, pid int references parent(id) \
|
||||
on delete cascade on update set null)"
|
||||
.to_string(),
|
||||
),
|
||||
))
|
||||
.expect("create");
|
||||
r.block_on(db.rebuild_from_text(p.path().to_path_buf(), None)).expect("rebuild");
|
||||
let child = r.block_on(db.describe_table("child".to_string(), None)).expect("describe");
|
||||
let rel = &child.outbound_relationships[0];
|
||||
assert_eq!(rel.on_delete, ReferentialAction::Cascade, "ON DELETE survived rebuild");
|
||||
assert_eq!(rel.on_update, ReferentialAction::SetNull, "ON UPDATE survived rebuild");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn dropping_the_child_clears_the_fk_relationship() {
|
||||
let (_p, db, _d) = open(false);
|
||||
let r = rt();
|
||||
make_parent(&db, &r);
|
||||
r.block_on(db.sql_create_table(
|
||||
"child".to_string(),
|
||||
vec![ColumnSpec::new("id", Type::Serial), ColumnSpec::new("pid", Type::Int)],
|
||||
vec!["id".to_string()],
|
||||
vec![],
|
||||
vec![],
|
||||
vec![fk("pid", "parent", Some("id"))],
|
||||
false,
|
||||
Some("create table child (id serial primary key, pid int references parent(id))".to_string()),
|
||||
))
|
||||
.expect("create");
|
||||
r.block_on(db.drop_table("child".to_string(), Some("drop table child".to_string())))
|
||||
.expect("drop child");
|
||||
let parent = r.block_on(db.describe_table("parent".to_string(), None)).expect("describe parent");
|
||||
assert!(parent.inbound_relationships.is_empty(), "dropping the child cleared the relationship");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn dropping_a_referenced_parent_is_refused() {
|
||||
// ADR-0013: a parent with inbound relationships can't be dropped.
|
||||
let (_p, db, _d) = open(false);
|
||||
let r = rt();
|
||||
make_parent(&db, &r);
|
||||
r.block_on(db.sql_create_table(
|
||||
"child".to_string(),
|
||||
vec![ColumnSpec::new("id", Type::Serial), ColumnSpec::new("pid", Type::Int)],
|
||||
vec!["id".to_string()],
|
||||
vec![],
|
||||
vec![],
|
||||
vec![fk("pid", "parent", Some("id"))],
|
||||
false,
|
||||
Some("create table child (id serial primary key, pid int references parent(id))".to_string()),
|
||||
))
|
||||
.expect("create");
|
||||
assert!(
|
||||
r.block_on(db.drop_table("parent".to_string(), Some("drop table parent".to_string()))).is_err(),
|
||||
"a referenced parent can't be dropped while the child's FK exists"
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn bare_self_reference_resolves_to_own_pk() {
|
||||
let (_p, db, _d) = open(false);
|
||||
let r = rt();
|
||||
r.block_on(db.sql_create_table(
|
||||
"emp".to_string(),
|
||||
vec![ColumnSpec::new("id", Type::Int), ColumnSpec::new("mgr", Type::Int)],
|
||||
vec!["id".to_string()],
|
||||
vec![],
|
||||
vec![],
|
||||
vec![fk("mgr", "emp", None)], // bare AND self-referential
|
||||
false,
|
||||
Some("create table emp (id int primary key, mgr int references emp)".to_string()),
|
||||
))
|
||||
.expect("create self-referential emp with a bare reference");
|
||||
let emp = r.block_on(db.describe_table("emp".to_string(), None)).expect("describe");
|
||||
assert_eq!(emp.outbound_relationships[0].other_column, "id", "bare self-ref resolved to own PK");
|
||||
// Enforced: a non-existent manager is rejected.
|
||||
r.block_on(db.insert(
|
||||
"emp".to_string(),
|
||||
Some(vec!["id".to_string()]),
|
||||
vec![Value::Number("1".to_string())],
|
||||
Some("insert".to_string()),
|
||||
))
|
||||
.expect("root row");
|
||||
assert!(
|
||||
r.block_on(db.insert(
|
||||
"emp".to_string(),
|
||||
Some(vec!["id".to_string(), "mgr".to_string()]),
|
||||
vec![Value::Number("2".to_string()), Value::Number("99".to_string())],
|
||||
Some("insert".to_string()),
|
||||
))
|
||||
.is_err(),
|
||||
"bare self-ref FK is enforced"
|
||||
);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user