[12.x] Introduce Migration@shouldPrune()
#55979
Closed
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
What
Allow specifying if a migration should be considered prunable by the schema:dump command.
Why
We often insert data into our DB via migrations. While we could do this via a seeder, it's really not terribly convenient. Having data insertions done in migrations disallows us from taking advantage of pruning our migrations into a single SQL file.
Why not just use seeders?
This requires a person to SSH into each environment and run a seeder upon deploying the code changes. This is easy to forget to do. It also requires all devs on the team to be bothered to run a seeder. This coordination costs time and headaches.Furthermore, say we have a seeder that stores four fiscal years in our budget. Next week, our product owner tells us that we actually need to add another year into the future due to product reasons that no developer really understands. What are the options here? Create a seeder that adds a single entry? Modify the existing seeder but disable foreign key checks, truncate the table, and then re-insert?
How
Here we have simply added a method called
shouldPrune()
to theMigration
class. This is akin to theshouldRun()
method that was added earlier this year by @danmatthews. The method can be overridden in these migrations where data is being manipulated in the database.This will slightly slow down the schema pruning, as it is going to instantiate each Migration class to check if it should be pruned or not. Since I would assume this command is run infrequently, I don't think this risk is terribly great.
I have also modified the MigrationsPruned event to include a list of the migrations that were deleted. Don't know if anyone would ever need that, but there they are.
Gotchas
It is on the developer to know if the migrations they are marking as non-prunable will have side effects if the rest of their schema has been dumped to a single file.
Potential breaking change
I have updated one method in the Migrator class from protected to public. I could work around this by writing a public method which calls the protected method 🤷
Hey, where are the tests?
I noticed that this command didn't have any tests to begin with. If requested, I can figure out that how to make that happen.
Here's a screen recording of this working locally:
should_not_prune.mov