Repository: ariga/atlas
Pull request #324 (2021-12-20 19:49:16)

all: remove schema.Execer
Replaced with migrate.ApplyChanges


Patch:
diff --git a/cmd/action/apply.go b/cmd/action/apply.go
index 35706fdf..e5606c83 100644
--- a/cmd/action/apply.go
+++ b/cmd/action/apply.go
@@ -84,7 +84,7 @@ func applyRun(d *Driver, u schemaUnmarshaler, dsn string, file string) {
 	_, result, err := prompt.Run()
 	cobra.CheckErr(err)
 	if result == answerApply {
-		err = d.Exec(ctx, changes)
+		err = d.ApplyChanges(ctx, changes)
 		cobra.CheckErr(err)
 	}
 }
diff --git a/cmd/action/diff.go b/cmd/action/diff.go
index 21e26c8f..bb05fe42 100644
--- a/cmd/action/diff.go
+++ b/cmd/action/diff.go
@@ -61,7 +61,7 @@ func changeDescriptor(ctx context.Context, c schema.Change, d *Driver) (*changeD
 	d.interceptor.on()
 	defer d.interceptor.clear()
 	defer d.interceptor.off()
-	if err := d.Exec(ctx, []schema.Change{c}); err != nil {
+	if err := d.ApplyChanges(ctx, []schema.Change{c}); err != nil {
 		return nil, fmt.Errorf("atlas: failed getting planned sql: %w", err)
 	}
 	desc.queries = d.interceptor.history
diff --git a/cmd/action/mux.go b/cmd/action/mux.go
index b93b1edb..d9d91cd2 100644
--- a/cmd/action/mux.go
+++ b/cmd/action/mux.go
@@ -7,7 +7,9 @@ import (
 	"strings"
 
 	"ariga.io/atlas/schema/schemaspec"
+	"ariga.io/atlas/sql/migrate"
 	"ariga.io/atlas/sql/schema"
+
 	"github.com/go-sql-driver/mysql"
 )
 
@@ -19,19 +21,12 @@ type (
 
 	// Driver implements the Atlas interface.
 	Driver struct {
-		driver
+		migrate.Driver
 		MarshalSpec   func(v interface{}, marshaler schemaspec.Marshaler) ([]byte, error)
 		UnmarshalSpec func(data []byte, unmarshaler schemaspec.Unmarshaler, v interface{}) error
 		interceptor   *interceptor
 	}
 
-	// A schema driver.
-	driver interface {
-		schema.Differ
-		schema.Execer
-		schema.Inspector
-	}
-
 	schemaUnmarshal struct {
 		unmarshalSpec func(data []byte, unmarshaler schemaspec.Unmarshaler, v interface{}) error
 		unmarshaler   schemaspec.Unmarshaler
diff --git a/cmd/action/provider.go b/cmd/action/provider.go
index 0917c2ad..180bcfc0 100644
--- a/cmd/action/provider.go
+++ b/cmd/action/provider.go
@@ -23,7 +23,7 @@ func mysqlProvider(dsn string) (*Driver, error) {
 		return nil, err
 	}
 	return &Driver{
-		driver:        drv,
+		Driver:        drv,
 		interceptor:   i,
 		MarshalSpec:   mysql.MarshalSpec,
 		UnmarshalSpec: mysql.UnmarshalSpec,
@@ -41,7 +41,7 @@ func postgresProvider(dsn string) (*Driver, error) {
 		return nil, err
 	}
 	return &Driver{
-		driver:        drv,
+		Driver:        drv,
 		interceptor:   i,
 		MarshalSpec:   postgres.MarshalSpec,
 		UnmarshalSpec: postgres.UnmarshalSpec,
diff --git a/sql/migrate/migrate.go b/sql/migrate/migrate.go
index 03d12da4..ca469874 100644
--- a/sql/migrate/migrate.go
+++ b/sql/migrate/migrate.go
@@ -75,7 +75,12 @@ type (
 	// PlanApplier wraps the methods for planning and applying changes
 	// on the database.
 	PlanApplier interface {
+		// PlanChanges returns a migration plan for applying the given changeset.
 		PlanChanges(context.Context, string, []schema.Change) (*Plan, error)
+
+		// ApplyChanges is responsible for applying the given changeset.
+		// An error may return from ApplyChanges if the driver is unable
+		// to execute a change.
 		ApplyChanges(context.Context, []schema.Change) error
 	}
 
diff --git a/sql/mysql/driver.go b/sql/mysql/driver.go
index fdd933c6..024647bc 100644
--- a/sql/mysql/driver.go
+++ b/sql/mysql/driver.go
@@ -9,6 +9,7 @@ import (
 	"strings"
 
 	"ariga.io/atlas/sql/internal/sqlx"
+	"ariga.io/atlas/sql/migrate"
 	"ariga.io/atlas/sql/schema"
 
 	"golang.org/x/mod/semver"
@@ -20,8 +21,8 @@ type (
 	Driver struct {
 		conn
 		schema.Differ
-		schema.Execer
 		schema.Inspector
+		migrate.PlanApplier
 	}
 
 	// database connection and its information.
@@ -41,10 +42,10 @@ func Open(db schema.ExecQuerier) (*Driver, error) {
 		return nil, fmt.Errorf("mysql: scanning system variables: %w", err)
 	}
 	return &Driver{
-		conn:      c,
-		Differ:    &sqlx.Diff{DiffDriver: &diff{c}},
-		Execer:    &planApply{c},
-		Inspector: &inspect{c},
+		conn:        c,
+		Differ:      &sqlx.Diff{DiffDriver: &diff{c}},
+		Inspector:   &inspect{c},
+		PlanApplier: &planApply{c},
 	}, nil
 }
 
diff --git a/sql/mysql/migrate.go b/sql/mysql/migrate.go
index 6c0bfa6d..a08117ae 100644
--- a/sql/mysql/migrate.go
+++ b/sql/mysql/migrate.go
@@ -59,11 +59,6 @@ func (p *planApply) ApplyChanges(ctx context.Context, changes []schema.Change) e
 	return nil
 }
 
-// Exec calls ApplyChanges. Exists here for backwards compatibility.
-func (p *planApply) Exec(ctx context.Context, changes []schema.Change) error {
-	return p.ApplyChanges(ctx, changes)
-}
-
 // state represents the state of a planning. It is not part of
 // planApply so that multiple planning/applying can be called
 // in parallel.
diff --git a/sql/postgres/driver.go b/sql/postgres/driver.go
index ee4f0309..1208ce5b 100644
--- a/sql/postgres/driver.go
+++ b/sql/postgres/driver.go
@@ -8,7 +8,7 @@ import (
 	"fmt"
 
 	"ariga.io/atlas/sql/internal/sqlx"
-
+	"ariga.io/atlas/sql/migrate"
 	"ariga.io/atlas/sql/schema"
 
 	"golang.org/x/mod/semver"
@@ -20,8 +20,8 @@ type (
 	Driver struct {
 		conn
 		schema.Differ
-		schema.Execer
 		schema.Inspector
+		migrate.PlanApplier
 	}
 
 	// database connection and its information.
@@ -62,10 +62,10 @@ func Open(db schema.ExecQuerier) (*Driver, error) {
 		return nil, fmt.Errorf("postgres: unsupported postgres version: %s", c.version)
 	}
 	return &Driver{
-		conn:      c,
-		Differ:    &sqlx.Diff{DiffDriver: &diff{c}},
-		Execer:    &planApply{c},
-		Inspector: &inspect{c},
+		conn:        c,
+		Differ:      &sqlx.Diff{DiffDriver: &diff{c}},
+		Inspector:   &inspect{c},
+		PlanApplier: &planApply{c},
 	}, nil
 }
 
diff --git a/sql/postgres/migrate.go b/sql/postgres/migrate.go
index b49d27f5..a52d0123 100644
--- a/sql/postgres/migrate.go
+++ b/sql/postgres/migrate.go
@@ -54,11 +54,6 @@ func (p *planApply) ApplyChanges(ctx context.Context, changes []schema.Change) e
 	return nil
 }
 
-// Exec calls ApplyChanges. Exists here for backwards compatibility.
-func (p *planApply) Exec(ctx context.Context, changes []schema.Change) error {
-	return p.ApplyChanges(ctx, changes)
-}
-
 // state represents the state of a planning. It is not part of
 // planApply so that multiple planning/applying can be called
 // in parallel.
diff --git a/sql/schema/migrate.go b/sql/schema/migrate.go
index ca53866c..d00c0bad 100644
--- a/sql/schema/migrate.go
+++ b/sql/schema/migrate.go
@@ -4,8 +4,6 @@
 
 package schema
 
-import "context"
-
 type (
 	// A Change represents a schema change. The types below implement this
 	// interface and can be used for describing schema changes.
@@ -207,35 +205,24 @@ func (k ChangeKind) Is(c ChangeKind) bool {
 	return k == c || k&c != 0
 }
 
-type (
-	// Differ is the interface implemented by the different
-	// drivers for comparing and diffing schema top elements.
-	Differ interface {
-		// RealmDiff returns a diff report for migrating a realm
-		// (or a database) from state "from" to state "to". An error
-		// is returned if such step is not possible.
-		RealmDiff(from, to *Realm) ([]Change, error)
-
-		// SchemaDiff returns a diff report for migrating a schema
-		// from state "from" to state "to". An error is returned
-		// if such step is not possible.
-		SchemaDiff(from, to *Schema) ([]Change, error)
-
-		// TableDiff returns a diff report for migrating a table
-		// from state "from" to state "to". An error is returned
-		// if such step is not possible.
-		TableDiff(from, to *Table) ([]Change, error)
-	}
-
-	// Execer is the interface implemented by the different
-	// drivers for executing schema changes.
-	Execer interface {
-		// Exec responsible for executing the given changeset.
-		// An error may return from Exec if the driver does not know
-		// how to exec a change.
-		Exec(ctx context.Context, changes []Change) error
-	}
-)
+// Differ is the interface implemented by the different
+// drivers for comparing and diffing schema top elements.
+type Differ interface {
+	// RealmDiff returns a diff report for migrating a realm
+	// (or a database) from state "from" to state "to". An error
+	// is returned if such step is not possible.
+	RealmDiff(from, to *Realm) ([]Change, error)
+
+	// SchemaDiff returns a diff report for migrating a schema
+	// from state "from" to state "to". An error is returned
+	// if such step is not possible.
+	SchemaDiff(from, to *Schema) ([]Change, error)
+
+	// TableDiff returns a diff report for migrating a table
+	// from state "from" to state "to". An error is returned
+	// if such step is not possible.
+	TableDiff(from, to *Table) ([]Change, error)
+}
 
 // changes.
 func (*AddAttr) change()          {}
diff --git a/sql/sqlite/driver.go b/sql/sqlite/driver.go
index e9c5ddfd..3e77a897 100644
--- a/sql/sqlite/driver.go
+++ b/sql/sqlite/driver.go
@@ -8,7 +8,7 @@ import (
 	"fmt"
 
 	"ariga.io/atlas/sql/internal/sqlx"
-
+	"ariga.io/atlas/sql/migrate"
 	"ariga.io/atlas/sql/schema"
 )
 
@@ -18,8 +18,8 @@ type (
 	Driver struct {
 		conn
 		schema.Differ
-		schema.Execer
 		schema.Inspector
+		migrate.PlanApplier
 	}
 
 	// database connection and its information.
@@ -49,10 +49,10 @@ func Open(db schema.ExecQuerier) (*Driver, error) {
 		return nil, fmt.Errorf("sqlite: scanning database collations: %w", err)
 	}
 	return &Driver{
-		conn:      c,
-		Differ:    &sqlx.Diff{DiffDriver: &diff{c}},
-		Execer:    &planApply{c},
-		Inspector: &inspect{c},
+		conn:        c,
+		Differ:      &sqlx.Diff{DiffDriver: &diff{c}},
+		Inspector:   &inspect{c},
+		PlanApplier: &planApply{c},
 	}, nil
 }
 
diff --git a/sql/sqlite/migrate.go b/sql/sqlite/migrate.go
index 1bbee145..b920efef 100644
--- a/sql/sqlite/migrate.go
+++ b/sql/sqlite/migrate.go
@@ -54,11 +54,6 @@ func (p *planApply) ApplyChanges(ctx context.Context, changes []schema.Change) e
 	return nil
 }
 
-// Exec calls ApplyChanges. Exists here for backwards compatibility.
-func (p *planApply) Exec(ctx context.Context, changes []schema.Change) error {
-	return p.ApplyChanges(ctx, changes)
-}
-
 // state represents the state of a planning. It's not part of
 // planApply so that multiple planning/applying can be called
 // in parallel.


Test patch:
diff --git a/cmd/action/diff_test.go b/cmd/action/diff_test.go
index 3e0206ee..fdcdb7ae 100644
--- a/cmd/action/diff_test.go
+++ b/cmd/action/diff_test.go
@@ -92,7 +92,7 @@ func mockDriver(t *testing.T) *Driver {
 	drv, err := mysql.Open(i)
 	require.NoError(t, err)
 	return &Driver{
-		driver:      drv,
+		Driver:      drv,
 		interceptor: i,
 	}
 }
diff --git a/internal/integration/mysql_test.go b/internal/integration/mysql_test.go
index 6d2f24f9..f03ccccc 100644
--- a/internal/integration/mysql_test.go
+++ b/internal/integration/mysql_test.go
@@ -918,7 +918,7 @@ func (t *myTest) applyHcl(spec string) {
 	existing := realm.Schemas[0]
 	diff, err := t.drv.SchemaDiff(existing, &desired)
 	require.NoError(t, err)
-	err = t.drv.Exec(context.Background(), diff)
+	err = t.drv.ApplyChanges(context.Background(), diff)
 	require.NoError(t, err)
 }
 
@@ -929,7 +929,7 @@ func (t *myTest) diff(t1, t2 *schema.Table) []schema.Change {
 }
 
 func (t *myTest) migrate(changes ...schema.Change) {
-	err := t.drv.Exec(context.Background(), changes)
+	err := t.drv.ApplyChanges(context.Background(), changes)
 	require.NoError(t, err)
 }
 
diff --git a/internal/integration/postgres_test.go b/internal/integration/postgres_test.go
index 8f1d7baa..9f929d46 100644
--- a/internal/integration/postgres_test.go
+++ b/internal/integration/postgres_test.go
@@ -151,7 +151,7 @@ func TestPostgres_ColumnInt(t *testing.T) {
 				Name:    "users",
 				Columns: []*schema.Column{{Name: "a", Type: &schema.ColumnType{Type: &schema.IntegerType{T: "bigint"}}}},
 			}
-			err := t.drv.Exec(ctx, []schema.Change{&schema.AddTable{T: usersT}})
+			err := t.drv.ApplyChanges(ctx, []schema.Change{&schema.AddTable{T: usersT}})
 			require.NoError(t, err)
 			t.dropTables(usersT.Name)
 			change(usersT.Columns[0])
@@ -226,7 +226,7 @@ func TestPostgres_Enums(t *testing.T) {
 		})
 
 		// Create table with an enum column.
-		err := t.drv.Exec(ctx, []schema.Change{&schema.AddTable{T: usersT}})
+		err := t.drv.ApplyChanges(ctx, []schema.Change{&schema.AddTable{T: usersT}})
 		require.NoError(t, err, "create a new table with an enum column")
 		t.dropTables(usersT.Name)
 		ensureNoChange(t, usersT)
@@ -238,7 +238,7 @@ func TestPostgres_Enums(t *testing.T) {
 		)
 		changes := t.diff(t.loadUsers(), usersT)
 		require.Len(t, changes, 1)
-		err = t.drv.Exec(ctx, []schema.Change{&schema.ModifyTable{T: usersT, Changes: changes}})
+		err = t.drv.ApplyChanges(ctx, []schema.Change{&schema.ModifyTable{T: usersT, Changes: changes}})
 		require.NoError(t, err, "add a new enum column to existing table")
 		ensureNoChange(t, usersT)
 
@@ -247,7 +247,7 @@ func TestPostgres_Enums(t *testing.T) {
 		e.Values = append(e.Values, "tuesday")
 		changes = t.diff(t.loadUsers(), usersT)
 		require.Len(t, changes, 1)
-		err = t.drv.Exec(ctx, []schema.Change{&schema.ModifyTable{T: usersT, Changes: changes}})
+		err = t.drv.ApplyChanges(ctx, []schema.Change{&schema.ModifyTable{T: usersT, Changes: changes}})
 		require.NoError(t, err, "append a value to existing enum")
 		ensureNoChange(t, usersT)
 
@@ -256,7 +256,7 @@ func TestPostgres_Enums(t *testing.T) {
 		e.Values = append(e.Values, "wednesday", "thursday", "friday", "saturday")
 		changes = t.diff(t.loadUsers(), usersT)
 		require.Len(t, changes, 1)
-		err = t.drv.Exec(ctx, []schema.Change{&schema.ModifyTable{T: usersT, Changes: changes}})
+		err = t.drv.ApplyChanges(ctx, []schema.Change{&schema.ModifyTable{T: usersT, Changes: changes}})
 		require.NoError(t, err, "append multiple values to existing enum")
 		ensureNoChange(t, usersT)
 	})
@@ -836,7 +836,7 @@ func (t *pgTest) applyHcl(spec string) {
 	existing := realm.Schemas[0]
 	diff, err := t.drv.SchemaDiff(existing, &desired)
 	require.NoError(t, err)
-	err = t.drv.Exec(context.Background(), diff)
+	err = t.drv.ApplyChanges(context.Background(), diff)
 	require.NoError(t, err)
 }
 
@@ -953,7 +953,7 @@ func (t *pgTest) diff(t1, t2 *schema.Table) []schema.Change {
 }
 
 func (t *pgTest) migrate(changes ...schema.Change) {
-	err := t.drv.Exec(context.Background(), changes)
+	err := t.drv.ApplyChanges(context.Background(), changes)
 	require.NoError(t, err)
 }
 
diff --git a/internal/integration/sqlite_test.go b/internal/integration/sqlite_test.go
index 069a63a9..f1512777 100644
--- a/internal/integration/sqlite_test.go
+++ b/internal/integration/sqlite_test.go
@@ -646,7 +646,7 @@ func (t *liteTest) applyHcl(spec string) {
 	existing := realm.Schemas[0]
 	diff, err := t.drv.SchemaDiff(existing, &desired)
 	require.NoError(t, err)
-	err = t.drv.Exec(context.Background(), diff)
+	err = t.drv.ApplyChanges(context.Background(), diff)
 	require.NoError(t, err)
 }
 
@@ -754,7 +754,7 @@ func (t *liteTest) diff(t1, t2 *schema.Table) []schema.Change {
 }
 
 func (t *liteTest) migrate(changes ...schema.Change) {
-	err := t.drv.Exec(context.Background(), changes)
+	err := t.drv.ApplyChanges(context.Background(), changes)
 	require.NoError(t, err)
 }
 
diff --git a/sql/mysql/migrate_test.go b/sql/mysql/migrate_test.go
index 8a3761df..e898f660 100644
--- a/sql/mysql/migrate_test.go
+++ b/sql/mysql/migrate_test.go
@@ -282,5 +282,5 @@ func newMigrate(version string) (migrate.PlanApplier, *mock, error) {
 	if err != nil {
 		return nil, nil, err
 	}
-	return drv.Execer.(migrate.PlanApplier), mk, nil
+	return drv, mk, nil
 }
diff --git a/sql/postgres/migrate_test.go b/sql/postgres/migrate_test.go
index f84255ab..518e07a6 100644
--- a/sql/postgres/migrate_test.go
+++ b/sql/postgres/migrate_test.go
@@ -140,7 +140,7 @@ func TestPlanChanges(t *testing.T) {
 		mock{m}.version("130000")
 		drv, err := Open(db)
 		require.NoError(t, err)
-		plan, err := drv.Execer.(migrate.PlanApplier).PlanChanges(context.Background(), "plan", tt.changes)
+		plan, err := drv.PlanChanges(context.Background(), "plan", tt.changes)
 		require.NoError(t, err)
 		require.Equal(t, tt.plan.Reversible, plan.Reversible)
 		require.Equal(t, tt.plan.Transactional, plan.Transactional)
diff --git a/sql/sqlite/migrate_test.go b/sql/sqlite/migrate_test.go
index f32c997e..f24caec8 100644
--- a/sql/sqlite/migrate_test.go
+++ b/sql/sqlite/migrate_test.go
@@ -127,7 +127,7 @@ func TestPlanChanges(t *testing.T) {
 		m.systemVars("3.36.0")
 		drv, err := Open(db)
 		require.NoError(t, err)
-		plan, err := drv.Execer.(migrate.PlanApplier).PlanChanges(context.Background(), "plan", tt.changes)
+		plan, err := drv.PlanChanges(context.Background(), "plan", tt.changes)
 		require.NoError(t, err)
 		require.Equal(t, tt.plan.Reversible, plan.Reversible)
 		require.Equal(t, tt.plan.Transactional, plan.Transactional)