Skip to content

Commit

Permalink
sql/schema/schemaspec: support Resource Merge (#100)
Browse files Browse the repository at this point in the history
  • Loading branch information
rotemtam authored Jul 25, 2021
1 parent 1d61893 commit 08aa59f
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 13 deletions.
11 changes: 11 additions & 0 deletions sql/schema/schemaspec/spec.go
Original file line number Diff line number Diff line change
Expand Up @@ -287,10 +287,21 @@ func (a *Attr) Strings() ([]string, error) {
return out, nil
}

// Merge merges the attributes of another Resource into the Resource.
func (r *Resource) Merge(other *Resource) {
for _, attr := range other.Attrs {
r.SetAttr(attr)
}
}

func (r *Resource) Attr(name string) (*Attr, bool) {
return getAttrVal(r.Attrs, name)
}

func (r *Resource) SetAttr(attr *Attr) {
r.Attrs = replaceOrAppendAttr(r.Attrs, attr)
}

func (*LiteralValue) val() {}
func (*ListValue) val() {}

Expand Down
66 changes: 53 additions & 13 deletions sql/schema/schemaspec/spec_test.go
Original file line number Diff line number Diff line change
@@ -1,22 +1,24 @@
package schemaspec
package schemaspec_test

import (
"testing"

"ariga.io/atlas/sql/internal/schemautil"
"ariga.io/atlas/sql/schema/schemaspec"
"github.com/stretchr/testify/require"
)

func TestAccessors(t *testing.T) {
s := &Schema{
s := &schemaspec.Schema{
Name: "hello",
Tables: []*Table{
Tables: []*schemaspec.Table{
{
Name: "t1",
Columns: []*Column{
Columns: []*schemaspec.Column{
{Name: "c1", Type: "string"},
{Name: "c2", Type: "string"},
},
Indexes: []*Index{
Indexes: []*schemaspec.Index{
{Name: "idx"},
},
},
Expand Down Expand Up @@ -52,26 +54,26 @@ func TestAccessors(t *testing.T) {
}

func TestColumn_OverridesFor(t *testing.T) {
col := &Column{}
col.Overrides = []*Override{
col := &schemaspec.Column{}
col.Overrides = []*schemaspec.Override{
{
Dialect: "mysql",
Resource: Resource{
Attrs: []*Attr{
Resource: schemaspec.Resource{
Attrs: []*schemaspec.Attr{
{
K: "type",
V: &LiteralValue{V: "varchar(100)"},
V: &schemaspec.LiteralValue{V: "varchar(100)"},
},
},
},
},
{
Dialect: "postgres",
Resource: Resource{
Attrs: []*Attr{
Resource: schemaspec.Resource{
Attrs: []*schemaspec.Attr{
{
K: "type",
V: &LiteralValue{V: "varchar(200)"},
V: &schemaspec.LiteralValue{V: "varchar(200)"},
},
},
},
Expand All @@ -82,3 +84,41 @@ func TestColumn_OverridesFor(t *testing.T) {
pgo := col.Override("postgres")
require.EqualValues(t, pgo.Attrs[0], col.Overrides[1].Attrs[0])
}

func TestResource_Merge(t *testing.T) {
r := &schemaspec.Resource{
Attrs: []*schemaspec.Attr{
schemautil.StrLitAttr("existing", "old_val"),
schemautil.StrLitAttr("other", "val"),
schemautil.StrLitAttr("to_bool", "val"),
},
}
other := &schemaspec.Resource{
Attrs: []*schemaspec.Attr{
schemautil.StrLitAttr("new", "val"),
schemautil.StrLitAttr("existing", "val"),
schemautil.LitAttr("to_bool", "true"),
},
}
r.Merge(other)
require.EqualValues(t, "val", getStrAttr(t, r, "new"))
require.EqualValues(t, "val", getStrAttr(t, r, "existing"))
require.EqualValues(t, "val", getStrAttr(t, r, "other"))
require.EqualValues(t, true, getBoolAttr(t, r, "to_bool"))
}

func getStrAttr(t *testing.T, r *schemaspec.Resource, name string) string {
v, ok := r.Attr(name)
require.True(t, ok)
s, err := v.String()
require.NoError(t, err)
return s
}

func getBoolAttr(t *testing.T, r *schemaspec.Resource, name string) bool {
v, ok := r.Attr(name)
require.True(t, ok)
b, err := v.Bool()
require.NoError(t, err)
return b
}

0 comments on commit 08aa59f

Please sign in to comment.