Skip to content

Commit

Permalink
sql/internal/sqlx: diff realm adds new schema with all its resources (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
a8m authored Dec 5, 2023
1 parent a38c429 commit 87ead07
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 0 deletions.
9 changes: 9 additions & 0 deletions sql/internal/sqlx/diff.go
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,15 @@ func (d *Diff) RealmDiff(from, to *schema.Realm, options ...schema.DiffOption) (
continue
}
changes = opts.AddOrSkip(changes, &schema.AddSchema{S: s1})
for _, o := range s1.Objects {
changes = opts.AddOrSkip(changes, &schema.AddObject{O: o})
}
for _, f := range s1.Funcs {
changes = opts.AddOrSkip(changes, &schema.AddFunc{F: f})
}
for _, p := range s1.Procs {
changes = opts.AddOrSkip(changes, &schema.AddProc{P: p})
}
for _, t := range s1.Tables {
changes = opts.AddOrSkip(changes, &schema.AddTable{T: t})
}
Expand Down
21 changes: 21 additions & 0 deletions sql/postgres/diff_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -484,6 +484,27 @@ func TestDiff_TableDiff(t *testing.T) {
}
}

func TestDiff_RealmDiff(t *testing.T) {
db, m, err := sqlmock.New()
require.NoError(t, err)
mock{m}.version("130000")
drv, err := Open(db)
to := schema.New("public").
AddTables(
schema.NewTable("users").AddColumns(schema.NewIntColumn("t2_id", "int")),
).
AddObjects(
&schema.EnumType{T: "e", Values: []string{"b"}},
)
changes, err := drv.RealmDiff(schema.NewRealm(), schema.NewRealm(to))
require.NoError(t, err)
require.EqualValues(t, []schema.Change{
&schema.AddSchema{S: to},
&schema.AddObject{O: to.Objects[0]},
&schema.AddTable{T: to.Tables[0]},
}, changes)
}

func TestDiff_SchemaDiff(t *testing.T) {
db, m, err := sqlmock.New()
require.NoError(t, err)
Expand Down

0 comments on commit 87ead07

Please sign in to comment.