Skip to content

Commit

Permalink
sql: adds unique constraint checks to SCRUB
Browse files Browse the repository at this point in the history
The SCRUB tool validates table constraints. Before this change, it could
only validate `CONSTRAINT` and foreign key constraints. This PR enhances
SCRUB to also validate tables that contain `PRIMARY KEY`, `UNIQUE`, or
the experimental `UNIQUE WITHOUT INDEX` keywords.

Fixes: cockroachdb#73632

Release note (sql): This PR expands the capabilities of the experimental
SCRUB tool to include checking unique constraints for primary keys,
unique indexes, and unique columns without indexes. The usage and output
of SCRUB is unchanged, but if there is a unique constraint violation,
users will see the error message `unique_constraint_violation` for all
rows that violate the constraint, along with information about the row.

Release justification:
  • Loading branch information
rharding6373 committed Mar 22, 2022
1 parent 9bab39f commit 060077d
Show file tree
Hide file tree
Showing 5 changed files with 551 additions and 45 deletions.
4 changes: 4 additions & 0 deletions pkg/sql/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,7 @@ go_library(
"scrub_constraint.go",
"scrub_fk.go",
"scrub_index.go",
"scrub_unique_constraint.go",
"select_name_resolution.go",
"sequence.go",
"sequence_select.go",
Expand Down Expand Up @@ -370,6 +371,7 @@ go_library(
"//pkg/sql/schemachanger/scrun",
"//pkg/sql/scrub",
"//pkg/sql/sem/builtins",
"//pkg/sql/sem/catid",
"//pkg/sql/sem/transform",
"//pkg/sql/sem/tree",
"//pkg/sql/sem/tree/treebin",
Expand Down Expand Up @@ -573,6 +575,8 @@ go_test(
"//pkg/base",
"//pkg/build/bazel",
"//pkg/ccl/kvccl/kvtenantccl",
"//pkg/ccl/partitionccl",
"//pkg/ccl/utilccl",
"//pkg/cloud/impl:cloudimpl",
"//pkg/clusterversion",
"//pkg/config",
Expand Down
12 changes: 10 additions & 2 deletions pkg/sql/scrub.go
Original file line number Diff line number Diff line change
Expand Up @@ -394,8 +394,7 @@ func createIndexCheckOperations(
// createConstraintCheckOperations will return all of the constraints
// that are being checked. If constraintNames is nil, then all
// constraints are returned.
// TODO(joey): Only SQL CHECK and FOREIGN KEY constraints are
// implemented.
// Only SQL CHECK, FOREIGN KEY, and UNIQUE constraints are supported.
func createConstraintCheckOperations(
ctx context.Context,
p *planner,
Expand Down Expand Up @@ -443,6 +442,15 @@ func createConstraintCheckOperations(
constraint,
asOf,
))
case descpb.ConstraintTypePK:
fallthrough
case descpb.ConstraintTypeUnique:
results = append(results, newSQLUniqueConstraintCheckOperation(
tableName,
tableDesc,
constraint,
asOf,
))
}
}
return results, nil
Expand Down
3 changes: 3 additions & 0 deletions pkg/sql/scrub/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,9 @@ const (
// ForeignKeyConstraintViolation occurs when a row in a
// table is violating a foreign key constraint.
ForeignKeyConstraintViolation = "foreign_key_violation"
// UniqueConstraintViolation occurs when a row in a table is violating
// a unique constraint.
UniqueConstraintViolation = "unique_constraint_violation"
)

// Error contains the details on the scrub error that was caught.
Expand Down
Loading

0 comments on commit 060077d

Please sign in to comment.