From d2ef939da56a32a8764b86854d10c345570bf096 Mon Sep 17 00:00:00 2001 From: Rotem Tamir Date: Thu, 25 Nov 2021 11:18:07 +0200 Subject: [PATCH] sql/migratespec: support modify_table and add_column (#246) --- .../integration/hclsqlspec/migrate_test.go | 39 +++++++++++++++++++ sql/sqlspec/migratespec.go | 31 +++++++++++++++ 2 files changed, 70 insertions(+) create mode 100644 internal/integration/hclsqlspec/migrate_test.go create mode 100644 sql/sqlspec/migratespec.go diff --git a/internal/integration/hclsqlspec/migrate_test.go b/internal/integration/hclsqlspec/migrate_test.go new file mode 100644 index 00000000000..b02a11d3c28 --- /dev/null +++ b/internal/integration/hclsqlspec/migrate_test.go @@ -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]) +} diff --git a/sql/sqlspec/migratespec.go b/sql/sqlspec/migratespec.go new file mode 100644 index 00000000000..fc72b5b9e6d --- /dev/null +++ b/sql/sqlspec/migratespec.go @@ -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{}) +}