Skip to content

Commit

Permalink
sql/migratespec: support modify_table and add_column (#246)
Browse files Browse the repository at this point in the history
  • Loading branch information
rotemtam authored Nov 25, 2021
1 parent 47a0632 commit d2ef939
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 0 deletions.
39 changes: 39 additions & 0 deletions internal/integration/hclsqlspec/migrate_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package hclsqlspec

import (
"testing"

"ariga.io/atlas/schema/schemaspec/schemahcl"
"ariga.io/atlas/sql/sqlspec"
"github.com/stretchr/testify/require"
)

func TestMigrate(t *testing.T) {
f := `
modify_table {
table = "users"
add_column {
column "id" {
type = "int"
}
}
}
`
var test struct {
Changes []sqlspec.Change `spec:""`
}
err := schemahcl.Unmarshal([]byte(f), &test)
require.NoError(t, err)
require.EqualValues(t, &sqlspec.ModifyTable{
Table: "users",
Changes: []sqlspec.Change{
&sqlspec.AddColumn{
Column: &sqlspec.Column{
Name: "id",
Null: false,
TypeName: "int",
},
},
},
}, test.Changes[0])
}
31 changes: 31 additions & 0 deletions sql/sqlspec/migratespec.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package sqlspec

import (
"ariga.io/atlas/schema/schemaspec"
)

type (
// Change is the interface implemented by change specifications. Change instances are supposed
// to be mappable to schema.Change instances.
Change interface {
change()
}

// ModifyTable is a specification for a schema.ModifyTable.
ModifyTable struct {
Change
Table string `spec:"table"`
Changes []Change `spec:""`
}

// AddColumn is a specification for a schema.AddColumn.
AddColumn struct {
Change
Column *Column `spec:"column"`
}
)

func init() {
schemaspec.Register("modify_table", &ModifyTable{})
schemaspec.Register("add_column", &AddColumn{})
}

0 comments on commit d2ef939

Please sign in to comment.