Skip to content

Commit a985fe9

Browse files
feat(rules): add ban-drop-table
1 parent 150f1a2 commit a985fe9

File tree

8 files changed

+76
-2
lines changed

8 files changed

+76
-2
lines changed

crates/pglt_analyser/src/lint.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
//! Generated file, do not edit by hand, see `xtask/codegen`
22
33
pub mod safety;
4-
::pglt_analyse::declare_category! { pub Lint { kind : Lint , groups : [self :: safety :: Safety ,] } }
4+
::pglt_analyse::declare_category! { pub Lint { kind : Lint , groups : [self :: safety :: Safety , ] } }

crates/pglt_analyser/src/lint/safety.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,5 @@
33
use pglt_analyse::declare_lint_group;
44
pub mod ban_drop_column;
55
pub mod ban_drop_not_null;
6-
declare_lint_group! { pub Safety { name : "safety" , rules : [self :: ban_drop_column :: BanDropColumn , self :: ban_drop_not_null :: BanDropNotNull ,] } }
6+
pub mod ban_drop_table;
7+
declare_lint_group! { pub Safety { name : "safety" , rules : [self :: ban_drop_column :: BanDropColumn , self :: ban_drop_not_null :: BanDropNotNull , self :: ban_drop_table :: BanDropTable ,] } }
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
use pglt_analyse::{context::RuleContext, declare_lint_rule, Rule, RuleDiagnostic, RuleSource};
2+
use pglt_console::markup;
3+
4+
declare_lint_rule! {
5+
/// Dropping a table may break existing clients.
6+
///
7+
/// Update your application code to no longer read or write the table.
8+
///
9+
/// Once the table is no longer needed, you can delete it by running the command "DROP TABLE mytable;".
10+
///
11+
/// This command will permanently remove the table from the database and all its contents.
12+
/// Be sure to back up the table before deleting it, just in case you need to restore it in the future.
13+
///
14+
/// ## Examples
15+
/// ```sql,expect_diagnostic
16+
/// drop table some_table;
17+
/// ```
18+
pub BanDropTable {
19+
version: "next",
20+
name: "banDropTable",
21+
recommended: false,
22+
sources: &[RuleSource::Squawk("ban-drop-table")],
23+
}
24+
}
25+
26+
impl Rule for BanDropTable {
27+
type Options = ();
28+
29+
fn run(ctx: &RuleContext<Self>) -> Vec<RuleDiagnostic> {
30+
let mut diagnostics = vec![];
31+
32+
if let pglt_query_ext::NodeEnum::DropStmt(stmt) = &ctx.stmt() {
33+
if stmt.remove_type() == pglt_query_ext::protobuf::ObjectType::ObjectTable {
34+
diagnostics.push(
35+
RuleDiagnostic::new(
36+
rule_category!(),
37+
None,
38+
markup! {
39+
"Dropping a table may break existing clients."
40+
},
41+
)
42+
.detail(
43+
None,
44+
"Update your application code to no longer read or write the table, and only then delete the table. Be sure to create a backup.",
45+
),
46+
);
47+
}
48+
}
49+
50+
diagnostics
51+
}
52+
}

crates/pglt_analyser/src/options.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,4 @@ pub type BanDropColumn =
55
<lint::safety::ban_drop_column::BanDropColumn as pglt_analyse::Rule>::Options;
66
pub type BanDropNotNull =
77
<lint::safety::ban_drop_not_null::BanDropNotNull as pglt_analyse::Rule>::Options;
8+
pub type BanDropTable = <lint::safety::ban_drop_table::BanDropTable as pglt_analyse::Rule>::Options;
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
-- expect_only_lint/safety/banDropTable
2+
drop table test;
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
---
2+
source: crates/pglt_analyser/tests/rules_tests.rs
3+
expression: snapshot
4+
---
5+
# Input
6+
```
7+
-- expect_only_lint/safety/banDropTable
8+
drop table test;
9+
```
10+
11+
# Diagnostics
12+
lint/safety/banDropTable ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
13+
14+
× Dropping a table may break existing clients.
15+
16+
i Update your application code to no longer read or write the table, and only then delete the table. Be sure to create a backup.

crates/pglt_configuration/src/analyser/linter/rules.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ pub struct Rules {
5454
pub all: Option<bool>,
5555
#[serde(skip_serializing_if = "Option::is_none")]
5656
pub safety: Option<Safety>,
57+
#[serde(skip_serializing_if = "Option::is_none")]
5758
}
5859
impl Rules {
5960
#[doc = r" Checks if the code coming from [pglt_diagnostics::Diagnostic] corresponds to a rule."]

crates/pglt_diagnostics_categories/src/categories.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
define_categories! {
1616
"lint/safety/banDropColumn": "https://pglt.dev/linter/rules/ban-drop-column",
1717
"lint/safety/banDropNotNull": "https://pglt.dev/linter/rules/ban-drop-not-null",
18+
"lint/safety/banDropTable": "https://pglt.dev/linter/rules/ban-drop-table",
1819
// end lint rules
1920
;
2021
// General categories

0 commit comments

Comments
 (0)