From 56a217bbaa1285f7264decd5c616143ca0d6efe8 Mon Sep 17 00:00:00 2001 From: Euler Date: Thu, 28 Nov 2019 08:36:16 -0300 Subject: [PATCH] add name to migrate status response (close #2376) (#3109) --- cli/commands/migrate_status.go | 5 +++-- cli/commands/migrate_test.go | 8 ++++++++ cli/migrate/migrate.go | 1 + cli/migrate/source/driver.go | 4 ++++ cli/migrate/source/file/file.go | 4 ++++ cli/migrate/source/migration.go | 7 +++++++ cli/migrate/source/stub/stub.go | 4 ++++ cli/migrate/status.go | 3 +++ 8 files changed, 34 insertions(+), 2 deletions(-) diff --git a/cli/commands/migrate_status.go b/cli/commands/migrate_status.go index 587f2e2b407..a32ccd4f53f 100644 --- a/cli/commands/migrate_status.go +++ b/cli/commands/migrate_status.go @@ -74,10 +74,11 @@ func printStatus(status *migrate.Status) *bytes.Buffer { buf := &bytes.Buffer{} out.Init(buf, 0, 8, 2, ' ', 0) w := util.NewPrefixWriter(out) - w.Write(util.LEVEL_0, "VERSION\tSOURCE STATUS\tDATABASE STATUS\n") + w.Write(util.LEVEL_0, "VERSION\tNAME\tSOURCE STATUS\tDATABASE STATUS\n") for _, version := range status.Index { - w.Write(util.LEVEL_0, "%d\t%s\t%s\n", + w.Write(util.LEVEL_0, "%d\t%s\t%s\t%s\n", version, + status.Migrations[version].Name, convertBool(status.Migrations[version].IsPresent), convertBool(status.Migrations[version].IsApplied), ) diff --git a/cli/commands/migrate_test.go b/cli/commands/migrate_test.go index 584192eb516..6585749ad6f 100644 --- a/cli/commands/migrate_test.go +++ b/cli/commands/migrate_test.go @@ -202,11 +202,13 @@ func testMigrate(t *testing.T, endpoint *url.URL, migrationsDir string) { expectedStatus := migrate.NewStatus() expectedStatus.Append(&migrate.MigrationStatus{ Version: 1, + Name: "create_table_test", IsApplied: true, IsPresent: true, }) expectedStatus.Append(&migrate.MigrationStatus{ Version: 2, + Name: "add_table_test", IsApplied: false, IsPresent: true, }) @@ -219,11 +221,13 @@ func testMigrate(t *testing.T, endpoint *url.URL, migrationsDir string) { expectedStatus = migrate.NewStatus() expectedStatus.Append(&migrate.MigrationStatus{ Version: 1, + Name: "create_table_test", IsApplied: true, IsPresent: true, }) expectedStatus.Append(&migrate.MigrationStatus{ Version: 2, + Name: "add_table_test", IsApplied: true, IsPresent: true, }) @@ -236,11 +240,13 @@ func testMigrate(t *testing.T, endpoint *url.URL, migrationsDir string) { expectedStatus = migrate.NewStatus() expectedStatus.Append(&migrate.MigrationStatus{ Version: 1, + Name: "create_table_test", IsApplied: true, IsPresent: true, }) expectedStatus.Append(&migrate.MigrationStatus{ Version: 2, + Name: "add_table_test", IsApplied: false, IsPresent: true, }) @@ -253,11 +259,13 @@ func testMigrate(t *testing.T, endpoint *url.URL, migrationsDir string) { expectedStatus = migrate.NewStatus() expectedStatus.Append(&migrate.MigrationStatus{ Version: 1, + Name: "create_table_test", IsApplied: false, IsPresent: true, }) expectedStatus.Append(&migrate.MigrationStatus{ Version: 2, + Name: "add_table_test", IsApplied: false, IsPresent: true, }) diff --git a/cli/migrate/migrate.go b/cli/migrate/migrate.go index c16a590529f..60b512ba2cd 100644 --- a/cli/migrate/migrate.go +++ b/cli/migrate/migrate.go @@ -255,6 +255,7 @@ func (m *Migrate) newMigrationStatus(version uint64, driverType string) *Migrati if !ok { migrStatus = &MigrationStatus{ Version: version, + Name: m.sourceDrv.ReadName(version), } } diff --git a/cli/migrate/source/driver.go b/cli/migrate/source/driver.go index 4c3a60cf30a..db7b4b83d78 100644 --- a/cli/migrate/source/driver.go +++ b/cli/migrate/source/driver.go @@ -92,6 +92,10 @@ type Driver interface { // it must return os.ErrNotExist. // Do not start reading, just return the ReadCloser! ReadMetaDown(version uint64) (r io.ReadCloser, identifier string, fileName string, err error) + + // ReadName returns an name that helps + // finding this migration in the source for a given version + ReadName(version uint64) (name string) } // Open returns a new driver instance. diff --git a/cli/migrate/source/file/file.go b/cli/migrate/source/file/file.go index 9193e1c224a..bcf3756b438 100644 --- a/cli/migrate/source/file/file.go +++ b/cli/migrate/source/file/file.go @@ -219,3 +219,7 @@ func (f *File) ReadMetaDown(version uint64) (r io.ReadCloser, identifier string, } return nil, "", "", &os.PathError{Op: fmt.Sprintf("read version %v", version), Path: f.path, Err: os.ErrNotExist} } + +func (f *File) ReadName(version uint64) (name string) { + return f.migrations.ReadName(version) +} diff --git a/cli/migrate/source/migration.go b/cli/migrate/source/migration.go index 79313181aeb..946f44d2d54 100644 --- a/cli/migrate/source/migration.go +++ b/cli/migrate/source/migration.go @@ -166,6 +166,13 @@ func (i *Migrations) MetaDown(version uint64) (m *Migration, ok bool) { return nil, false } +func (i *Migrations) ReadName(version uint64) (name string) { + for k := range i.migrations[version] { + return i.migrations[version][k].Identifier + } + return "-" +} + func (i *Migrations) findPos(version uint64) int { if len(i.index) > 0 { ix := i.index.Search(version) diff --git a/cli/migrate/source/stub/stub.go b/cli/migrate/source/stub/stub.go index 72d032b9082..e5b0e4fce15 100644 --- a/cli/migrate/source/stub/stub.go +++ b/cli/migrate/source/stub/stub.go @@ -108,3 +108,7 @@ func (s *Stub) ReadMetaDown(version uint64) (r io.ReadCloser, identifier string, } return nil, "", "", &os.PathError{Op: fmt.Sprintf("read down yaml version %v", version), Path: s.Url, Err: os.ErrNotExist} } + +func (f *Stub) ReadName(version uint64) (name string) { + return f.Migrations.ReadName(version) +} diff --git a/cli/migrate/status.go b/cli/migrate/status.go index 8a7954ebbdf..d69200d3b87 100644 --- a/cli/migrate/status.go +++ b/cli/migrate/status.go @@ -8,6 +8,9 @@ type MigrationStatus struct { // Version is the version of this migration. Version uint64 `json:"-"` + // Name helps finding this migration in the source folder + Name string `json:"-"` + // Check if the migration is applied on the cluster IsApplied bool `json:"database_status"`