Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
mfridman committed Jul 9, 2022
1 parent 5fef684 commit f958903
Show file tree
Hide file tree
Showing 4 changed files with 132 additions and 114 deletions.
10 changes: 9 additions & 1 deletion provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,15 @@ type Provider struct {
dialect dialect.SQL
}

// Apply applies a migration at a given version up. This is only useful for testing.
func (p *Provider) Apply(ctx context.Context, version int64) error {
migration, err := p.migrations.Current(version)
if err != nil {
return err
}
return p.startMigration(ctx, true, migration)
}

func (p *Provider) ListMigrations() Migrations {
return p.migrations
}
Expand Down Expand Up @@ -306,7 +315,6 @@ func (p *Provider) up(ctx context.Context, upByOne bool, version int64) error {
return fmt.Errorf("version must be a number greater than zero: %d", version)
}
if p.opt.NoVersioning {
fmt.Println("here", upByOne)
// This code path does not rely on database state to resolve which
// migrations have already been applied. Instead we blindly apply
// the requested migrations when user requests no versioning.
Expand Down
26 changes: 19 additions & 7 deletions provider_internal.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ func (p *Provider) UpTo(ctx context.Context, version int64) error {
func (p *Provider) Down(ctx context.Context) error {
if p.opt.NoVersioning {
currentVersion := p.migrations[len(p.migrations)-1].Version
fmt.Println(">>", currentVersion)
// Migrate only the latest migration down.
return p.downToNoVersioning(ctx, currentVersion-1)
}
Expand All @@ -45,7 +44,6 @@ func (p *Provider) Down(ctx context.Context) error {

func (p *Provider) downToNoVersioning(ctx context.Context, version int64) error {
for i := len(p.migrations) - 1; i >= 0; i-- {
fmt.Println(version, p.migrations[i].Version)
if version >= p.migrations[i].Version {
return nil
}
Expand Down Expand Up @@ -93,15 +91,29 @@ func (p *Provider) DownTo(ctx context.Context, version int64) error {
}
}

// Redo reapplies the last migration. This is equivalent to running Down followed by UpByOne.
// Redo reapplies the last migration by migrating down and then up.
func (p *Provider) Redo(ctx context.Context) error {
if err := p.Down(ctx); err != nil {
return err
var migration *Migration
var err error
if p.opt.NoVersioning {
migration, err = p.migrations.Last()
if err != nil {
return err
}
} else {
currentVersion, err := p.CurrentVersion(ctx)
if err != nil {
return err
}
migration, err = p.migrations.Current(currentVersion)
if err != nil {
return err
}
}
if err := p.UpByOne(ctx); err != nil {
if err := p.startMigration(ctx, false, migration); err != nil {
return err
}
return nil
return p.startMigration(ctx, true, migration)
}

// Reset applies all down migrations. This is equivalent to running DownTo 0.
Expand Down
Loading

0 comments on commit f958903

Please sign in to comment.