Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

sql/sqlite: table options diff #1773

Merged
merged 1 commit into from
Jun 22, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 11 additions & 9 deletions sql/sqlite/diff.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,15 +31,17 @@ 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, error) {
var changes []schema.Change
switch {
case sqlx.Has(from.Attrs, &WithoutRowID{}) && !sqlx.Has(to.Attrs, &WithoutRowID{}):
changes = append(changes, &schema.DropAttr{
A: &WithoutRowID{},
})
case !sqlx.Has(from.Attrs, &WithoutRowID{}) && sqlx.Has(to.Attrs, &WithoutRowID{}):
changes = append(changes, &schema.AddAttr{
A: &WithoutRowID{},
})
for _, a := range []schema.Attr{&WithoutRowID{}, &Strict{}} {
switch {
case sqlx.Has(from.Attrs, a) && !sqlx.Has(to.Attrs, a):
changes = append(changes, &schema.DropAttr{
A: a,
})
case !sqlx.Has(from.Attrs, a) && sqlx.Has(to.Attrs, a):
changes = append(changes, &schema.AddAttr{
A: a,
})
}
}
return append(changes, sqlx.CheckDiff(from, to)...), nil
}
Expand Down
14 changes: 10 additions & 4 deletions sql/sqlite/diff_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,23 +27,29 @@ func TestDiff_TableDiff(t *testing.T) {
to: &schema.Table{Name: "users"},
},
{
name: "add attr",
name: "add attrs",
from: &schema.Table{Name: "t1", Schema: &schema.Schema{Name: "public"}},
to: &schema.Table{Name: "t1", Attrs: []schema.Attr{&WithoutRowID{}}},
to: &schema.Table{Name: "t1", Attrs: []schema.Attr{&WithoutRowID{}, &Strict{}}},
wantChanges: []schema.Change{
&schema.AddAttr{
A: &WithoutRowID{},
},
&schema.AddAttr{
A: &Strict{},
},
},
},
{
name: "drop attr",
from: &schema.Table{Name: "t1", Attrs: []schema.Attr{&WithoutRowID{}}},
name: "drop attrs",
from: &schema.Table{Name: "t1", Attrs: []schema.Attr{&WithoutRowID{}, &Strict{}}},
to: &schema.Table{Name: "t1"},
wantChanges: []schema.Change{
&schema.DropAttr{
A: &WithoutRowID{},
},
&schema.DropAttr{
A: &Strict{},
},
},
},
{
Expand Down