-
Notifications
You must be signed in to change notification settings - Fork 102
feat(rules): adding-required-field #208
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
psteinroe
merged 4 commits into
supabase-community:main
from
juleswritescode:rule/adding-required-field
Feb 20, 2025
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,8 @@ | ||
//! Generated file, do not edit by hand, see `xtask/codegen` | ||
|
||
use pglt_analyse::declare_lint_group; | ||
pub mod adding_required_field; | ||
pub mod ban_drop_column; | ||
pub mod ban_drop_not_null; | ||
pub mod ban_drop_table; | ||
declare_lint_group! { pub Safety { name : "safety" , rules : [self :: ban_drop_column :: BanDropColumn , self :: ban_drop_not_null :: BanDropNotNull , self :: ban_drop_table :: BanDropTable ,] } } | ||
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 ,] } } |
67 changes: 67 additions & 0 deletions
67
crates/pglt_analyser/src/lint/safety/adding_required_field.rs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
use pglt_analyse::{context::RuleContext, declare_lint_rule, Rule, RuleDiagnostic, RuleSource}; | ||
use pglt_console::markup; | ||
|
||
declare_lint_rule! { | ||
/// Adding a new column that is NOT NULL and has no default value to an existing table effectively makes it required. | ||
/// | ||
/// 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. | ||
/// | ||
/// 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. | ||
/// | ||
/// ## Invalid | ||
/// alter table test add column count int not null; | ||
/// | ||
/// ## Valid in Postgres >= 11 | ||
/// alter table test add column count int not null default 0; | ||
pub AddingRequiredField { | ||
version: "next", | ||
name: "addingRequiredField", | ||
recommended: false, | ||
sources: &[RuleSource::Squawk("adding-required-field")], | ||
} | ||
} | ||
|
||
impl Rule for AddingRequiredField { | ||
type Options = (); | ||
|
||
fn run(ctx: &RuleContext<Self>) -> Vec<RuleDiagnostic> { | ||
let mut diagnostics = vec![]; | ||
|
||
if let pglt_query_ext::NodeEnum::AlterTableStmt(stmt) = ctx.stmt() { | ||
// We are currently lacking a way to check if a `AtAddColumn` subtype sets a | ||
// not null constraint – so we'll need to check the plain SQL. | ||
let plain_sql = ctx.stmt().to_ref().deparse().unwrap().to_ascii_lowercase(); | ||
let is_nullable = !plain_sql.contains("not null"); | ||
let has_set_default = plain_sql.contains("default"); | ||
if is_nullable || has_set_default { | ||
return diagnostics; | ||
} | ||
|
||
for cmd in &stmt.cmds { | ||
if let Some(pglt_query_ext::NodeEnum::AlterTableCmd(alter_table_cmd)) = &cmd.node { | ||
if alter_table_cmd.subtype() | ||
== pglt_query_ext::protobuf::AlterTableType::AtAddColumn | ||
{ | ||
diagnostics.push( | ||
RuleDiagnostic::new( | ||
rule_category!(), | ||
None, | ||
markup! { | ||
"Adding a new column that is NOT NULL and has no default value to an existing table effectively makes it required." | ||
}, | ||
) | ||
.detail( | ||
None, | ||
"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. | ||
", | ||
), | ||
); | ||
} | ||
} | ||
} | ||
} | ||
|
||
diagnostics | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
3 changes: 3 additions & 0 deletions
3
crates/pglt_analyser/tests/specs/safety/addingRequiredField/basic.sql
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
-- expect_only_lint/safety/addingRequiredField | ||
alter table test | ||
add column c int not null; |
17 changes: 17 additions & 0 deletions
17
crates/pglt_analyser/tests/specs/safety/addingRequiredField/basic.sql.snap
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
--- | ||
source: crates/pglt_analyser/tests/rules_tests.rs | ||
expression: snapshot | ||
--- | ||
# Input | ||
``` | ||
-- expect_only_lint/safety/addingRequiredField | ||
alter table test | ||
add column c int not null; | ||
``` | ||
|
||
# Diagnostics | ||
lint/safety/addingRequiredField ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ | ||
|
||
× Adding a new column that is NOT NULL and has no default value to an existing table effectively makes it required. | ||
|
||
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. |
3 changes: 3 additions & 0 deletions
3
crates/pglt_analyser/tests/specs/safety/addingRequiredField/with_default.sql
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
-- expect_no_diagnostics | ||
alter table test | ||
add column c int not null default 0; |
10 changes: 10 additions & 0 deletions
10
crates/pglt_analyser/tests/specs/safety/addingRequiredField/with_default.sql.snap
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
--- | ||
source: crates/pglt_analyser/tests/rules_tests.rs | ||
expression: snapshot | ||
--- | ||
# Input | ||
``` | ||
-- expect_no_diagnostics | ||
alter table test | ||
add column c int not null default 0; | ||
``` |
3 changes: 3 additions & 0 deletions
3
crates/pglt_analyser/tests/specs/safety/addingRequiredField/without_required.sql
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
-- expect_no_diagnostics | ||
alter table test | ||
add column c int; |
10 changes: 10 additions & 0 deletions
10
crates/pglt_analyser/tests/specs/safety/addingRequiredField/without_required.sql.snap
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
--- | ||
source: crates/pglt_analyser/tests/rules_tests.rs | ||
expression: snapshot | ||
--- | ||
# Input | ||
``` | ||
-- expect_no_diagnostics | ||
alter table test | ||
add column c int; | ||
``` |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
# addingRequiredField | ||
**Diagnostic Category: `lint/safety/addingRequiredField`** | ||
|
||
**Since**: `vnext` | ||
|
||
|
||
**Sources**: | ||
- Inspired from: <a href="https://squawkhq.com/docs/adding-required-field" target="_blank"><code>squawk/adding-required-field</code></a> | ||
|
||
## Description | ||
Adding a new column that is NOT NULL and has no default value to an existing table effectively makes it required. | ||
|
||
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. | ||
|
||
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. | ||
|
||
## Invalid | ||
|
||
alter table test add column count int not null; | ||
|
||
## Valid in Postgres >= 11 | ||
|
||
alter table test add column count int not null default 0; | ||
|
||
## How to configure | ||
```toml title="pglt.toml" | ||
[linter.rules.safety] | ||
addingRequiredField = "error" | ||
|
||
``` |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
we should pass the current postgres version to the rules!