Skip to content

Commit

Permalink
Fix up new migration
Browse files Browse the repository at this point in the history
  • Loading branch information
raoptimus committed Jan 16, 2024
1 parent 34d67ae commit 1458f88
Show file tree
Hide file tree
Showing 7 changed files with 65 additions and 34 deletions.
2 changes: 1 addition & 1 deletion internal/action/dependency.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
13 changes: 8 additions & 5 deletions internal/action/history_new.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand All @@ -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
}
29 changes: 14 additions & 15 deletions internal/action/mockaction/MigrationService.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions internal/action/upgrade.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down Expand Up @@ -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) {
Expand Down
2 changes: 1 addition & 1 deletion internal/action/upgrade_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
37 changes: 34 additions & 3 deletions internal/migrator/db_service_integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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,
Expand Down
12 changes: 5 additions & 7 deletions internal/service/migration.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ const (
baseMigration = "000000_000000_base"
baseMigrationsCount = 1
defaultLimit = 10000
maxLimit = 100000
regexpFileNameGroupCount = 5
)

Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -150,6 +147,7 @@ func (m *Migration) NewMigrations(ctx context.Context, limit int) (entity.Migrat
}

newMigrations.SortByVersion()

return newMigrations, err
}

Expand Down

0 comments on commit 1458f88

Please sign in to comment.