0

When working on a Laravel project I often switch between my local MySQL database (which has minimal seeded data) and an external staging database (filled with representative, production-like data). I switch by commenting out the connection details in my .env file.

For my local database, I often run php artisan migrate:refresh --seed to test new models and relations. But I never want to run this command (by accident, when I forget to switch to my local db) on the staging database.

Is there a pre-execution function (or something similar) for the migrate command I can implement to first check if my DB_HOST has some value, and stop the execution if I detect that the staging environment is chosen?

Note: Disabling write permissions for my user on the staging database is not an option since I test inserts/deletes on that database.

Daan
  • 3
  • 1
  • Check this https://stackoverflow.com/questions/21810251/can-one-modify-the-templates-created-by-artisan-migrate-command – LordF Aug 12 '21 at 08:31

1 Answers1

3

Laravel already has a sanity check on the migrate Artisan command based on the environment used.

If you set your APP_ENV to production, then it will throw a warning when you try to run the migrate command without the --force flag, and will inform you that the application is in production and ask for confirmation that you wish to run this command.

In your situation I'd also avoid working with the "dummy" data you're using locally since that won't be representative of the production data. I'd take a copy of the staging database and use that locally since it's more representative of what you're actually working with in production, and it sidesteps the issue you're having by removing the need to work with a separate database. As it is, the approach you're using is inherently risky.

Matthew Daly
  • 9,212
  • 2
  • 42
  • 83
  • Thanks. I will accept this answer since it is a limit of Laravel itself. However, switching to `APP_ENV = production` when switching to the staging database would not be ideal since you lose the logic specifically build for development purposes. – Daan Aug 12 '21 at 18:03