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

Callbacks or pre-migration scripts to aid testing #1157

Open
rajeevs opened this issue Sep 10, 2024 · 1 comment
Open

Callbacks or pre-migration scripts to aid testing #1157

rajeevs opened this issue Sep 10, 2024 · 1 comment

Comments

@rajeevs
Copy link

rajeevs commented Sep 10, 2024

Is your feature request related to a problem? Please describe.
I have some migrations that backfill data into a new table using another table.

I want to be able to test that the backfill migration worked correctly, but in order to test it, I need to insert data into the original table before the migrations run.

Overall, there are situations in test where you want to insert data into the DB to test migrations are correct. Testing on empty DB is one use case, but testing when data is not empty mirrors production more.

Describe the solution you'd like
It would be ideal if there was a callback mechanism / event handler that we can register with golang-migrate that executes before/after a migration runs. (event params could be eventType:[Before/After], and migrationName and a db connection/session object).

We would use this during test only to intercept pre-migration event for backfill migration, and insert the data we want into the original table. Then we would check at the end of all migrations being run that the state of DB is what is expected.

This is similar to FlyWay Java callbacks. https://documentation.red-gate.com/fd/callback-concept-184127466.html

This would also address situations like #136 which could be achieved using a callback (to insert data).

Note that what I propose is an optional way to add callbacks in special situations. It should not affect existing use cases.

Describe alternatives you've considered
Other solutions, could be

  1. A SQL callback: You provide a SQL file (named in a suitable way to indicate which migration it is connected to). This is similar to Flyway SQL callbacks. I think programmatic is better, so we can insert it only in test code.
  2. Another would be for Up to run till a specific migration (like Up(sentinelMigrationId, inclusive)). Then we could run
Up(BakfillMigrationId, false)
// insert some test data
Up() // run everything
// test backfill was correct

But I think my proposed solution is better.

Additional context
Of course, we are opening up a way for people to run code in the migration sequence.
But it is not magical, and someone would have to explicitly register a callback.

@rajeevs
Copy link
Author

rajeevs commented Sep 11, 2024

@dhui : Your thoughts?

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

1 participant