Skip to content

Commit

Permalink
sql/schemachanger: constraint name is not populated in errors
Browse files Browse the repository at this point in the history
Previously, the declarative schema changer set, the constraint name
causing errors to have place holders. To address this, this patch
will set the constraint name when the constraint becomes visible
and impacts DML statements.

Release note: None
Epic: None
  • Loading branch information
fqazi committed Mar 17, 2023
1 parent d6bef5e commit 0f36130
Show file tree
Hide file tree
Showing 80 changed files with 1,791 additions and 1,036 deletions.
147 changes: 134 additions & 13 deletions pkg/cli/testdata/declarative-rules/deprules
Original file line number Diff line number Diff line change
Expand Up @@ -293,6 +293,114 @@ deprules
- descriptorIsNotBeingDropped-23.1($prev)
- joinTargetNode($prev, $prev-Target, $prev-Node)
- joinTargetNode($next, $next-Target, $next-Node)
- name: Constraint should be hidden before name
from: constraint-name-Node
kind: SameStagePrecedence
to: constraint-Node
query:
- $constraint-name[Type] = '*scpb.ConstraintWithoutIndexName'
- $constraint[Type] IN ['*scpb.UniqueWithoutIndexConstraint', '*scpb.UniqueWithoutIndexConstraintUnvalidated', '*scpb.CheckConstraint', '*scpb.CheckConstraintUnvalidated', '*scpb.ForeignKeyConstraint', '*scpb.ForeignKeyConstraintUnvalidated', '*scpb.ColumnNotNull']
- joinOnConstraintID($constraint-name, $constraint, $table-id, $constraint-id)
- toAbsent($constraint-name-Target, $constraint-Target)
- $constraint-name-Node[CurrentStatus] = ABSENT
- $constraint-Node[CurrentStatus] = ABSENT
- joinTargetNode($constraint-name, $constraint-name-Target, $constraint-name-Node)
- joinTargetNode($constraint, $constraint-Target, $constraint-Node)
- name: Constraint should be hidden before name
from: constraint-name-Node
kind: SameStagePrecedence
to: constraint-Node
query:
- $constraint-name[Type] = '*scpb.ConstraintWithoutIndexName'
- $constraint[Type] IN ['*scpb.UniqueWithoutIndexConstraint', '*scpb.UniqueWithoutIndexConstraintUnvalidated', '*scpb.CheckConstraint', '*scpb.CheckConstraintUnvalidated', '*scpb.ForeignKeyConstraint', '*scpb.ForeignKeyConstraintUnvalidated', '*scpb.ColumnNotNull']
- joinOnConstraintID($constraint-name, $constraint, $table-id, $constraint-id)
- transient($constraint-name-Target, $constraint-Target)
- $constraint-name-Node[CurrentStatus] = TRANSIENT_ABSENT
- $constraint-Node[CurrentStatus] = TRANSIENT_ABSENT
- joinTargetNode($constraint-name, $constraint-name-Target, $constraint-name-Node)
- joinTargetNode($constraint, $constraint-Target, $constraint-Node)
- name: Constraint should be hidden before name
from: constraint-name-Node
kind: SameStagePrecedence
to: constraint-Node
query:
- $constraint-name[Type] = '*scpb.ConstraintWithoutIndexName'
- $constraint[Type] IN ['*scpb.UniqueWithoutIndexConstraint', '*scpb.UniqueWithoutIndexConstraintUnvalidated', '*scpb.CheckConstraint', '*scpb.CheckConstraintUnvalidated', '*scpb.ForeignKeyConstraint', '*scpb.ForeignKeyConstraintUnvalidated', '*scpb.ColumnNotNull']
- joinOnConstraintID($constraint-name, $constraint, $table-id, $constraint-id)
- $constraint-name-Target[TargetStatus] = TRANSIENT_ABSENT
- $constraint-name-Node[CurrentStatus] = TRANSIENT_ABSENT
- $constraint-Target[TargetStatus] = ABSENT
- $constraint-Node[CurrentStatus] = ABSENT
- joinTargetNode($constraint-name, $constraint-name-Target, $constraint-name-Node)
- joinTargetNode($constraint, $constraint-Target, $constraint-Node)
- name: Constraint should be hidden before name
from: constraint-name-Node
kind: SameStagePrecedence
to: constraint-Node
query:
- $constraint-name[Type] = '*scpb.ConstraintWithoutIndexName'
- $constraint[Type] IN ['*scpb.UniqueWithoutIndexConstraint', '*scpb.UniqueWithoutIndexConstraintUnvalidated', '*scpb.CheckConstraint', '*scpb.CheckConstraintUnvalidated', '*scpb.ForeignKeyConstraint', '*scpb.ForeignKeyConstraintUnvalidated', '*scpb.ColumnNotNull']
- joinOnConstraintID($constraint-name, $constraint, $table-id, $constraint-id)
- $constraint-name-Target[TargetStatus] = ABSENT
- $constraint-name-Node[CurrentStatus] = ABSENT
- $constraint-Target[TargetStatus] = TRANSIENT_ABSENT
- $constraint-Node[CurrentStatus] = TRANSIENT_ABSENT
- joinTargetNode($constraint-name, $constraint-name-Target, $constraint-name-Node)
- joinTargetNode($constraint, $constraint-Target, $constraint-Node)
- name: Constraint should be hidden before name
from: constraint-Node
kind: Precedence
to: constraint-name-Node
query:
- $constraint[Type] IN ['*scpb.UniqueWithoutIndexConstraint', '*scpb.UniqueWithoutIndexConstraintUnvalidated', '*scpb.CheckConstraint', '*scpb.CheckConstraintUnvalidated', '*scpb.ForeignKeyConstraint', '*scpb.ForeignKeyConstraintUnvalidated', '*scpb.ColumnNotNull']
- $constraint-name[Type] = '*scpb.ConstraintWithoutIndexName'
- joinOnConstraintID($constraint, $constraint-name, $table-id, $constraint-id)
- toAbsent($constraint-Target, $constraint-name-Target)
- $constraint-Node[CurrentStatus] = VALIDATED
- $constraint-name-Node[CurrentStatus] = ABSENT
- joinTargetNode($constraint, $constraint-Target, $constraint-Node)
- joinTargetNode($constraint-name, $constraint-name-Target, $constraint-name-Node)
- name: Constraint should be hidden before name
from: constraint-Node
kind: Precedence
to: constraint-name-Node
query:
- $constraint[Type] IN ['*scpb.UniqueWithoutIndexConstraint', '*scpb.UniqueWithoutIndexConstraintUnvalidated', '*scpb.CheckConstraint', '*scpb.CheckConstraintUnvalidated', '*scpb.ForeignKeyConstraint', '*scpb.ForeignKeyConstraintUnvalidated', '*scpb.ColumnNotNull']
- $constraint-name[Type] = '*scpb.ConstraintWithoutIndexName'
- joinOnConstraintID($constraint, $constraint-name, $table-id, $constraint-id)
- transient($constraint-Target, $constraint-name-Target)
- $constraint-Node[CurrentStatus] = TRANSIENT_VALIDATED
- $constraint-name-Node[CurrentStatus] = TRANSIENT_ABSENT
- joinTargetNode($constraint, $constraint-Target, $constraint-Node)
- joinTargetNode($constraint-name, $constraint-name-Target, $constraint-name-Node)
- name: Constraint should be hidden before name
from: constraint-Node
kind: Precedence
to: constraint-name-Node
query:
- $constraint[Type] IN ['*scpb.UniqueWithoutIndexConstraint', '*scpb.UniqueWithoutIndexConstraintUnvalidated', '*scpb.CheckConstraint', '*scpb.CheckConstraintUnvalidated', '*scpb.ForeignKeyConstraint', '*scpb.ForeignKeyConstraintUnvalidated', '*scpb.ColumnNotNull']
- $constraint-name[Type] = '*scpb.ConstraintWithoutIndexName'
- joinOnConstraintID($constraint, $constraint-name, $table-id, $constraint-id)
- $constraint-Target[TargetStatus] = TRANSIENT_ABSENT
- $constraint-Node[CurrentStatus] = TRANSIENT_VALIDATED
- $constraint-name-Target[TargetStatus] = ABSENT
- $constraint-name-Node[CurrentStatus] = ABSENT
- joinTargetNode($constraint, $constraint-Target, $constraint-Node)
- joinTargetNode($constraint-name, $constraint-name-Target, $constraint-name-Node)
- name: Constraint should be hidden before name
from: constraint-Node
kind: Precedence
to: constraint-name-Node
query:
- $constraint[Type] IN ['*scpb.UniqueWithoutIndexConstraint', '*scpb.UniqueWithoutIndexConstraintUnvalidated', '*scpb.CheckConstraint', '*scpb.CheckConstraintUnvalidated', '*scpb.ForeignKeyConstraint', '*scpb.ForeignKeyConstraintUnvalidated', '*scpb.ColumnNotNull']
- $constraint-name[Type] = '*scpb.ConstraintWithoutIndexName'
- joinOnConstraintID($constraint, $constraint-name, $table-id, $constraint-id)
- $constraint-Target[TargetStatus] = ABSENT
- $constraint-Node[CurrentStatus] = VALIDATED
- $constraint-name-Target[TargetStatus] = TRANSIENT_ABSENT
- $constraint-name-Node[CurrentStatus] = TRANSIENT_ABSENT
- joinTargetNode($constraint, $constraint-Target, $constraint-Node)
- joinTargetNode($constraint-name, $constraint-name-Target, $constraint-name-Node)
- name: DEFAULT or ON UPDATE existence precedes writes to column
from: expr-Node
kind: Precedence
Expand Down Expand Up @@ -1779,7 +1887,7 @@ deprules
kind: SameStagePrecedence
to: complex-constraint-Node
query:
- $dependent[Type] IN ['*scpb.ConstraintWithoutIndexName', '*scpb.ConstraintComment']
- $dependent[Type] = '*scpb.ConstraintComment'
- $complex-constraint[Type] IN ['*scpb.UniqueWithoutIndexConstraint', '*scpb.CheckConstraint', '*scpb.ForeignKeyConstraint', '*scpb.ColumnNotNull']
- joinOnConstraintID($dependent, $complex-constraint, $table-id, $constraint-id)
- ToPublicOrTransient($dependent-Target, $complex-constraint-Target)
Expand All @@ -1793,7 +1901,7 @@ deprules
to: dependent-Node
query:
- $constraint[Type] IN ['*scpb.UniqueWithoutIndexConstraint', '*scpb.CheckConstraint', '*scpb.ForeignKeyConstraint', '*scpb.ColumnNotNull']
- $dependent[Type] IN ['*scpb.ConstraintWithoutIndexName', '*scpb.ConstraintComment']
- $dependent[Type] = '*scpb.ConstraintComment'
- joinOnConstraintID($constraint, $dependent, $table-id, $constraint-id)
- toAbsent($constraint-Target, $dependent-Target)
- $constraint-Node[CurrentStatus] = VALIDATED
Expand All @@ -1806,7 +1914,7 @@ deprules
to: dependent-Node
query:
- $constraint[Type] IN ['*scpb.UniqueWithoutIndexConstraint', '*scpb.CheckConstraint', '*scpb.ForeignKeyConstraint', '*scpb.ColumnNotNull']
- $dependent[Type] IN ['*scpb.ConstraintWithoutIndexName', '*scpb.ConstraintComment']
- $dependent[Type] = '*scpb.ConstraintComment'
- joinOnConstraintID($constraint, $dependent, $table-id, $constraint-id)
- transient($constraint-Target, $dependent-Target)
- $constraint-Node[CurrentStatus] = TRANSIENT_VALIDATED
Expand All @@ -1819,7 +1927,7 @@ deprules
to: dependent-Node
query:
- $constraint[Type] IN ['*scpb.UniqueWithoutIndexConstraint', '*scpb.CheckConstraint', '*scpb.ForeignKeyConstraint', '*scpb.ColumnNotNull']
- $dependent[Type] IN ['*scpb.ConstraintWithoutIndexName', '*scpb.ConstraintComment']
- $dependent[Type] = '*scpb.ConstraintComment'
- joinOnConstraintID($constraint, $dependent, $table-id, $constraint-id)
- $constraint-Target[TargetStatus] = TRANSIENT_ABSENT
- $constraint-Node[CurrentStatus] = TRANSIENT_VALIDATED
Expand All @@ -1833,7 +1941,7 @@ deprules
to: dependent-Node
query:
- $constraint[Type] IN ['*scpb.UniqueWithoutIndexConstraint', '*scpb.CheckConstraint', '*scpb.ForeignKeyConstraint', '*scpb.ColumnNotNull']
- $dependent[Type] IN ['*scpb.ConstraintWithoutIndexName', '*scpb.ConstraintComment']
- $dependent[Type] = '*scpb.ConstraintComment'
- joinOnConstraintID($constraint, $dependent, $table-id, $constraint-id)
- $constraint-Target[TargetStatus] = ABSENT
- $constraint-Node[CurrentStatus] = VALIDATED
Expand Down Expand Up @@ -2047,7 +2155,7 @@ deprules
kind: Precedence
to: constraint-Node
query:
- $dependents[Type] IN ['*scpb.ConstraintWithoutIndexName', '*scpb.ConstraintComment']
- $dependents[Type] = '*scpb.ConstraintComment'
- $constraint[Type] IN ['*scpb.UniqueWithoutIndexConstraint', '*scpb.CheckConstraint', '*scpb.ForeignKeyConstraint', '*scpb.ColumnNotNull']
- joinOnConstraintID($dependents, $constraint, $table-id, $constraint-id)
- toAbsent($dependents-Target, $constraint-Target)
Expand All @@ -2060,7 +2168,7 @@ deprules
kind: Precedence
to: constraint-Node
query:
- $dependents[Type] IN ['*scpb.ConstraintWithoutIndexName', '*scpb.ConstraintComment']
- $dependents[Type] = '*scpb.ConstraintComment'
- $constraint[Type] IN ['*scpb.UniqueWithoutIndexConstraint', '*scpb.CheckConstraint', '*scpb.ForeignKeyConstraint', '*scpb.ColumnNotNull']
- joinOnConstraintID($dependents, $constraint, $table-id, $constraint-id)
- transient($dependents-Target, $constraint-Target)
Expand All @@ -2073,7 +2181,7 @@ deprules
kind: Precedence
to: constraint-Node
query:
- $dependents[Type] IN ['*scpb.ConstraintWithoutIndexName', '*scpb.ConstraintComment']
- $dependents[Type] = '*scpb.ConstraintComment'
- $constraint[Type] IN ['*scpb.UniqueWithoutIndexConstraint', '*scpb.CheckConstraint', '*scpb.ForeignKeyConstraint', '*scpb.ColumnNotNull']
- joinOnConstraintID($dependents, $constraint, $table-id, $constraint-id)
- $dependents-Target[TargetStatus] = TRANSIENT_ABSENT
Expand All @@ -2087,7 +2195,7 @@ deprules
kind: Precedence
to: constraint-Node
query:
- $dependents[Type] IN ['*scpb.ConstraintWithoutIndexName', '*scpb.ConstraintComment']
- $dependents[Type] = '*scpb.ConstraintComment'
- $constraint[Type] IN ['*scpb.UniqueWithoutIndexConstraint', '*scpb.CheckConstraint', '*scpb.ForeignKeyConstraint', '*scpb.ColumnNotNull']
- joinOnConstraintID($dependents, $constraint, $table-id, $constraint-id)
- $dependents-Target[TargetStatus] = ABSENT
Expand Down Expand Up @@ -2155,7 +2263,7 @@ deprules
kind: SameStagePrecedence
to: constraint-Node
query:
- $dependents[Type] IN ['*scpb.ConstraintWithoutIndexName', '*scpb.ConstraintComment']
- $dependents[Type] = '*scpb.ConstraintComment'
- $constraint[Type] IN ['*scpb.UniqueWithoutIndexConstraintUnvalidated', '*scpb.CheckConstraintUnvalidated', '*scpb.ForeignKeyConstraintUnvalidated']
- joinOnConstraintID($dependents, $constraint, $table-id, $constraint-id)
- toAbsent($dependents-Target, $constraint-Target)
Expand All @@ -2168,7 +2276,7 @@ deprules
kind: SameStagePrecedence
to: constraint-Node
query:
- $dependents[Type] IN ['*scpb.ConstraintWithoutIndexName', '*scpb.ConstraintComment']
- $dependents[Type] = '*scpb.ConstraintComment'
- $constraint[Type] IN ['*scpb.UniqueWithoutIndexConstraintUnvalidated', '*scpb.CheckConstraintUnvalidated', '*scpb.ForeignKeyConstraintUnvalidated']
- joinOnConstraintID($dependents, $constraint, $table-id, $constraint-id)
- transient($dependents-Target, $constraint-Target)
Expand All @@ -2181,7 +2289,7 @@ deprules
kind: SameStagePrecedence
to: constraint-Node
query:
- $dependents[Type] IN ['*scpb.ConstraintWithoutIndexName', '*scpb.ConstraintComment']
- $dependents[Type] = '*scpb.ConstraintComment'
- $constraint[Type] IN ['*scpb.UniqueWithoutIndexConstraintUnvalidated', '*scpb.CheckConstraintUnvalidated', '*scpb.ForeignKeyConstraintUnvalidated']
- joinOnConstraintID($dependents, $constraint, $table-id, $constraint-id)
- $dependents-Target[TargetStatus] = TRANSIENT_ABSENT
Expand All @@ -2195,7 +2303,7 @@ deprules
kind: SameStagePrecedence
to: constraint-Node
query:
- $dependents[Type] IN ['*scpb.ConstraintWithoutIndexName', '*scpb.ConstraintComment']
- $dependents[Type] = '*scpb.ConstraintComment'
- $constraint[Type] IN ['*scpb.UniqueWithoutIndexConstraintUnvalidated', '*scpb.CheckConstraintUnvalidated', '*scpb.ForeignKeyConstraintUnvalidated']
- joinOnConstraintID($dependents, $constraint, $table-id, $constraint-id)
- $dependents-Target[TargetStatus] = ABSENT
Expand Down Expand Up @@ -3279,6 +3387,19 @@ deprules
- $dependent-Node[CurrentStatus] = PUBLIC
- joinTargetNode($simple-constraint, $simple-constraint-Target, $simple-constraint-Node)
- joinTargetNode($dependent, $dependent-Target, $dependent-Node)
- name: simple constraint visible before name
from: simple-constraint-Node
kind: SameStagePrecedence
to: constraint-name-Node
query:
- $simple-constraint[Type] IN ['*scpb.UniqueWithoutIndexConstraint', '*scpb.UniqueWithoutIndexConstraintUnvalidated', '*scpb.CheckConstraint', '*scpb.CheckConstraintUnvalidated', '*scpb.ForeignKeyConstraint', '*scpb.ForeignKeyConstraintUnvalidated', '*scpb.ColumnNotNull']
- $constraint-name[Type] = '*scpb.ConstraintWithoutIndexName'
- joinOnConstraintID($simple-constraint, $constraint-name, $table-id, $constraint-id)
- ToPublicOrTransient($simple-constraint-Target, $constraint-name-Target)
- $simple-constraint-Node[CurrentStatus] = WRITE_ONLY
- $constraint-name-Node[CurrentStatus] = PUBLIC
- joinTargetNode($simple-constraint, $simple-constraint-Target, $simple-constraint-Node)
- joinTargetNode($constraint-name, $constraint-name-Target, $constraint-name-Node)
- name: swapped primary index public before column
from: index-Node
kind: Precedence
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ func init() {
"dependent", "complex-constraint",
func(from, to NodeVars) rel.Clauses {
return rel.Clauses{
from.TypeFilter(rulesVersionKey, isConstraintDependent),
from.TypeFilter(rulesVersionKey, isConstraintDependent, Not(isConstraintWithIndexName)),
to.TypeFilter(rulesVersionKey, isNonIndexBackedConstraint, isSubjectTo2VersionInvariant),
JoinOnConstraintID(from, to, "table-id", "constraint-id"),
StatusesToPublicOrTransient(from, scpb.Status_PUBLIC, to, scpb.Status_PUBLIC),
Expand All @@ -47,4 +47,20 @@ func init() {
}
},
)

// Constraint name should be assigned right before it becomes visible, otherwise
// we won't have the correct message inside errors.
registerDepRule(
"simple constraint visible before name",
scgraph.SameStagePrecedence,
"simple-constraint", "constraint-name",
func(from, to NodeVars) rel.Clauses {
return rel.Clauses{
from.TypeFilter(rulesVersionKey, isNonIndexBackedConstraint),
to.Type((*scpb.ConstraintWithoutIndexName)(nil)),
JoinOnConstraintID(from, to, "table-id", "constraint-id"),
StatusesToPublicOrTransient(from, scpb.Status_WRITE_ONLY, to, scpb.Status_PUBLIC),
}
},
)
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ func init() {
func(from, to NodeVars) rel.Clauses {
return rel.Clauses{
from.TypeFilter(rulesVersionKey, isNonIndexBackedConstraint, isSubjectTo2VersionInvariant),
to.TypeFilter(rulesVersionKey, isConstraintDependent),
to.TypeFilter(rulesVersionKey, isConstraintDependent, Not(isConstraintWithIndexName)),
JoinOnConstraintID(from, to, "table-id", "constraint-id"),
}
},
Expand All @@ -42,7 +42,7 @@ func init() {
scpb.Status_ABSENT, scpb.Status_ABSENT,
func(from, to NodeVars) rel.Clauses {
return rel.Clauses{
from.TypeFilter(rulesVersionKey, isConstraintDependent),
from.TypeFilter(rulesVersionKey, isConstraintDependent, Not(isConstraintWithIndexName)),
to.TypeFilter(rulesVersionKey, isNonIndexBackedConstraint, isSubjectTo2VersionInvariant),
JoinOnConstraintID(from, to, "table-id", "constraint-id"),
}
Expand All @@ -61,10 +61,41 @@ func init() {
scpb.Status_ABSENT, scpb.Status_ABSENT,
func(from, to NodeVars) rel.Clauses {
return rel.Clauses{
from.TypeFilter(rulesVersionKey, isConstraintDependent),
from.TypeFilter(rulesVersionKey, isConstraintDependent, Not(isConstraintWithIndexName)),
to.TypeFilter(rulesVersionKey, isNonIndexBackedConstraint, Not(isSubjectTo2VersionInvariant)),
JoinOnConstraintID(from, to, "table-id", "constraint-id"),
}
},
)

// Constraint name should be cleared right before the constraint is no
// longer visible.
registerDepRuleForDrop(
"Constraint should be hidden before name",
scgraph.SameStagePrecedence,
"constraint-name", "constraint",
scpb.Status_ABSENT, scpb.Status_ABSENT,
func(from, to NodeVars) rel.Clauses {
return rel.Clauses{
from.Type((*scpb.ConstraintWithoutIndexName)(nil)),
to.TypeFilter(rulesVersionKey, isNonIndexBackedConstraint),
JoinOnConstraintID(from, to, "table-id", "constraint-id"),
}
},
)
// Constraint should be validated before the constraint name is attempted
// to be cleaned.
registerDepRuleForDrop(
"Constraint should be hidden before name",
scgraph.Precedence,
"constraint", "constraint-name",
scpb.Status_VALIDATED, scpb.Status_ABSENT,
func(from, to NodeVars) rel.Clauses {
return rel.Clauses{
from.TypeFilter(rulesVersionKey, isNonIndexBackedConstraint),
to.Type((*scpb.ConstraintWithoutIndexName)(nil)),
JoinOnConstraintID(from, to, "table-id", "constraint-id"),
}
},
)
}
Original file line number Diff line number Diff line change
Expand Up @@ -264,6 +264,14 @@ func isConstraintDependent(e scpb.Element) bool {
return false
}

func isConstraintWithIndexName(e scpb.Element) bool {
switch e.(type) {
case *scpb.ConstraintWithoutIndexName:
return true
}
return false
}

func isData(e scpb.Element) bool {
switch e.(type) {
case *scpb.DatabaseData:
Expand Down
Loading

0 comments on commit 0f36130

Please sign in to comment.