Skip to content

Commit 6d904d8

Browse files
feat(rules): adding-required-field (#208)
1 parent fb67a55 commit 6d904d8

File tree

16 files changed

+205
-10
lines changed

16 files changed

+205
-10
lines changed

Diff for: crates/pglt_analyser/src/lint/safety.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
//! Generated file, do not edit by hand, see `xtask/codegen`
22
33
use pglt_analyse::declare_lint_group;
4+
pub mod adding_required_field;
45
pub mod ban_drop_column;
56
pub mod ban_drop_not_null;
67
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 ,] } }
8+
declare_lint_group! { pub Safety { name : "safety" , rules : [self :: adding_required_field :: AddingRequiredField , self :: ban_drop_column :: BanDropColumn , self :: ban_drop_not_null :: BanDropNotNull , self :: ban_drop_table :: BanDropTable ,] } }
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
use pglt_analyse::{context::RuleContext, declare_lint_rule, Rule, RuleDiagnostic, RuleSource};
2+
use pglt_console::markup;
3+
4+
declare_lint_rule! {
5+
/// Adding a new column that is NOT NULL and has no default value to an existing table effectively makes it required.
6+
///
7+
/// This will fail immediately upon running for any populated table. Furthermore, old application code that is unaware of this column will fail to INSERT to this table.
8+
///
9+
/// Make new columns optional initially by omitting the NOT NULL constraint until all existing data and application code has been updated. Once no NULL values are written to or persisted in the database, set it to NOT NULL.
10+
/// Alternatively, if using Postgres version 11 or later, add a DEFAULT value that is not volatile. This allows the column to keep its NOT NULL constraint.
11+
///
12+
/// ## Invalid
13+
/// alter table test add column count int not null;
14+
///
15+
/// ## Valid in Postgres >= 11
16+
/// alter table test add column count int not null default 0;
17+
pub AddingRequiredField {
18+
version: "next",
19+
name: "addingRequiredField",
20+
recommended: false,
21+
sources: &[RuleSource::Squawk("adding-required-field")],
22+
}
23+
}
24+
25+
impl Rule for AddingRequiredField {
26+
type Options = ();
27+
28+
fn run(ctx: &RuleContext<Self>) -> Vec<RuleDiagnostic> {
29+
let mut diagnostics = vec![];
30+
31+
if let pglt_query_ext::NodeEnum::AlterTableStmt(stmt) = ctx.stmt() {
32+
// We are currently lacking a way to check if a `AtAddColumn` subtype sets a
33+
// not null constraint – so we'll need to check the plain SQL.
34+
let plain_sql = ctx.stmt().to_ref().deparse().unwrap().to_ascii_lowercase();
35+
let is_nullable = !plain_sql.contains("not null");
36+
let has_set_default = plain_sql.contains("default");
37+
if is_nullable || has_set_default {
38+
return diagnostics;
39+
}
40+
41+
for cmd in &stmt.cmds {
42+
if let Some(pglt_query_ext::NodeEnum::AlterTableCmd(alter_table_cmd)) = &cmd.node {
43+
if alter_table_cmd.subtype()
44+
== pglt_query_ext::protobuf::AlterTableType::AtAddColumn
45+
{
46+
diagnostics.push(
47+
RuleDiagnostic::new(
48+
rule_category!(),
49+
None,
50+
markup! {
51+
"Adding a new column that is NOT NULL and has no default value to an existing table effectively makes it required."
52+
},
53+
)
54+
.detail(
55+
None,
56+
"Make new columns optional initially by omitting the NOT NULL constraint until all existing data and application code has been updated. Once no NULL values are written to or persisted in the database, set it to NOT NULL. Alternatively, if using Postgres version 11 or later, add a DEFAULT value that is not volatile. This allows the column to keep its NOT NULL constraint.
57+
",
58+
),
59+
);
60+
}
61+
}
62+
}
63+
}
64+
65+
diagnostics
66+
}
67+
}

Diff for: crates/pglt_analyser/src/options.rs

