Skip to content

Commit

Permalink
sql/mysql: reject CHECK constraints when it is not supported by the d…
Browse files Browse the repository at this point in the history
…atabase
  • Loading branch information
a8m committed Jan 16, 2022
1 parent 8fb363a commit b760593
Show file tree
Hide file tree
Showing 6 changed files with 34 additions and 9 deletions.
5 changes: 4 additions & 1 deletion .golangci.yml
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
run:
timeout: 3m

issues:
include:
- EXC0012
- EXC0012
8 changes: 6 additions & 2 deletions sql/internal/sqlx/diff.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ type (

// TableAttrDiff returns a changeset for migrating table attributes from
// one state to the other. For example, dropping or adding a `CHECK` constraint.
TableAttrDiff(from, to *schema.Table) []schema.Change
TableAttrDiff(from, to *schema.Table) ([]schema.Change, error)

// ColumnChange returns the schema changes (if any) for migrating one column to the other.
ColumnChange(from, to *schema.Column) (schema.ChangeKind, error)
Expand Down Expand Up @@ -154,7 +154,11 @@ func (d *Diff) TableDiff(from, to *schema.Table) ([]schema.Change, error) {
}

// Drop or modify attributes (collations, checks, etc).
changes = append(changes, d.TableAttrDiff(from, to)...)
change, err := d.TableAttrDiff(from, to)
if err != nil {
return nil, err
}
changes = append(changes, change...)

// Drop or modify columns.
for _, c1 := range from.Columns {
Expand Down
7 changes: 5 additions & 2 deletions sql/mysql/diff.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ func (d *diff) SchemaAttrDiff(from, to *schema.Schema) []schema.Change {
}

// TableAttrDiff returns a changeset for migrating table attributes from one state to the other.
func (d *diff) TableAttrDiff(from, to *schema.Table) []schema.Change {
func (d *diff) TableAttrDiff(from, to *schema.Table) ([]schema.Change, error) {
var changes []schema.Change
if change := d.autoIncChange(from.Attrs, to.Attrs); change != noChange {
changes = append(changes, change)
Expand All @@ -47,9 +47,12 @@ func (d *diff) TableAttrDiff(from, to *schema.Table) []schema.Change {
if change := d.collationChange(from.Attrs, from.Schema.Attrs, to.Attrs); change != noChange {
changes = append(changes, change)
}
if _, ok := d.supportsCheck(); !ok && sqlx.Has(to.Attrs, &schema.Check{}) {
return nil, fmt.Errorf("version %q does not support CHECK constraints", d.version)
}
return append(changes, sqlx.CheckDiff(from, to, func(c1, c2 *schema.Check) bool {
return c1.Expr != c2.Expr || sqlx.Has(c1.Attrs, &Enforced{}) != sqlx.Has(c2.Attrs, &Enforced{})
})...)
})...), nil
}

// ColumnChange returns the schema changes (if any) for migrating one column to the other.
Expand Down
15 changes: 15 additions & 0 deletions sql/mysql/diff_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -339,6 +339,21 @@ func TestDiff_TableDiff(t *testing.T) {
}
}

func TestDiff_UnsupportedChecks(t *testing.T) {
db, m, err := sqlmock.New()
require.NoError(t, err)
mock{m}.version("5.6.35")
drv, err := Open(db)
require.NoError(t, err)
s := schema.New("public")
changes, err := drv.TableDiff(
schema.NewTable("t").SetSchema(s),
schema.NewTable("t").SetSchema(s).AddChecks(schema.NewCheck()),
)
require.Nil(t, changes)
require.EqualError(t, err, `version "5.6.35" does not support CHECK constraints`)
}

func TestDiff_SchemaDiff(t *testing.T) {
db, m, err := sqlmock.New()
require.NoError(t, err)
Expand Down
4 changes: 2 additions & 2 deletions sql/postgres/diff.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,14 @@ func (d *diff) SchemaAttrDiff(_, _ *schema.Schema) []schema.Change {
}

// TableAttrDiff returns a changeset for migrating table attributes from one state to the other.
func (d *diff) TableAttrDiff(from, to *schema.Table) []schema.Change {
func (d *diff) TableAttrDiff(from, to *schema.Table) ([]schema.Change, error) {
var changes []schema.Change
if change := sqlx.CommentDiff(from.Attrs, to.Attrs); change != nil {
changes = append(changes, change)
}
return append(changes, sqlx.CheckDiff(from, to, func(c1, c2 *schema.Check) bool {
return c1.Expr != c2.Expr || sqlx.Has(c1.Attrs, &NoInherit{}) != sqlx.Has(c2.Attrs, &NoInherit{})
})...)
})...), nil
}

// ColumnChange returns the schema changes (if any) for migrating one column to the other.
Expand Down
4 changes: 2 additions & 2 deletions sql/sqlite/diff.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ func (d *diff) SchemaAttrDiff(_, _ *schema.Schema) []schema.Change {
}

// TableAttrDiff returns a changeset for migrating table attributes from one state to the other.
func (d *diff) TableAttrDiff(from, to *schema.Table) []schema.Change {
func (d *diff) TableAttrDiff(from, to *schema.Table) ([]schema.Change, error) {
var changes []schema.Change
switch {
case sqlx.Has(from.Attrs, &WithoutRowID{}) && !sqlx.Has(to.Attrs, &WithoutRowID{}):
Expand All @@ -36,7 +36,7 @@ func (d *diff) TableAttrDiff(from, to *schema.Table) []schema.Change {
}
return append(changes, sqlx.CheckDiff(from, to, func(c1, c2 *schema.Check) bool {
return c1.Expr != c2.Expr
})...)
})...), nil
}

// ColumnChange returns the schema changes (if any) for migrating one column to the other.
Expand Down

0 comments on commit b760593

Please sign in to comment.