Repository: ariga/atlas
Pull request #1107 (2022-08-29 13:20:36)

drop unnecessary revisions type


Patch:
diff --git a/cmd/atlas/internal/cmdapi/migrate.go b/cmd/atlas/internal/cmdapi/migrate.go
index bd35afcb..c259e770 100644
--- a/cmd/atlas/internal/cmdapi/migrate.go
+++ b/cmd/atlas/internal/cmdapi/migrate.go
@@ -632,7 +632,7 @@ func CmdMigrateStatusRun(cmd *cobra.Command, _ []string) error {
 	return statusPrint(cmd.OutOrStdout(), avail, pending, revs)
 }
 
-func statusPrint(out io.Writer, avail, pending []migrate.File, revs migrate.Revisions) (err error) {
+func statusPrint(out io.Writer, avail, pending []migrate.File, revs []*migrate.Revision) (err error) {
 	var (
 		cur, next, state string
 		applied          = avail[: len(avail)-len(pending) : len(avail)-len(pending)]
diff --git a/cmd/atlas/internal/migrate/migrate.go b/cmd/atlas/internal/migrate/migrate.go
index 7a06dab5..0ee07029 100644
--- a/cmd/atlas/internal/migrate/migrate.go
+++ b/cmd/atlas/internal/migrate/migrate.go
@@ -112,12 +112,12 @@ func (r *EntRevisions) ReadRevision(ctx context.Context, v string) (*migrate.Rev
 // ReadRevisions reads the revisions from the revisions table.
 //
 // ReadRevisions will not return results only saved to cache.
-func (r *EntRevisions) ReadRevisions(ctx context.Context) (migrate.Revisions, error) {
+func (r *EntRevisions) ReadRevisions(ctx context.Context) ([]*migrate.Revision, error) {
 	revs, err := r.ec.Revision.Query().Order(ent.Asc(revision.FieldID)).All(ctx)
 	if err != nil {
 		return nil, err
 	}
-	ret := make(migrate.Revisions, len(revs))
+	ret := make([]*migrate.Revision, len(revs))
 	for i, rev := range revs {
 		ret[i] = fromEnt(rev)
 	}
diff --git a/sql/migrate/migrate.go b/sql/migrate/migrate.go
index 094d4bd4..49055ede 100644
--- a/sql/migrate/migrate.go
+++ b/sql/migrate/migrate.go
@@ -167,7 +167,7 @@ type (
 		// Ident returns an object identifies this history table.
 		Ident() *TableIdent
 		// ReadRevisions returns all revisions.
-		ReadRevisions(context.Context) (Revisions, error)
+		ReadRevisions(context.Context) ([]*Revision, error)
 		// ReadRevision returns a revision by version.
 		// Returns ErrRevisionNotExist if the version does not exist.
 		ReadRevision(context.Context, string) (*Revision, error)
@@ -207,9 +207,6 @@ type (
 	// RevisionType defines the type of the revision record in the history table.
 	RevisionType uint
 
-	// Revisions is an ordered set of Revision structs.
-	Revisions []*Revision
-
 	// Executor is responsible to manage and execute a set of migration files against a database.
 	Executor struct {
 		drv         Driver             // The Driver to access and manage the database.
@@ -733,7 +730,7 @@ func (NopRevisionReadWriter) Ident() *TableIdent {
 }
 
 // ReadRevisions implements RevisionsReadWriter.ReadRevisions.
-func (NopRevisionReadWriter) ReadRevisions(context.Context) (Revisions, error) {
+func (NopRevisionReadWriter) ReadRevisions(context.Context) ([]*Revision, error) {
 	return nil, nil
 }
 
@@ -835,7 +832,7 @@ func (NopLogger) Log(LogEntry) {}
 
 // LogIntro gathers some meta information from the migration files and stored revisions to
 // log some general information prior to actual execution.
-func LogIntro(l Logger, revs Revisions, files []File) error {
+func LogIntro(l Logger, revs []*Revision, files []File) error {
 	names := make([]string, len(files))
 	for i := range files {
 		names[i] = files[i].Name()
@@ -858,7 +855,7 @@ func wrap(err1, err2 error) error {
 
 // RevisionsLastIndex returns the index of the last revision
 // satisfying f(i), or -1 if none do.
-func RevisionsLastIndex(revs Revisions, f func(*Revision) bool) int {
+func RevisionsLastIndex(revs []*Revision, f func(*Revision) bool) int {
 	for i := len(revs) - 1; i >= 0; i-- {
 		if f(revs[i]) {
 			return i


Test patch:
diff --git a/internal/integration/integration_test.go b/internal/integration/integration_test.go
index 4b013967..c5b3c703 100644
--- a/internal/integration/integration_test.go
+++ b/internal/integration/integration_test.go
@@ -523,7 +523,7 @@ func plan(t T, name string, changes ...schema.Change) *migrate.Plan {
 	return p
 }
 
-type rrw migrate.Revisions
+type rrw []*migrate.Revision
 
 func (r *rrw) Ident() *migrate.TableIdent {
 	return &migrate.TableIdent{}
@@ -549,12 +549,12 @@ func (r *rrw) ReadRevision(_ context.Context, v string) (*migrate.Revision, erro
 	return nil, migrate.ErrRevisionNotExist
 }
 
-func (r *rrw) ReadRevisions(context.Context) (migrate.Revisions, error) {
-	return migrate.Revisions(*r), nil
+func (r *rrw) ReadRevisions(context.Context) ([]*migrate.Revision, error) {
+	return []*migrate.Revision(*r), nil
 }
 
 func (r *rrw) clean() {
-	*r = rrw(migrate.Revisions{})
+	*r = rrw([]*migrate.Revision{})
 }
 
 func buildCmd(t *testing.T) (string, error) {
diff --git a/sql/migrate/migrate_test.go b/sql/migrate/migrate_test.go
index 4725e00a..4d2a1ae3 100644
--- a/sql/migrate/migrate_test.go
+++ b/sql/migrate/migrate_test.go
@@ -179,19 +179,19 @@ func TestExecutor_Pending(t *testing.T) {
 	require.Len(t, p, 3)
 
 	// 2 are pending.
-	*rrw = mockRevisionReadWriter(migrate.Revisions{rev1})
+	*rrw = mockRevisionReadWriter([]*migrate.Revision{rev1})
 	p, err = ex.Pending(context.Background())
 	require.NoError(t, err)
 	require.Len(t, p, 2)
 
 	// Only the last one is pending (in full).
-	*rrw = mockRevisionReadWriter(migrate.Revisions{rev1, rev2})
+	*rrw = mockRevisionReadWriter([]*migrate.Revision{rev1, rev2})
 	p, err = ex.Pending(context.Background())
 	require.NoError(t, err)
 	require.Len(t, p, 1)
 
 	// First statement of last one is marked as applied, second isn't. Third file is still pending.
-	*rrw = mockRevisionReadWriter(migrate.Revisions{rev1, rev2, rev3})
+	*rrw = mockRevisionReadWriter([]*migrate.Revision{rev1, rev2, rev3})
 	p, err = ex.Pending(context.Background())
 	require.NoError(t, err)
 	require.Len(t, p, 1)
@@ -199,14 +199,14 @@ func TestExecutor_Pending(t *testing.T) {
 	// All versions have revisions, but are last two are rolled back (0 applied).
 	rev2.Applied = 0
 	rev3.Applied = 0
-	*rrw = mockRevisionReadWriter(migrate.Revisions{rev1, rev2, rev3})
+	*rrw = mockRevisionReadWriter([]*migrate.Revision{rev1, rev2, rev3})
 	p, err = ex.Pending(context.Background())
 	require.NoError(t, err)
 	require.Len(t, p, 2)
 
 	// All versions have revisions, but are all rolled back (0 applied).
 	rev1.Applied = 0
-	*rrw = mockRevisionReadWriter(migrate.Revisions{rev1, rev2, rev3})
+	*rrw = mockRevisionReadWriter([]*migrate.Revision{rev1, rev2, rev3})
 	p, err = ex.Pending(context.Background())
 	require.NoError(t, err)
 	require.Len(t, p, 3)
@@ -269,7 +269,7 @@ func TestExecutor(t *testing.T) {
 	require.Equal(t, drv.executed, []string{
 		"CREATE TABLE t_sub(c int);", "ALTER TABLE t_sub ADD c1 int;", "ALTER TABLE t_sub ADD c2 int;",
 	})
-	requireEqualRevisions(t, migrate.Revisions{rev1, rev2}, migrate.Revisions(*rrw))
+	requireEqualRevisions(t, []*migrate.Revision{rev1, rev2}, []*migrate.Revision(*rrw))
 	require.Equal(t, []migrate.LogEntry{
 		migrate.LogExecution{To: "2.10.x-20", Files: []string{"1.a_sub.up.sql", "2.10.x-20_description.sql"}},
 		migrate.LogFile{Version: "1.a", Desc: "sub.up"},
@@ -292,13 +292,13 @@ func TestExecutor(t *testing.T) {
 
 	require.NoError(t, ex.ExecuteN(context.Background(), 1))
 	require.Equal(t, []string{"CREATE TABLE t_sub(c int);", "ALTER TABLE t_sub ADD c1 int;"}, drv.executed)
-	requireEqualRevisions(t, migrate.Revisions{rev1}, migrate.Revisions(*rrw))
+	requireEqualRevisions(t, []*migrate.Revision{rev1}, []*migrate.Revision(*rrw))
 
 	require.NoError(t, ex.ExecuteN(context.Background(), 1))
 	require.Equal(t, []string{
 		"CREATE TABLE t_sub(c int);", "ALTER TABLE t_sub ADD c1 int;", "ALTER TABLE t_sub ADD c2 int;",
 	}, drv.executed)
-	requireEqualRevisions(t, migrate.Revisions{rev1, rev2}, migrate.Revisions(*rrw))
+	requireEqualRevisions(t, []*migrate.Revision{rev1, rev2}, []*migrate.Revision(*rrw))
 
 	// Partly is pending.
 	p, err = ex.Pending(context.Background())
@@ -307,12 +307,12 @@ func TestExecutor(t *testing.T) {
 	require.Equal(t, "3_partly.sql", p[0].Name())
 
 	// Suppose first revision is already executed, only execute second migration file.
-	*rrw = mockRevisionReadWriter(migrate.Revisions{rev1})
+	*rrw = mockRevisionReadWriter([]*migrate.Revision{rev1})
 	*drv = mockDriver{}
 
 	require.NoError(t, ex.ExecuteN(context.Background(), 1))
 	require.Equal(t, []string{"ALTER TABLE t_sub ADD c2 int;"}, drv.executed)
-	requireEqualRevisions(t, migrate.Revisions{rev1, rev2}, migrate.Revisions(*rrw))
+	requireEqualRevisions(t, []*migrate.Revision{rev1, rev2}, []*migrate.Revision(*rrw))
 
 	// Partly is pending.
 	p, err = ex.Pending(context.Background())
@@ -321,7 +321,7 @@ func TestExecutor(t *testing.T) {
 	require.Equal(t, "3_partly.sql", p[0].Name())
 
 	// Failing, counter will be correct.
-	*rrw = mockRevisionReadWriter(migrate.Revisions{rev1, rev2})
+	*rrw = mockRevisionReadWriter([]*migrate.Revision{rev1, rev2})
 	*drv = mockDriver{}
 	drv.failOn(2, errors.New("this is an error"))
 	require.ErrorContains(t, ex.ExecuteN(context.Background(), 1), "this is an error")
@@ -510,7 +510,7 @@ func (m *mockDriver) CheckClean(context.Context, *migrate.TableIdent) error {
 	return nil
 }
 
-type mockRevisionReadWriter migrate.Revisions
+type mockRevisionReadWriter []*migrate.Revision
 
 func (mockRevisionReadWriter) Ident() *migrate.TableIdent {
 	return nil
@@ -536,7 +536,7 @@ func (rrw *mockRevisionReadWriter) WriteRevision(_ context.Context, r *migrate.R
 }
 
 func (rrw *mockRevisionReadWriter) ReadRevision(_ context.Context, v string) (*migrate.Revision, error) {
-	for _, r := range migrate.Revisions(*rrw) {
+	for _, r := range []*migrate.Revision(*rrw) {
 		if r.Version == v {
 			return r, nil
 		}
@@ -544,19 +544,19 @@ func (rrw *mockRevisionReadWriter) ReadRevision(_ context.Context, v string) (*m
 	return nil, migrate.ErrRevisionNotExist
 }
 
-func (rrw *mockRevisionReadWriter) ReadRevisions(context.Context) (migrate.Revisions, error) {
-	return migrate.Revisions(*rrw), nil
+func (rrw *mockRevisionReadWriter) ReadRevisions(context.Context) ([]*migrate.Revision, error) {
+	return []*migrate.Revision(*rrw), nil
 }
 
 func (rrw *mockRevisionReadWriter) clean() {
-	*rrw = mockRevisionReadWriter(migrate.Revisions{})
+	*rrw = mockRevisionReadWriter([]*migrate.Revision{})
 }
 
 type mockLogger []migrate.LogEntry
 
 func (m *mockLogger) Log(e migrate.LogEntry) { *m = append(*m, e) }
 
-func requireEqualRevisions(t *testing.T, expected, actual migrate.Revisions) {
+func requireEqualRevisions(t *testing.T, expected, actual []*migrate.Revision) {
 	require.Equal(t, len(expected), len(actual))
 	for i := range expected {
 		requireEqualRevision(t, expected[i], actual[i])