From 1458f88951e5714861fd5671e51f921279ebb9cd Mon Sep 17 00:00:00 2001 From: raoptimus Date: Wed, 17 Jan 2024 00:34:28 +0300 Subject: [PATCH] Fix up new migration --- internal/action/dependency.go | 2 +- internal/action/history_new.go | 13 ++++--- .../action/mockaction/MigrationService.go | 29 +++++++-------- internal/action/upgrade.go | 4 +- internal/action/upgrade_test.go | 2 +- .../migrator/db_service_integration_test.go | 37 +++++++++++++++++-- internal/service/migration.go | 12 +++--- 7 files changed, 65 insertions(+), 34 deletions(-) diff --git a/internal/action/dependency.go b/internal/action/dependency.go index 1c76ac5..702cadf 100644 --- a/internal/action/dependency.go +++ b/internal/action/dependency.go @@ -53,7 +53,7 @@ type MigrationService interface { Migrations(ctx context.Context, limit int) (entity.Migrations, error) // NewMigrations returns an entities of new migrations //todo: domain.Migrations - NewMigrations(ctx context.Context, limit int) (entity.Migrations, error) + NewMigrations(ctx context.Context) (entity.Migrations, error) // ApplyFile applies new migration //todo: domain.Migration ApplyFile(ctx context.Context, entity *entity.Migration, fileName string, safely bool) error diff --git a/internal/action/history_new.go b/internal/action/history_new.go index 47df318..350f036 100644 --- a/internal/action/history_new.go +++ b/internal/action/history_new.go @@ -32,7 +32,7 @@ func (h *HistoryNew) Run(ctx *cli.Context) error { return err } - migrations, err := h.service.NewMigrations(ctx.Context, limit) + migrations, err := h.service.NewMigrations(ctx.Context) if err != nil { return err } @@ -43,20 +43,23 @@ func (h *HistoryNew) Run(ctx *cli.Context) error { return nil } - if limit > 0 { + if limit > 0 && migrationsCount > limit { + migrations = migrations[:limit] console.Warnf( - "Showing the last %d %s: \n", + "Showing %d out of %d new %s \n", + limit, migrationsCount, console.NumberPlural(migrationsCount, "migration", "migrations"), ) } else { console.Warnf( - "Total %d %s been applied before: \n", + "Found %d new %s \n", migrationsCount, - console.NumberPlural(migrationsCount, "migration has", "migrations have"), + console.NumberPlural(migrationsCount, "migration", "migrations"), ) } printMigrations(migrations, true) + return nil } diff --git a/internal/action/mockaction/MigrationService.go b/internal/action/mockaction/MigrationService.go index e7aa10b..696a450 100644 --- a/internal/action/mockaction/MigrationService.go +++ b/internal/action/mockaction/MigrationService.go @@ -122,25 +122,25 @@ func (_c *MigrationService_Migrations_Call) RunAndReturn(run func(context.Contex return _c } -// NewMigrations provides a mock function with given fields: ctx, limit -func (_m *MigrationService) NewMigrations(ctx context.Context, limit int) (entity.Migrations, error) { - ret := _m.Called(ctx, limit) +// NewMigrations provides a mock function with given fields: ctx +func (_m *MigrationService) NewMigrations(ctx context.Context) (entity.Migrations, error) { + ret := _m.Called(ctx) var r0 entity.Migrations var r1 error - if rf, ok := ret.Get(0).(func(context.Context, int) (entity.Migrations, error)); ok { - return rf(ctx, limit) + if rf, ok := ret.Get(0).(func(context.Context) (entity.Migrations, error)); ok { + return rf(ctx) } - if rf, ok := ret.Get(0).(func(context.Context, int) entity.Migrations); ok { - r0 = rf(ctx, limit) + if rf, ok := ret.Get(0).(func(context.Context) entity.Migrations); ok { + r0 = rf(ctx) } else { if ret.Get(0) != nil { r0 = ret.Get(0).(entity.Migrations) } } - if rf, ok := ret.Get(1).(func(context.Context, int) error); ok { - r1 = rf(ctx, limit) + if rf, ok := ret.Get(1).(func(context.Context) error); ok { + r1 = rf(ctx) } else { r1 = ret.Error(1) } @@ -155,14 +155,13 @@ type MigrationService_NewMigrations_Call struct { // NewMigrations is a helper method to define mock.On call // - ctx context.Context -// - limit int -func (_e *MigrationService_Expecter) NewMigrations(ctx interface{}, limit interface{}) *MigrationService_NewMigrations_Call { - return &MigrationService_NewMigrations_Call{Call: _e.mock.On("NewMigrations", ctx, limit)} +func (_e *MigrationService_Expecter) NewMigrations(ctx interface{}) *MigrationService_NewMigrations_Call { + return &MigrationService_NewMigrations_Call{Call: _e.mock.On("NewMigrations", ctx)} } -func (_c *MigrationService_NewMigrations_Call) Run(run func(ctx context.Context, limit int)) *MigrationService_NewMigrations_Call { +func (_c *MigrationService_NewMigrations_Call) Run(run func(ctx context.Context)) *MigrationService_NewMigrations_Call { _c.Call.Run(func(args mock.Arguments) { - run(args[0].(context.Context), args[1].(int)) + run(args[0].(context.Context)) }) return _c } @@ -172,7 +171,7 @@ func (_c *MigrationService_NewMigrations_Call) Return(_a0 entity.Migrations, _a1 return _c } -func (_c *MigrationService_NewMigrations_Call) RunAndReturn(run func(context.Context, int) (entity.Migrations, error)) *MigrationService_NewMigrations_Call { +func (_c *MigrationService_NewMigrations_Call) RunAndReturn(run func(context.Context) (entity.Migrations, error)) *MigrationService_NewMigrations_Call { _c.Call.Return(run) return _c } diff --git a/internal/action/upgrade.go b/internal/action/upgrade.go index 5d515c8..f25e63c 100644 --- a/internal/action/upgrade.go +++ b/internal/action/upgrade.go @@ -42,7 +42,7 @@ func (u *Upgrade) Run(ctx *cli.Context) error { return err } - migrations, err := u.service.NewMigrations(ctx.Context, limit) + migrations, err := u.service.NewMigrations(ctx.Context) if err != nil { return err } @@ -74,7 +74,7 @@ func (u *Upgrade) Run(ctx *cli.Context) error { printMigrations(migrations, false) - question := fmt.Sprintf("ApplyFile the above %s?", + question := fmt.Sprintf("Apply the above %s?", u.console.NumberPlural(migrations.Len(), "migration", "migrations"), ) if u.interactive && !u.console.Confirm(question) { diff --git a/internal/action/upgrade_test.go b/internal/action/upgrade_test.go index c08e1de..b7f9e81 100644 --- a/internal/action/upgrade_test.go +++ b/internal/action/upgrade_test.go @@ -13,7 +13,7 @@ func TestUpgrade_Run_NoMigrations_NoError(t *testing.T) { serv := mockaction.NewMigrationService(t) serv.EXPECT(). - NewMigrations(ctx.Context, 2). + NewMigrations(ctx.Context). Return(entity.Migrations{}, nil) c := mockaction.NewConsole(t) diff --git a/internal/migrator/db_service_integration_test.go b/internal/migrator/db_service_integration_test.go index 49aba58..1fb194a 100644 --- a/internal/migrator/db_service_integration_test.go +++ b/internal/migrator/db_service_integration_test.go @@ -11,9 +11,9 @@ import ( ) func TestIntegrationDBService_UpDown_Successfully(t *testing.T) { - if testing.Short() { - t.Skip("skipping integration test") - } + //if testing.Short() { + t.Skip("skipping integration test") + //} // region data provider tests := []struct { @@ -96,6 +96,37 @@ func TestIntegrationDBService_UpDown_Successfully(t *testing.T) { } } +func TestIntegrationDBService_Upgrade_AlreadyExistsMigration(t *testing.T) { + if testing.Short() { + t.Skip("skipping integration test") + } + opts := Options{ + DSN: os.Getenv("POSTGRES_DSN"), + Directory: migrationsPathAbs(os.Getenv("POSTGRES_MIGRATIONS_PATH")), + TableName: "migration", + Compact: true, + Interactive: false, + } + dbServ := New(&opts) + + down, err := dbServ.Downgrade() + assert.NoError(t, err) + err = down.Run(cliContext(t, "all")) + assert.NoError(t, err) + + up, err := dbServ.Upgrade() + assert.NoError(t, err) + // apply first migration + err = up.Run(cliContext(t, "1")) + assert.NoError(t, err) + // apply second migration + err = up.Run(cliContext(t, "1")) + assert.NoError(t, err) + // apply third broken migration + err = up.Run(cliContext(t, "1")) + assert.Error(t, err) +} + func assertEqualRowsCount( t *testing.T, ctx context.Context, diff --git a/internal/service/migration.go b/internal/service/migration.go index 8b951c4..c55778a 100644 --- a/internal/service/migration.go +++ b/internal/service/migration.go @@ -26,6 +26,7 @@ const ( baseMigration = "000000_000000_base" baseMigrationsCount = 1 defaultLimit = 10000 + maxLimit = 100000 regexpFileNameGroupCount = 5 ) @@ -102,20 +103,16 @@ func (m *Migration) Migrations(ctx context.Context, limit int) (entity.Migration return migrations, nil } -func (m *Migration) NewMigrations(ctx context.Context, limit int) (entity.Migrations, error) { +func (m *Migration) NewMigrations(ctx context.Context) (entity.Migrations, error) { if err := m.InitializeTableHistory(ctx); err != nil { return nil, err } - if limit < 1 { - limit = defaultLimit - } - migrations, err := m.repo.Migrations(ctx, limit+baseMigrationsCount) + migrations, err := m.repo.Migrations(ctx, maxLimit) if err != nil { return nil, err } - var files []string - files, err = filepath.Glob(filepath.Join(m.options.Directory, "*.up.sql")) + files, err := filepath.Glob(filepath.Join(m.options.Directory, "*.up.sql")) if err != nil { return nil, err } @@ -150,6 +147,7 @@ func (m *Migration) NewMigrations(ctx context.Context, limit int) (entity.Migrat } newMigrations.SortByVersion() + return newMigrations, err }