Skip to content

Commit 98fe985

Browse files
authored
linter: docs for require-table-schema-rule (#1064)
1 parent 0affaf6 commit 98fe985

4 files changed

Lines changed: 53 additions & 10 deletions

File tree

crates/squawk_linter/src/rules/require_table_schema.rs

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -22,21 +22,20 @@ pub(crate) fn require_table_schema(ctx: &mut Linter, parse: &Parse<SourceFile>)
2222
ast::Stmt::DropTable(drop_table) => {
2323
check_path(ctx, drop_table.path(), drop_table.syntax());
2424
}
25-
_ => {}
25+
_ => (),
2626
}
2727
}
2828
}
2929

3030
fn check_path(ctx: &mut Linter, path: Option<ast::Path>, syntax: &squawk_syntax::SyntaxNode) {
31-
if let Some(path) = path {
32-
if path.qualifier().is_none() {
33-
ctx.report(Violation::for_node(
34-
Rule::RequireTableSchema,
35-
"Table name is not schema-qualified. Use schema.table (e.g., public.my_table)."
36-
.into(),
37-
syntax,
38-
));
39-
}
31+
if let Some(path) = path
32+
&& path.qualifier().is_none()
33+
{
34+
ctx.report(Violation::for_node(
35+
Rule::RequireTableSchema,
36+
"Table name is not schema-qualified. Use schema.table (e.g., public.my_table).".into(),
37+
syntax,
38+
));
4039
}
4140
}
4241

docs/docs/require-table-schema.md

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
---
2+
id: require-table-schema
3+
title: require-table-schema
4+
---
5+
6+
## problem
7+
8+
Without an explicit schema, Postgres uses the `search_path` when looking up tables and similar objects. This can result in ambiguity as to what specific table you're acting on.
9+
10+
```sql
11+
-- bad
12+
create table posts(id bigint);
13+
14+
alter table posts add column name text;
15+
16+
drop table posts;
17+
18+
create table posts as select 1;
19+
```
20+
21+
## solution
22+
23+
Specify the schema (i.e., `public`) alongside the table names in your DDL.
24+
25+
```sql
26+
-- good
27+
create table public.posts(id bigint);
28+
29+
alter table public.posts add column name text;
30+
31+
drop table public.posts;
32+
33+
create table public.posts as select 1;
34+
```
35+
36+
## links
37+
38+
- https://www.postgresql.org/docs/current/ddl-schemas.html#DDL-SCHEMAS-PATH

docs/sidebars.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ module.exports = {
3535
"require-timeout-settings",
3636
"ban-uncommitted-transaction",
3737
"require-enum-value-ordering",
38+
"require-table-schema",
3839
// xtask:new-rule:error-name
3940
],
4041
},

docs/src/pages/index.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -207,6 +207,11 @@ const rules = [
207207
tags: ["schema"],
208208
description: "Require BEFORE or AFTER when adding enum values",
209209
},
210+
{
211+
name: "require-table-schema",
212+
tags: ["schema"],
213+
description: "Require explicit schema in table DDL to avoid ambiguity.",
214+
},
210215
// xtask:new-rule:rule-doc-meta
211216
]
212217

0 commit comments

Comments
 (0)