Skip to content

Commit

Permalink
feat(migrations): Adding steps count for the migrations (#25)
Browse files Browse the repository at this point in the history
Adding steps count for the migrations
  • Loading branch information
Jacobbrewer1 authored Oct 19, 2024
1 parent 90d0c93 commit 035b450
Show file tree
Hide file tree
Showing 7 changed files with 27 additions and 5 deletions.
2 changes: 1 addition & 1 deletion cmd/schema/cmd_create.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ func (c *createCmd) Execute(_ context.Context, _ *flag.FlagSet, _ ...interface{}
// The timestamp is the current time in the format YYYYMMDDHHMMSS
// The name is the name of the migration with spaces as underscores

now := time.Now()
now := time.Now().UTC()
name := fmt.Sprintf("%s_%s", now.Format(migrations.FilePrefix), strings.TrimSpace(c.name))
name = strings.ReplaceAll(name, " ", "_")

Expand Down
8 changes: 6 additions & 2 deletions cmd/schema/cmd_migrate.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@ type migrateCmd struct {

// migrationLocation is where the migrations are located.
migrationLocation string

// steps is the number of steps to migrate.
steps int
}

func (m *migrateCmd) Name() string {
Expand All @@ -41,6 +44,7 @@ func (m *migrateCmd) SetFlags(f *flag.FlagSet) {
f.BoolVar(&m.up, "up", false, "Migrate up.")
f.BoolVar(&m.down, "down", false, "Migrate down.")
f.StringVar(&m.migrationLocation, "loc", "./migrations", "The location of the migrations.")
f.IntVar(&m.steps, "steps", 0, "The number of steps to migrate (0 means all).")
}

func (m *migrateCmd) Execute(ctx context.Context, _ *flag.FlagSet, _ ...interface{}) subcommands.ExitStatus {
Expand Down Expand Up @@ -71,12 +75,12 @@ func (m *migrateCmd) Execute(ctx context.Context, _ *flag.FlagSet, _ ...interfac

switch {
case m.up:
if err := migrations.NewVersioning(db, absPath).MigrateUp(); err != nil {
if err := migrations.NewVersioning(db, absPath, m.steps).MigrateUp(); err != nil {
slog.Error("Error migrating up", slog.String("error", err.Error()))
return subcommands.ExitFailure
}
case m.down:
if err := migrations.NewVersioning(db, absPath).MigrateDown(); err != nil {
if err := migrations.NewVersioning(db, absPath, m.steps).MigrateDown(); err != nil {
slog.Error("Error migrating down", slog.String("error", err.Error()))
return subcommands.ExitFailure
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
ALTER TABLE test DROP COLUMN age;
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
ALTER TABLE test ADD COLUMN age INT;
6 changes: 6 additions & 0 deletions pkg/migrations/down.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ func (v *versioning) MigrateDown() error {
}

// Migrate down.
count := 0
for _, f := range orderedFiles {
slog.Debug(fmt.Sprintf("Migrating up: %s", f.Name()))

Expand Down Expand Up @@ -71,6 +72,11 @@ func (v *versioning) MigrateDown() error {
continue
}

if v.steps > 0 && count == v.steps {
break
}
count++

// Migrate down.
if err := v.migrate(f, down); err != nil {
return fmt.Errorf("error migrating down: %w", err)
Expand Down
8 changes: 7 additions & 1 deletion pkg/migrations/up.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ func (v *versioning) MigrateUp() error {
}

// Migrate up.
count := 0
for _, f := range orderedFiles {
slog.Debug(fmt.Sprintf("Migrating up: %s", f.Name()))

Expand All @@ -67,11 +68,16 @@ func (v *versioning) MigrateUp() error {
return fmt.Errorf("error parsing current version: %w", err)
}

if parsed.Before(currentParsed) {
if parsed.Before(currentParsed) || parsed.Equal(currentParsed) {
continue
}
}

if v.steps > 0 && count == v.steps {
break
}
count++

// Migrate up.
if err := v.migrate(f, up); err != nil {
return fmt.Errorf("error migrating up: %w", err)
Expand Down
6 changes: 5 additions & 1 deletion pkg/migrations/versioning.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,16 @@ type versioning struct {

// migrationLocation is the location of the migrations.
migrationLocation string

// steps is the number of steps to migrate.
steps int
}

func NewVersioning(db *sqlx.DB, migrationLocation string) Versioning {
func NewVersioning(db *sqlx.DB, migrationLocation string, steps int) Versioning {
return &versioning{
db: db,
migrationLocation: migrationLocation,
steps: steps,
}
}

Expand Down

0 comments on commit 035b450

Please sign in to comment.