Skip to content

Commit

Permalink
Merge pull request cockroachdb#62880 from postamar/backport20.2-62732
Browse files Browse the repository at this point in the history
release-20.2: sql: fix error when adding and dropping a constraint in same txn.
  • Loading branch information
Marius Posta authored Apr 6, 2021
2 parents 390b5df + 9e732f7 commit 65530d7
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 2 deletions.
19 changes: 17 additions & 2 deletions pkg/sql/catalog/tabledesc/structured.go
Original file line number Diff line number Diff line change
Expand Up @@ -2816,7 +2816,6 @@ func (desc *Mutable) DropConstraint(
return nil
}
}
return errors.Errorf("constraint %q not found on table %q", name, desc.Name)

case descpb.ConstraintTypeFK:
if detail.FK.Validity == descpb.ConstraintValidity_Validating {
Expand Down Expand Up @@ -2849,13 +2848,29 @@ func (desc *Mutable) DropConstraint(
return nil
}
}
return errors.AssertionFailedf("constraint %q not found on table %q", name, desc.Name)

default:
return unimplemented.Newf(fmt.Sprintf("drop-constraint-%s", detail.Kind),
"constraint %q has unsupported type", tree.ErrNameString(name))
}

// Check if the constraint can be found in a mutation, complain appropriately.
for i := range desc.Mutations {
m := &desc.Mutations[i]
if m.GetConstraint() != nil && m.GetConstraint().Name == name {
switch m.Direction {
case descpb.DescriptorMutation_ADD:
return unimplemented.NewWithIssueDetailf(42844,
"drop-constraint-mutation",
"constraint %q in the middle of being added, try again later", name)
case descpb.DescriptorMutation_DROP:
return unimplemented.NewWithIssueDetailf(42844,
"drop-constraint-mutation",
"constraint %q in the middle of being dropped", name)
}
}
}
return errors.AssertionFailedf("constraint %q not found on table %q", name, desc.Name)
}

// RenameConstraint renames a constraint.
Expand Down
23 changes: 23 additions & 0 deletions pkg/sql/logictest/testdata/logic_test/alter_table
Original file line number Diff line number Diff line change
Expand Up @@ -1407,3 +1407,26 @@ CREATE TABLE t54629 (c INT NOT NULL, UNIQUE INDEX (c));
ALTER TABLE t54629 ADD CONSTRAINT pk PRIMARY KEY (c);
INSERT INTO t54629 VALUES (1);
DELETE FROM t54629 WHERE c = 1;

# Regression test for #60786. Handle in-transaction constraint ADD+DROP correctly.
subtest regression_60786

statement ok
CREATE TABLE t60786(i INT PRIMARY KEY);

statement error pgcode 0A000 constraint "fk" in the middle of being added, try again later
BEGIN;
CREATE TABLE child_60786(i INT PRIMARY KEY);
ALTER TABLE t60786 ADD CONSTRAINT fk FOREIGN KEY (i) REFERENCES child_60786(i) NOT VALID;
ALTER TABLE t60786 DROP CONSTRAINT fk CASCADE

statement ok
ROLLBACK

statement error pgcode 0A000 constraint "ck" in the middle of being added, try again later
BEGIN;
ALTER TABLE t60786 ADD CONSTRAINT ck CHECK(i > 0) NOT VALID;
ALTER TABLE t60786 DROP CONSTRAINT ck CASCADE

statement ok
ROLLBACK

0 comments on commit 65530d7

Please sign in to comment.