Skip to content

Commit b425685

Browse files
committed
Adding support for ALTER TABLE ... DROP CONSTRAINT IF EXISTS
1 parent 7c05ba9 commit b425685

File tree

4 files changed

+38
-3
lines changed

4 files changed

+38
-3
lines changed

sql/analyzer/constraints.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,12 @@ func resolveDropConstraint(ctx *sql.Context, a *Analyzer, n sql.Node, scope *pla
7474
}
7575
}
7676

77+
if dropConstraint.IfExists {
78+
newAlterDropCheck := plan.NewAlterDropCheck(rt, dropConstraint.Name)
79+
newAlterDropCheck.IfExists = true
80+
return newAlterDropCheck, transform.NewTree, nil
81+
}
82+
7783
return nil, transform.SameTree, sql.ErrUnknownConstraint.New(dropConstraint.Name)
7884
})
7985
}
@@ -82,6 +88,11 @@ func resolveDropConstraint(ctx *sql.Context, a *Analyzer, n sql.Node, scope *pla
8288
func validateDropConstraint(ctx *sql.Context, a *Analyzer, n sql.Node, scope *plan.Scope, sel RuleSelector, qFlags *sql.QueryFlags) (sql.Node, transform.TreeIdentity, error) {
8389
switch n := n.(type) {
8490
case *plan.DropCheck:
91+
// Don't bother validating that the constraint exists if the IfExists flag is set
92+
if n.IfExists {
93+
return n, transform.SameTree, nil
94+
}
95+
8596
rt := n.Table
8697

8798
ct, ok := rt.Table.(sql.CheckTable)

sql/plan/alter_check.go

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,9 @@ var _ sql.CollationCoercible = (*CreateCheck)(nil)
4141

4242
type DropCheck struct {
4343
ddlNode
44-
Table *ResolvedTable
45-
Name string
44+
Table *ResolvedTable
45+
Name string
46+
IfExists bool
4647
}
4748

4849
var _ sql.Node = (*DropCheck)(nil)
@@ -172,7 +173,8 @@ func NewCheckDefinition(ctx *sql.Context, check *sql.CheckConstraint) (*sql.Chec
172173
// not known, and is determined during analysis.
173174
type DropConstraint struct {
174175
UnaryNode
175-
Name string
176+
Name string
177+
IfExists bool
176178
}
177179

178180
var _ sql.Node = (*DropConstraint)(nil)

sql/planbuilder/ddl.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -737,6 +737,7 @@ func (b *Builder) buildAlterConstraint(inScope *scope, ddl *ast.DDL, table *plan
737737
outScope.node = &plan.DropConstraint{
738738
UnaryNode: plan.UnaryNode{Child: table},
739739
Name: c.name,
740+
IfExists: ddl.ConstraintIfExists,
740741
}
741742
default:
742743
err := sql.ErrUnsupportedFeature.New(ast.String(ddl))

sql/rowexec/ddl_iters.go

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1859,6 +1859,27 @@ func (b *BaseBuilder) executeDropCheck(ctx *sql.Context, n *plan.DropCheck) erro
18591859
return err
18601860
}
18611861

1862+
checkTable, ok := chAlterable.(sql.CheckTable)
1863+
if !ok {
1864+
return plan.ErrNoCheckConstraintSupport.New(chAlterable.Name())
1865+
}
1866+
1867+
checks, err := checkTable.GetChecks(ctx)
1868+
if err != nil {
1869+
return err
1870+
}
1871+
1872+
exists := false
1873+
for _, check := range checks {
1874+
if strings.EqualFold(check.Name, n.Name) {
1875+
exists = true
1876+
}
1877+
}
1878+
1879+
if !exists && n.IfExists {
1880+
return nil
1881+
}
1882+
18621883
return chAlterable.DropCheck(ctx, n.Name)
18631884
}
18641885

0 commit comments

Comments
 (0)