Skip to content

Commit 36a2730

Browse files
generate manually
1 parent 2f2fb2e commit 36a2730

File tree

5 files changed

+55
-0
lines changed

5 files changed

+55
-0
lines changed

docs/rule_sources.md

Lines changed: 1 addition & 0 deletions
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) |

docs/rules.md

Lines changed: 1 addition & 0 deletions
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> |
Lines changed: 31 additions & 0 deletions
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+
```

docs/schemas/0.0.0/schema.json

Lines changed: 11 additions & 0 deletions
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": [

docs/schemas/latest/schema.json

Lines changed: 11 additions & 0 deletions
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)