Skip to content

Commit afbe1c2

Browse files
committed
progress
1 parent f0d8565 commit afbe1c2

File tree

4 files changed

+71
-71
lines changed

4 files changed

+71
-71
lines changed

crates/pgt_analyser/src/lint/safety/adding_foreign_key_constraint.rs

Lines changed: 36 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -53,48 +53,48 @@ impl Rule for AddingForeignKeyConstraint {
5353
if let Some(pgt_query::NodeEnum::AlterTableCmd(cmd)) = &cmd.node {
5454
match cmd.subtype() {
5555
pgt_query::protobuf::AlterTableType::AtAddConstraint => {
56-
if let Some(def) = cmd.def.as_ref().and_then(|d| d.node.as_ref()) {
57-
if let pgt_query::NodeEnum::Constraint(constraint) = def {
58-
// check if it's a foreign key constraint
59-
if constraint.contype()
60-
== pgt_query::protobuf::ConstrType::ConstrForeign
61-
{
62-
// it is okay if NOT VALID is specified (skip_validation = true)
63-
if !constraint.skip_validation {
64-
diagnostics.push(RuleDiagnostic::new(
65-
rule_category!(),
66-
None,
67-
markup! {
68-
"Adding a foreign key constraint requires a table scan and locks on both tables."
69-
},
70-
).detail(None, "This will block writes to both the referencing and referenced tables while PostgreSQL verifies the constraint.")
71-
.note("Add the constraint as NOT VALID first, then VALIDATE it in a separate transaction."));
72-
}
56+
if let Some(pgt_query::NodeEnum::Constraint(constraint)) =
57+
cmd.def.as_ref().and_then(|d| d.node.as_ref())
58+
{
59+
// check if it's a foreign key constraint
60+
if constraint.contype()
61+
== pgt_query::protobuf::ConstrType::ConstrForeign
62+
{
63+
// it is okay if NOT VALID is specified (skip_validation = true)
64+
if !constraint.skip_validation {
65+
diagnostics.push(RuleDiagnostic::new(
66+
rule_category!(),
67+
None,
68+
markup! {
69+
"Adding a foreign key constraint requires a table scan and locks on both tables."
70+
},
71+
).detail(None, "This will block writes to both the referencing and referenced tables while PostgreSQL verifies the constraint.")
72+
.note("Add the constraint as NOT VALID first, then VALIDATE it in a separate transaction."));
7373
}
7474
}
7575
}
7676
}
7777
pgt_query::protobuf::AlterTableType::AtAddColumn => {
78-
if let Some(def) = cmd.def.as_ref().and_then(|d| d.node.as_ref()) {
79-
if let pgt_query::NodeEnum::ColumnDef(col_def) = def {
80-
// check constraints on the column
81-
for constraint in &col_def.constraints {
82-
if let Some(pgt_query::NodeEnum::Constraint(constr)) =
83-
&constraint.node
78+
if let Some(pgt_query::NodeEnum::ColumnDef(col_def)) =
79+
cmd.def.as_ref().and_then(|d| d.node.as_ref())
80+
{
81+
// check constraints on the column
82+
for constraint in &col_def.constraints {
83+
if let Some(pgt_query::NodeEnum::Constraint(constr)) =
84+
&constraint.node
85+
{
86+
if constr.contype()
87+
== pgt_query::protobuf::ConstrType::ConstrForeign
88+
&& !constr.skip_validation
8489
{
85-
if constr.contype()
86-
== pgt_query::protobuf::ConstrType::ConstrForeign
87-
&& !constr.skip_validation
88-
{
89-
diagnostics.push(RuleDiagnostic::new(
90-
rule_category!(),
91-
None,
92-
markup! {
93-
"Adding a column with a foreign key constraint requires a table scan and locks."
94-
},
95-
).detail(None, "Using REFERENCES when adding a column will block writes while verifying the constraint.")
96-
.note("Add the column without the constraint first, then add the constraint as NOT VALID and VALIDATE it separately."));
97-
}
90+
diagnostics.push(RuleDiagnostic::new(
91+
rule_category!(),
92+
None,
93+
markup! {
94+
"Adding a column with a foreign key constraint requires a table scan and locks."
95+
},
96+
).detail(None, "Using REFERENCES when adding a column will block writes while verifying the constraint.")
97+
.note("Add the column without the constraint first, then add the constraint as NOT VALID and VALIDATE it separately."));
9898
}
9999
}
100100
}

crates/pgt_analyser/src/lint/safety/adding_primary_key_constraint.rs

Lines changed: 33 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -51,45 +51,45 @@ impl Rule for AddingPrimaryKeyConstraint {
5151
match cmd.subtype() {
5252
// Check for ADD CONSTRAINT PRIMARY KEY
5353
pgt_query::protobuf::AlterTableType::AtAddConstraint => {
54-
if let Some(def) = cmd.def.as_ref().and_then(|d| d.node.as_ref()) {
55-
if let pgt_query::NodeEnum::Constraint(constraint) = def {
56-
if constraint.contype()
57-
== pgt_query::protobuf::ConstrType::ConstrPrimary
58-
&& constraint.indexname.is_empty()
59-
{
60-
diagnostics.push(RuleDiagnostic::new(
61-
rule_category!(),
62-
None,
63-
markup! {
64-
"Adding a PRIMARY KEY constraint results in locks and table rewrites."
65-
},
66-
).detail(None, "Adding a PRIMARY KEY constraint requires an ACCESS EXCLUSIVE lock which blocks reads.")
67-
.note("Add the PRIMARY KEY constraint USING an index."));
68-
}
54+
if let Some(pgt_query::NodeEnum::Constraint(constraint)) =
55+
cmd.def.as_ref().and_then(|d| d.node.as_ref())
56+
{
57+
if constraint.contype()
58+
== pgt_query::protobuf::ConstrType::ConstrPrimary
59+
&& constraint.indexname.is_empty()
60+
{
61+
diagnostics.push(RuleDiagnostic::new(
62+
rule_category!(),
63+
None,
64+
markup! {
65+
"Adding a PRIMARY KEY constraint results in locks and table rewrites."
66+
},
67+
).detail(None, "Adding a PRIMARY KEY constraint requires an ACCESS EXCLUSIVE lock which blocks reads.")
68+
.note("Add the PRIMARY KEY constraint USING an index."));
6969
}
7070
}
7171
}
7272
// Check for ADD COLUMN with PRIMARY KEY
7373
pgt_query::protobuf::AlterTableType::AtAddColumn => {
74-
if let Some(def) = cmd.def.as_ref().and_then(|d| d.node.as_ref()) {
75-
if let pgt_query::NodeEnum::ColumnDef(col_def) = def {
76-
for constraint in &col_def.constraints {
77-
if let Some(pgt_query::NodeEnum::Constraint(constr)) =
78-
&constraint.node
74+
if let Some(pgt_query::NodeEnum::ColumnDef(col_def)) =
75+
cmd.def.as_ref().and_then(|d| d.node.as_ref())
76+
{
77+
for constraint in &col_def.constraints {
78+
if let Some(pgt_query::NodeEnum::Constraint(constr)) =
79+
&constraint.node
80+
{
81+
if constr.contype()
82+
== pgt_query::protobuf::ConstrType::ConstrPrimary
83+
&& constr.indexname.is_empty()
7984
{
80-
if constr.contype()
81-
== pgt_query::protobuf::ConstrType::ConstrPrimary
82-
&& constr.indexname.is_empty()
83-
{
84-
diagnostics.push(RuleDiagnostic::new(
85-
rule_category!(),
86-
None,
87-
markup! {
88-
"Adding a PRIMARY KEY constraint results in locks and table rewrites."
89-
},
90-
).detail(None, "Adding a PRIMARY KEY constraint requires an ACCESS EXCLUSIVE lock which blocks reads.")
91-
.note("Add the PRIMARY KEY constraint USING an index."));
92-
}
85+
diagnostics.push(RuleDiagnostic::new(
86+
rule_category!(),
87+
None,
88+
markup! {
89+
"Adding a PRIMARY KEY constraint results in locks and table rewrites."
90+
},
91+
).detail(None, "Adding a PRIMARY KEY constraint requires an ACCESS EXCLUSIVE lock which blocks reads.")
92+
.note("Add the PRIMARY KEY constraint USING an index."));
9393
}
9494
}
9595
}

crates/pgt_analyser/src/lint/safety/disallow_unique_constraint.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ impl Rule for DisallowUniqueConstraint {
5555
create
5656
.relation
5757
.as_ref()
58-
.map_or(false, |r| &r.relname == table_name)
58+
.is_some_and(|r| &r.relname == table_name)
5959
} else {
6060
false
6161
}

crates/pgt_analyser/src/lint/safety/prefer_big_int.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ fn check_column_def(
117117
"Using smaller integer types can lead to overflow issues."
118118
},
119119
)
120-
.detail(None, &format!("The '{}' type has a limited range that may be exceeded as your data grows.", name.sval))
120+
.detail(None, format!("The '{}' type has a limited range that may be exceeded as your data grows.", name.sval))
121121
.note("Consider using BIGINT for integer columns to avoid future migration issues."),
122122
);
123123
}

0 commit comments

Comments
 (0)