Skip to content

Commit d141e50

Browse files
author
Elrond
committed
test(buzz-db): also keep index and drop DDL out of non-table schema files
Outside tables/public/, reject CREATE [UNIQUE] INDEX and ALTER/DROP of tables and indexes, not just ALTER TABLE. Run the statement check over schema.sql too, so a one-line paste after CREATE EXTENSION is caught. Signed-off-by: Elrond <28d6302a099e5225b02c4155ac4236e4912603df2ab08dbfc2f4fef08ce598c8@buzz.block.builderlab.xyz>
1 parent 54f90cd commit d141e50

2 files changed

Lines changed: 19 additions & 9 deletions

File tree

‎crates/buzz-db/src/runtime/migration.rs‎

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1537,9 +1537,9 @@ mod postgres_tests {
15371537
.join("\n")
15381538
}
15391539

1540-
// Table DDL must live in its table file, where SchemaBot reads it. A
1541-
// merge conflict resolved by pasting DDL into the manifest, or a table
1542-
// declared in a sibling directory, would still build and pass CI.
1540+
// Table and index DDL must live in its table file, where SchemaBot
1541+
// reads it. A merge conflict resolved by pasting DDL into the manifest,
1542+
// or DDL in a sibling directory, would still build and pass CI.
15431543
for line in strip_comments(&manifest).lines().map(str::trim) {
15441544
assert!(
15451545
line.is_empty()
@@ -1549,13 +1549,23 @@ mod postgres_tests {
15491549
put table DDL in tables/public/<table>.sql: {line:.80}"
15501550
);
15511551
}
1552-
for file in included.iter().filter(|f| !f.starts_with("tables/public/")) {
1552+
let non_table_files = std::iter::once("schema.sql").chain(
1553+
included
1554+
.iter()
1555+
.map(String::as_str)
1556+
.filter(|f| !f.starts_with("tables/public/")),
1557+
);
1558+
for file in non_table_files {
15531559
let text = std::fs::read_to_string(schema_dir.join(file)).expect("read schema file");
15541560
for statement in strip_comments(&text).to_ascii_uppercase().split(';') {
15551561
let tokens: Vec<_> = statement.split_whitespace().collect();
15561562
assert!(
1557-
!tokens.windows(2).any(|pair| pair == ["ALTER", "TABLE"]),
1558-
"{file} may not ALTER TABLE; change the table file in tables/public/ instead"
1563+
!tokens.windows(2).any(|pair| matches!(
1564+
pair,
1565+
["CREATE" | "UNIQUE", "INDEX"] | ["DROP" | "ALTER", "TABLE" | "INDEX"]
1566+
)),
1567+
"{file} may not create, alter or drop tables or indexes; \
1568+
change tables/public/<table>.sql instead"
15591569
);
15601570
let creates_table = tokens.iter().enumerate().any(|(i, token)| {
15611571
*token == "CREATE"

‎schema/schema.sql‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,9 @@
2121
-- Adding a table: add `tables/public/<table>.sql` and include it below after
2222
-- the tables it references; put its partitions, triggers, and seed rows in the
2323
-- matching subdirectories and include those too. This file holds only `\i`
24-
-- lines and `CREATE EXTENSION`. Outside `tables/public/`, no file may
25-
-- `ALTER TABLE` or `CREATE TABLE`, except `PARTITION OF` in `partitions/`.
26-
-- The layout test enforces both.
24+
-- lines and `CREATE EXTENSION`. Outside `tables/public/`, no file may create,
25+
-- alter or drop a table or index, except `CREATE TABLE ... PARTITION OF` in
26+
-- `partitions/`. The layout test enforces both.
2727

2828
-- Buzz initial Postgres schema — multi-tenant.
2929
--

0 commit comments

Comments
 (0)