-
Notifications
You must be signed in to change notification settings - Fork 267
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
sql/migratespec: support modify_table and add_column (#246)
- Loading branch information
Showing
2 changed files
with
70 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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]) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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{}) | ||
} |