+2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
//! Generated file, do not edit by hand, see `xtask/codegen`
22
33
use crate::lint;
4+
pub type AddingRequiredField =
5+
<lint::safety::adding_required_field::AddingRequiredField as pglt_analyse::Rule>::Options;
46
pub type BanDropColumn =
57
<lint::safety::ban_drop_column::BanDropColumn as pglt_analyse::Rule>::Options;
68
pub type BanDropNotNull =
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
-- expect_only_lint/safety/addingRequiredField
2+
alter table test
3+
add column c int not null;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
---
2+
source: crates/pglt_analyser/tests/rules_tests.rs
3+
expression: snapshot
4+
---
5+
# Input
6+
```
7+
-- expect_only_lint/safety/addingRequiredField
8+
alter table test
9+
add column c int not null;
10+
```
11+
12+
# Diagnostics
13+
lint/safety/addingRequiredField ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
14+
15+
× Adding a new column that is NOT NULL and has no default value to an existing table effectively makes it required.
16+
17+
i Make new columns optional initially by omitting the NOT NULL constraint until all existing data and application code has been updated. Once no NULL values are written to or persisted in the database, set it to NOT NULL. Alternatively, if using Postgres version 11 or later, add a DEFAULT value that is not volatile. This allows the column to keep its NOT NULL constraint.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
-- expect_no_diagnostics
2+
alter table test
3+
add column c int not null default 0;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
---
2+
source: crates/pglt_analyser/tests/rules_tests.rs
3+
expression: snapshot
4+
---
5+
# Input
6+
```
7+
-- expect_no_diagnostics
8+
alter table test
9+
add column c int not null default 0;
10+
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
-- expect_no_diagnostics
2+
alter table test
3+
add column c int;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
---
2+
source: crates/pglt_analyser/tests/rules_tests.rs
3+
expression: snapshot
4+
---
5+
# Input
6+
```
7+
-- expect_no_diagnostics
8+
alter table test
9+
add column c int;
10+
```

Diff for: crates/pglt_configuration/src/analyser/linter/rules.rs

+32-9
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,10 @@ pub struct Safety {
143143
#[doc = r" It enables ALL rules for this group."]
144144
#[serde(skip_serializing_if = "Option::is_none")]
145145
pub all: Option<bool>,
146+
#[doc = "Adding a new column that is NOT NULL and has no default value to an existing table effectively makes it required."]
147+
#[serde(skip_serializing_if = "Option::is_none")]
148+
pub adding_required_field:
149+
Option<RuleConfiguration<pglt_analyser::options::AddingRequiredField>>,
146150
#[doc = "Dropping a column may break existing clients."]
147151
#[serde(skip_serializing_if = "Option::is_none")]
148152
pub ban_drop_column: Option<RuleConfiguration<pglt_analyser::options::BanDropColumn>>,
@@ -155,19 +159,24 @@ pub struct Safety {
155159
}
156160
impl Safety {
157161
const GROUP_NAME: &'static str = "safety";
158-
pub(crate) const GROUP_RULES: &'static [&'static str] =
159-
&["banDropColumn", "banDropNotNull", "banDropTable"];
162+
pub(crate) const GROUP_RULES: &'static [&'static str] = &[
163+
"addingRequiredField",
164+
"banDropColumn",
165+
"banDropNotNull",
166+
"banDropTable",
167+
];
160168
const RECOMMENDED_RULES: &'static [&'static str] =
161169
&["banDropColumn", "banDropNotNull", "banDropTable"];
162170
const RECOMMENDED_RULES_AS_FILTERS: &'static [RuleFilter<'static>] = &[
163-
RuleFilter::Rule(Self::GROUP_NAME, Self::GROUP_RULES[0]),
164171
RuleFilter::Rule(Self::GROUP_NAME, Self::GROUP_RULES[1]),
165172
RuleFilter::Rule(Self::GROUP_NAME, Self::GROUP_RULES[2]),
173+
RuleFilter::Rule(Self::GROUP_NAME, Self::GROUP_RULES[3]),
166174
];
167175
const ALL_RULES_AS_FILTERS: &'static [RuleFilter<'static>] = &[
168176
RuleFilter::Rule(Self::GROUP_NAME, Self::GROUP_RULES[0]),
169177
RuleFilter::Rule(Self::GROUP_NAME, Self::GROUP_RULES[1]),
170178
RuleFilter::Rule(Self::GROUP_NAME, Self::GROUP_RULES[2]),
179+
RuleFilter::Rule(Self::GROUP_NAME, Self::GROUP_RULES[3]),
171180
];
172181
#[doc = r" Retrieves the recommended rules"]
173182
pub(crate) fn is_recommended_true(&self) -> bool {
@@ -184,40 +193,50 @@ impl Safety {
184193
}
185194
pub(crate) fn get_enabled_rules(&self) -> FxHashSet<RuleFilter<'static>> {
186195
let mut index_set = FxHashSet::default();
187-
if let Some(rule) = self.ban_drop_column.as_ref() {
196+
if let Some(rule) = self.adding_required_field.as_ref() {
188197
if rule.is_enabled() {
189198
index_set.insert(RuleFilter::Rule(Self::GROUP_NAME, Self::GROUP_RULES[0]));
190199
}
191200
}
192-
if let Some(rule) = self.ban_drop_not_null.as_ref() {
201+
if let Some(rule) = self.ban_drop_column.as_ref() {
193202
if rule.is_enabled() {
194203
index_set.insert(RuleFilter::Rule(Self::GROUP_NAME, Self::GROUP_RULES[1]));
195204
}
196205
}
197-
if let Some(rule) = self.ban_drop_table.as_ref() {
206+
if let Some(rule) = self.ban_drop_not_null.as_ref() {
198207
if rule.is_enabled() {
199208
index_set.insert(RuleFilter::Rule(Self::GROUP_NAME, Self::GROUP_RULES[2]));
200209
}
201210
}
211+
if let Some(rule) = self.ban_drop_table.as_ref() {
212+
if rule.is_enabled() {
213+
index_set.insert(RuleFilter::Rule(Self::GROUP_NAME, Self::GROUP_RULES[3]));
214+
}
215+
}
202216
index_set
203217
}
204218
pub(crate) fn get_disabled_rules(&self) -> FxHashSet<RuleFilter<'static>> {
205219
let mut index_set = FxHashSet::default();
206-
if let Some(rule) = self.ban_drop_column.as_ref() {
220+
if let Some(rule) = self.adding_required_field.as_ref() {
207221
if rule.is_disabled() {
208222
index_set.insert(RuleFilter::Rule(Self::GROUP_NAME, Self::GROUP_RULES[0]));
209223
}
210224
}
211-
if let Some(rule) = self.ban_drop_not_null.as_ref() {
225+
if let Some(rule) = self.ban_drop_column.as_ref() {
212226
if rule.is_disabled() {
213227
index_set.insert(RuleFilter::Rule(Self::GROUP_NAME, Self::GROUP_RULES[1]));
214228
}
215229
}
216-
if let Some(rule) = self.ban_drop_table.as_ref() {
230+
if let Some(rule) = self.ban_drop_not_null.as_ref() {
217231
if rule.is_disabled() {
218232
index_set.insert(RuleFilter::Rule(Self::GROUP_NAME, Self::GROUP_RULES[2]));
219233
}
220234
}
235+
if let Some(rule) = self.ban_drop_table.as_ref() {
236+
if rule.is_disabled() {
237+
index_set.insert(RuleFilter::Rule(Self::GROUP_NAME, Self::GROUP_RULES[3]));
238+
}
239+
}
221240
index_set
222241
}
223242
#[doc = r" Checks if, given a rule name, matches one of the rules contained in this category"]
@@ -254,6 +273,10 @@ impl Safety {
254273
rule_name: &str,
255274
) -> Option<(RulePlainConfiguration, Option<RuleOptions>)> {
256275
match rule_name {
276+
"addingRequiredField" => self
277+
.adding_required_field
278+
.as_ref()
279+
.map(|conf| (conf.level(), conf.get_options())),
257280
"banDropColumn" => self
258281
.ban_drop_column
259282
.as_ref()

Diff for: crates/pglt_diagnostics_categories/src/categories.rs

+1
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
// must be between `define_categories! {\n` and `\n ;\n`.
1414

1515
define_categories! {
16+
"lint/safety/addingRequiredField": "https://pglt.dev/linter/rules/adding-required-field",
1617
"lint/safety/banDropColumn": "https://pglt.dev/linter/rules/ban-drop-column",
1718
"lint/safety/banDropNotNull": "https://pglt.dev/linter/rules/ban-drop-not-null",
1819
"lint/safety/banDropTable": "https://pglt.dev/linter/rules/ban-drop-table",

Diff for: docs/rule_sources.md

+1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
### Squawk
44
| Squawk Rule Name | Rule Name |
55
| ---- | ---- |
6+
| [adding-required-field](https://squawkhq.com/docs/adding-required-field) |[addingRequiredField](./rules/adding-required-field) |
67
| [ban-drop-column](https://squawkhq.com/docs/ban-drop-column) |[banDropColumn](./rules/ban-drop-column) |
78
| [ban-drop-not-null](https://squawkhq.com/docs/ban-drop-not-null) |[banDropNotNull](./rules/ban-drop-not-null) |
89
| [ban-drop-table](https://squawkhq.com/docs/ban-drop-table) |[banDropTable](./rules/ban-drop-table) |

Diff for: docs/rules.md

+1
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ Below the list of rules supported by Postgres Language Tools, divided by group.
1111
Rules that detect potential safety issues in your code.
1212
| Rule name | Description | Properties |
1313
| --- | --- | --- |
14+
| [addingRequiredField](./rules/adding-required-field) | Adding a new column that is NOT NULL and has no default value to an existing table effectively makes it required. | |
1415
| [banDropColumn](./rules/ban-drop-column) | Dropping a column may break existing clients. | <span class='inline-icon' title="This rule is recommended" ><Icon name="approve-check-circle" size="1.2rem" label="This rule is recommended" /></span> |
1516
| [banDropNotNull](./rules/ban-drop-not-null) | Dropping a NOT NULL constraint may break existing clients. | <span class='inline-icon' title="This rule is recommended" ><Icon name="approve-check-circle" size="1.2rem" label="This rule is recommended" /></span> |
1617
| [banDropTable](./rules/ban-drop-table) | Dropping a table may break existing clients. | <span class='inline-icon' title="This rule is recommended" ><Icon name="approve-check-circle" size="1.2rem" label="This rule is recommended" /></span> |

Diff for: docs/rules/adding-required-field.md

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
# addingRequiredField
2+
**Diagnostic Category: `lint/safety/addingRequiredField`**
3+
4+
**Since**: `vnext`
5+
6+
7+
**Sources**:
8+
- Inspired from: <a href="https://squawkhq.com/docs/adding-required-field" target="_blank"><code>squawk/adding-required-field</code></a>
9+
10+
## Description
11+
Adding a new column that is NOT NULL and has no default value to an existing table effectively makes it required.
12+
13+
This will fail immediately upon running for any populated table. Furthermore, old application code that is unaware of this column will fail to INSERT to this table.
14+
15+
Make new columns optional initially by omitting the NOT NULL constraint until all existing data and application code has been updated. Once no NULL values are written to or persisted in the database, set it to NOT NULL.
16+
Alternatively, if using Postgres version 11 or later, add a DEFAULT value that is not volatile. This allows the column to keep its NOT NULL constraint.
17+
18+
## Invalid
19+
20+
alter table test add column count int not null;
21+
22+
## Valid in Postgres >= 11
23+
24+
alter table test add column count int not null default 0;
25+
26+
## How to configure
27+
```toml title="pglt.toml"
28+
[linter.rules.safety]
29+
addingRequiredField = "error"
30+
31+
```

Diff for: docs/schemas/0.0.0/schema.json

+11
Original file line numberDiff line numberDiff line change
@@ -292,6 +292,17 @@
292292
"description": "A list of rules that belong to this group",
293293
"type": "object",
294294
"properties": {
295+
"addingRequiredField": {
296+
"description": "Adding a new column that is NOT NULL and has no default value to an existing table effectively makes it required.",
297+
"anyOf": [
298+
{
299+
"$ref": "#/definitions/RuleConfiguration"
300+
},
301+
{
302+
"type": "null"
303+
}
304+
]
305+
},
295306
"all": {
296307
"description": "It enables ALL rules for this group.",
297308
"type": [

Diff for: docs/schemas/latest/schema.json

+11
Original file line numberDiff line numberDiff line change
@@ -292,6 +292,17 @@
292292
"description": "A list of rules that belong to this group",
293293
"type": "object",
294294
"properties": {
295+
"addingRequiredField": {
296+
"description": "Adding a new column that is NOT NULL and has no default value to an existing table effectively makes it required.",
297+
"anyOf": [
298+
{
299+
"$ref": "#/definitions/RuleConfiguration"
300+
},
301+
{
302+
"type": "null"
303+
}
304+
]
305+
},
295306
"all": {
296307
"description": "It enables ALL rules for this group.",
297308
"type": [

0 commit comments

Comments
 (0)