You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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;
"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.
Copy file name to clipboardExpand all lines: docs/rules.md
+1
Original file line number
Diff line number
Diff line change
@@ -11,6 +11,7 @@ Below the list of rules supported by Postgres Language Tools, divided by group.
11
11
Rules that detect potential safety issues in your code.
12
12
| Rule name | Description | Properties |
13
13
| --- | --- | --- |
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. ||
14
15
|[banDropColumn](./rules/ban-drop-column)| Dropping a column may break existing clients. | <spanclass='inline-icon'title="This rule is recommended" ><Iconname="approve-check-circle"size="1.2rem"label="This rule is recommended" /></span> |
15
16
|[banDropNotNull](./rules/ban-drop-not-null)| Dropping a NOT NULL constraint may break existing clients. | <spanclass='inline-icon'title="This rule is recommended" ><Iconname="approve-check-circle"size="1.2rem"label="This rule is recommended" /></span> |
16
17
|[banDropTable](./rules/ban-drop-table)| Dropping a table may break existing clients. | <spanclass='inline-icon'title="This rule is recommended" ><Iconname="approve-check-circle"size="1.2rem"label="This rule is recommended" /></span> |
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;
0 commit comments