Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add goto flag(#2689) #3505

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 9 additions & 2 deletions cli/commands/migrate_apply.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ func newMigrateApplyCmd(ec *cli.ExecutionContext) *cobra.Command {
f.StringVar(&opts.downMigration, "down", "", "apply all or N down migration steps")
f.StringVar(&opts.versionMigration, "version", "", "only apply this particular migration")
f.StringVar(&opts.migrationType, "type", "up", "type of migration (up, down) to be used with version flag")
f.StringVar(&opts.goTo, "goto", "", "migrate to a particular version")
f.BoolVar(&opts.skipExecution, "skip-execution", false, "skip executing the migration action, but mark them as applied")

f.String("endpoint", "", "http(s) endpoint for Hasura GraphQL Engine")
Expand All @@ -63,10 +64,11 @@ type migrateApplyOptions struct {
versionMigration string
migrationType string
skipExecution bool
goTo string
}

func (o *migrateApplyOptions) run() error {
migrationType, step, err := getMigrationTypeAndStep(o.upMigration, o.downMigration, o.versionMigration, o.migrationType, o.skipExecution)
migrationType, step, err := getMigrationTypeAndStep(o.upMigration, o.downMigration, o.versionMigration, o.migrationType, o.goTo, o.skipExecution)
if err != nil {
return errors.Wrap(err, "error validating flags")
}
Expand Down Expand Up @@ -97,7 +99,7 @@ func (o *migrateApplyOptions) run() error {

// Only one flag out of up, down and version can be set at a time. This function
// checks whether that is the case and returns an error is not
func getMigrationTypeAndStep(upMigration, downMigration, versionMigration, migrationType string, skipExecution bool) (string, int64, error) {
func getMigrationTypeAndStep(upMigration, downMigration, versionMigration, migrationType, goTo string, skipExecution bool) (string, int64, error) {
var flagCount = 0
var stepString = "all"
var migrationName = "up"
Expand All @@ -119,6 +121,11 @@ func getMigrationTypeAndStep(upMigration, downMigration, versionMigration, migra
flagCount++
}

if goTo != "" {
migrationName = "goto"
stepString = goTo
}

if flagCount > 1 {
return "", 0, errors.New("Only one migration type can be applied at a time (--up, --down or --goto)")
}
Expand Down
81 changes: 81 additions & 0 deletions cli/migrate/migrate.go
Original file line number Diff line number Diff line change
Expand Up @@ -484,6 +484,27 @@ func (m *Migrate) Migrate(version uint64, direction string) error {
return m.unlockErr(m.runMigrations(ret))
}

func (m *Migrate) MigrateTo(version uint64) error {
mode, err := m.databaseDrv.GetSetting("migration_mode")
if err != nil {
return err
}

if mode != "true" {
return ErrNoMigrationMode
}

if err := m.lock(); err != nil {
return err
}

ret := make(chan interface{}, m.PrefetchMigrations)
go m.readTo(version, ret)

return m.unlockErr(m.runMigrations(ret))

}

// Steps looks at the currently active migration version.
// It will migrate up if n > 0, and down if n < 0.
func (m *Migrate) Steps(n int64) error {
Expand Down Expand Up @@ -833,6 +854,7 @@ func (m *Migrate) read(version uint64, direction string, ret chan<- interface{})
}
}


// readUp reads up migrations from `from` limitted by `limit`.
// limit can be -1, implying no limit and reading until there are no more migrations.
// Each migration is then written to the ret channel.
Expand Down Expand Up @@ -960,6 +982,65 @@ func (m *Migrate) readUp(limit int64, ret chan<- interface{}) {
}
}

func (m *Migrate) readTo(to uint64, ret chan<- interface{}) {
defer close(ret)

directions := m.sourceDrv.GetDirections(to)

from, _, err := m.databaseDrv.Version()
if err != nil {
ret <- err
return
}

curr := uint64(from)

for curr != to {
if m.stop() {
return
}

err = m.versionDownExists(curr)
if err != nil {
ret <- err
return
}

next, ok := curr, true
if directions[source.Up] {
next, err = m.sourceDrv.Next(curr)
if err != nil {
ret <- err
return
}
} else {
next, ok = m.databaseDrv.Prev(curr)
if !ok {
return
}
}

migr, err := m.metanewMigration(uint64(int64(curr)), int64(next))
if err != nil {
ret <- err
return
}

ret <- migr
go migr.Buffer()

migr, err = m.newMigration(curr, int64(next))
if err != nil {
ret <- err
return
}

ret <- migr
go migr.Buffer()
curr = next
}
}

// readDown reads down migrations from `from` limitted by `limit`.
// limit can be -1, implying no limit and reading until there are no more migrations.
// Each migration is then written to the ret channel.
Expand Down