amacneil / dbmate

🚀 A lightweight, framework-agnostic database migration tool.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

rollback: export ErrNoRollback to rollback all

gnuletik opened this issue · comments

Hi,

First, thanks for this great project!

I'm integrating dbmate as a golang library in my project and, in order to test all migrations, I want to apply all rollback migrations until the original state.
To achieve this, I'm using the following function:

func rollbackAll(mate *dbmate.DB) error {
	for {
		err := mate.Rollback()
		if err == nil {
			continue
		}
		// no more migration to rollback
		if err.Error() == "can't rollback: no migrations have been applied" {
			return nil
		}
		if err != nil {
			return err
		}
	}
}

In order to avoid issues with future upgrades, would it be possible to export the following error as a global ?

return fmt.Errorf("can't rollback: no migrations have been applied")

like:

const ErrNoRollback = errors.New("can't rollback: no migrations have been applied")

I can make a PR if that sounds good to you.

This issue is linked to #221 and #256 but it allows more flexibility when using it as a library.

Thanks!

Hi! Yes that seems like a good idea - PR welcome

Great ! PR done.

I can also implement the RollbackAll() func in a separate PR if you want.

Yeah you could make a PR for rollbackAll too. Curious what you use it for? Why don’t you just truncate or drop all tables?

Great!

I'm using it in a go test which:

  • Creates a temporary database and run all migrations up
  • Rollback all migrations to the initial state
  • Re-run all migrations up

It helps catching missing statements in migrate:down blocks.

For example, if there is a missing DROP TABLE, the last up function will fails with an error like TABLE "something" already exists.

Great!

I'm using it in a go test which:

* Creates a temporary database and run all migrations up

* Rollback all migrations to the initial state

* Re-run all migrations up

It helps catching missing statements in migrate:down blocks.

For example, if there is a missing DROP TABLE, the last up function will fails with an error like TABLE "something" already exists.

Not a bad idea. I'll do that in my project too.
Thank you !

Happy to help :)

For the reference, I created the PR for the RollbackAll func : #303