Add ability for automatic database seeding #276
Replies: 3 comments 4 replies
-
@danpastori: What are your thoughts on the proposal above? Does Laravel handle running seeding a database multiple times like migrations (where it's smart enough to not run things twice)? |
Beta Was this translation helpful? Give feedback.
-
@jaydrogers I could see this being helpful through an environment variable that's defaulted to false. Laravel will run things twice so you will have to account for that some way. Say if you seed initially, push an update, it will run again. I tend to manually seed through an artisan command so I have full control. If there's a way to run only once, I think that's practical, then handle the rest manually. |
Beta Was this translation helpful? Give feedback.
-
The problem of seeding is the environment and the location. Seeding should never happen on production environment, so there should be a check for that like On SQLite, you cannot seed before migrating, where latest Laravel creates the SQLite database file. On older Laravel version, creating the file must be done manually. In my opinion: Laravel seeding should run on the container. This is because two reasons:
By this, then we should have access to configure the env variables like so:
To give some magic to the container, the |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
I'd like the ability to automatically seed the database inside the Laravel Autorun Script.
Usecase
I have a Seeder that adds a default user to the database. I'd want to be able to run this anytime the application starts so there is a user to start with and my users can't get locked out. This seeder is written idempotently so it doesn't matter how often it executes. However this could be a problem for other seeders/applications where you only want your database to be seeded once.
Suggestion
Add two environment variables (suck at naming):
AUTORUN_LARAVEL_SEEDING
AUTORUN_LARAVEL_SEEDING_ALWAYS
If
AUTORUN_LARAVEL_SEEDING
is enabled the database will be seeded just once since this seems to be the default behaviour. This could be realized with the simple creation of a file but there has to be a better way.If
AUTORUN_LARAVEL_SEEDING_ALWAYS
is enabled the database will be seeded on every run of the script.I'd love some input on how to handle the "only run once" behaviour :)
Beta Was this translation helpful? Give feedback.
All reactions