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

rollback: export ErrNoRollback to rollback all #292

Closed
gnuletik opened this issue Apr 26, 2022 · 6 comments
Closed

rollback: export ErrNoRollback to rollback all #292

gnuletik opened this issue Apr 26, 2022 · 6 comments

Comments

@gnuletik
Copy link
Contributor

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!

@amacneil
Copy link
Owner

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

gnuletik added a commit to gnuletik/dbmate that referenced this issue May 31, 2022
@gnuletik
Copy link
Contributor Author

Great ! PR done.

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

@amacneil
Copy link
Owner

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?

@gnuletik
Copy link
Contributor Author

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.

@pawndev
Copy link

pawndev commented Jun 3, 2022

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 !

@gnuletik
Copy link
Contributor Author

gnuletik commented Jun 3, 2022

Happy to help :)

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants