Skip to content

[12.x] Introduce Migration@shouldPrune() #55979

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

Closed

Conversation

cosmastech
Copy link
Contributor

@cosmastech cosmastech commented Jun 11, 2025

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 the Migration class. This is akin to the shouldRun() 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

{
$connection = $connections->connection($database = $this->input->getOption('database'));
Copy link
Contributor Author

@cosmastech cosmastech Jun 11, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The $database variable was unused, probably a relic from the time before PHP had named args.

@taylorotwell
Copy link
Member

I honestly worry this would have a lot of gotchas as you mention, especially in regards to the order things are run. You would really have to think hard about whether this migration can be run after all of your other migrations even though it existed in a certain point in time when maybe the database was in a different state or the schema was in a wildly different state.

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

Successfully merging this pull request may close these issues.

2 participants