Skip to content

Commit 54f90cd

Browse files
author
Elrond
committed
test(buzz-db): keep table DDL out of the schema manifest and sibling dirs
The layout test only checked files under schema/tables/public/, so table DDL pasted into schema/schema.sql (an easy merge-conflict resolution) or a table declared in a sibling directory still built and passed CI. Restrict schema.sql to \i includes and CREATE EXTENSION, and outside tables/public/ reject ALTER TABLE and any CREATE TABLE other than PARTITION OF in partitions/. Signed-off-by: Elrond <28d6302a099e5225b02c4155ac4236e4912603df2ab08dbfc2f4fef08ce598c8@buzz.block.builderlab.xyz>
1 parent c867a6c commit 54f90cd

2 files changed

Lines changed: 48 additions & 6 deletions

File tree

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

Lines changed: 44 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1530,6 +1530,49 @@ mod postgres_tests {
15301530
"every schema/ file must be included by schema/schema.sql exactly once"
15311531
);
15321532

1533+
fn strip_comments(text: &str) -> String {
1534+
text.lines()
1535+
.map(|line| line.split_once("--").map_or(line, |(code, _)| code))
1536+
.collect::<Vec<_>>()
1537+
.join("\n")
1538+
}
1539+
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.
1543+
for line in strip_comments(&manifest).lines().map(str::trim) {
1544+
assert!(
1545+
line.is_empty()
1546+
|| line.starts_with("\\i ")
1547+
|| line.to_ascii_uppercase().starts_with("CREATE EXTENSION "),
1548+
"schema/schema.sql may only hold \\i includes and CREATE EXTENSION; \
1549+
put table DDL in tables/public/<table>.sql: {line:.80}"
1550+
);
1551+
}
1552+
for file in included.iter().filter(|f| !f.starts_with("tables/public/")) {
1553+
let text = std::fs::read_to_string(schema_dir.join(file)).expect("read schema file");
1554+
for statement in strip_comments(&text).to_ascii_uppercase().split(';') {
1555+
let tokens: Vec<_> = statement.split_whitespace().collect();
1556+
assert!(
1557+
!tokens.windows(2).any(|pair| pair == ["ALTER", "TABLE"]),
1558+
"{file} may not ALTER TABLE; change the table file in tables/public/ instead"
1559+
);
1560+
let creates_table = tokens.iter().enumerate().any(|(i, token)| {
1561+
*token == "CREATE"
1562+
&& tokens[i + 1..].iter().find(|t| {
1563+
!matches!(**t, "UNLOGGED" | "TEMP" | "TEMPORARY" | "GLOBAL" | "LOCAL")
1564+
}) == Some(&"TABLE")
1565+
});
1566+
assert!(
1567+
!creates_table
1568+
|| (file.starts_with("partitions/")
1569+
&& tokens.windows(2).any(|pair| pair == ["PARTITION", "OF"])),
1570+
"{file} may not CREATE TABLE; only partitions/ may, and only PARTITION OF. \
1571+
Declare tables in tables/public/<table>.sql"
1572+
);
1573+
}
1574+
}
1575+
15331576
let tables_dir = schema_dir.join("tables");
15341577
for entry in std::fs::read_dir(&tables_dir).expect("read schema/tables") {
15351578
let path = entry.expect("schema/tables entry").path();
@@ -1546,11 +1589,7 @@ mod postgres_tests {
15461589
.and_then(|name| name.strip_suffix(".sql"))
15471590
.expect("table file name");
15481591
let text = std::fs::read_to_string(schema_dir.join(file)).expect("read table file");
1549-
let code: String = text
1550-
.lines()
1551-
.map(|line| line.split_once("--").map_or(line, |(code, _)| code))
1552-
.collect::<Vec<_>>()
1553-
.join("\n");
1592+
let code = strip_comments(&text);
15541593
let mut creates = 0;
15551594
for statement in code
15561595
.split(';')

‎schema/schema.sql‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,10 @@
2020
--
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
23-
-- matching subdirectories and include those too.
23+
-- 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.
2427

2528
-- Buzz initial Postgres schema — multi-tenant.
2629
--

0 commit comments

Comments
 (0)