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/schema/schemaspec: support Resource Merge #100

Merged
merged 1 commit into from
Jul 25, 2021
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
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
}