Seedable Migrations with extra flexibility #50741
Unanswered
Garanaw
asked this question in
Show and tell
Replies: 0 comments
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
Who hasn't built a system where some initial data is needed in the database? And, who hasn't ever built a system where some data was needed for testing? And mocked data only for testing with initial database initialization?
Well, I have. Many times.
Initially, I started using the built in seeders. Then, I went to that company where they didn't like seeders, and used to add data to the database through migrations. I wasn't a big fan of this, so I started digging into the migration system, and discovered two events that fit what I had in mind like a glove.
So, I started building my apps using these events (
MigrationEnded
andMigrationsEnded
). I started with a rudimentary class that listened to these events and would run some seeders configured in my migrations. Later on, I started feeling that it wasn't very flexible, so I decided to build this:https://packagist.org/packages/garanaw/seedable-migrations
It has some documentation, but basically, the idea is that I can now configure my seeders in my migrations, and decide whether they have to run or not based on anything that can be executed inside of a method.
The package allows to run seeders when a migration has ended, or run a batch of seeders when all migrations have ended (if more than one runs with a single command). It allows to take decisions to check if the seeder should run (is my table already seeded? Is this migration altering some data somewhere else? Am I in prod/dev? Or just, am I adding a piece of config to my database that should always be ran, such as languages, a new type of post or anything else?)
I am well aware that some people still use the
down
method (if I recall it right, Taylor wasn't a bug fan of it?), so the methods to retrieve the seeders are named after the methods up and down:upSeeders
anddownSeeders
. Maybe some data has been moved to table A to table B in a migration that is adding a new column to the latest and removing it from the former. Well, with thedownSeeders
this data can be put back (yep, it requires caching the data first, it couldn't be perfect, maybe something to improve if it's a liked package).The migrations should now extend a new
Migration
file:The only method that is required is the
getTable
method, although I reckon I will get rid of it as I only added it for my own usage, but it's not really used anywhere. The design is definitely not the best, but I'm trying to work on fixing redundant stuff and removing pieces that are not required. Any suggestion is more than welcome!I hope it can add some value to your projects!
Beta Was this translation helpful? Give feedback.
All reactions