0

I want to create a console command that help me through a site deployment by calling other Artisan commands (a migration, followed by a series of functions that will fill the table with data.)

    Artisan::call('migrate', ['--path' => 'database/migrations/'.$filename.'.php']);
    foreach($data as $entry){
       // Fill the table with data
   }

Everything has been tested and works fine in my dev environment, however the migration command appears to be skipped when I try to run this in production. I tried doing the migration manually, and discovered that the console gives me a prompt asking if I'm sure that I want to run the migration in the production environment. Is there a way to modify the Artisan::call('migrate' ...); code so it will answer 'yes' to this prompt during execution? If not, is there another way to get the script to work (possibly setting the env variable to something else while the script is running?)

Edit: Can I fix this at the unix command level (I don't have access to the production environment to test)?

yes | php atrisan command:dothis

This may be the work around I need, however I'd rather be more specific, and provide the yes to the single internal atrisan call

Hoyt Jolly
  • 11
  • 3
  • Force start whould do the trick `Artisan::call('migrate', ['--path' => 'database/migrations/'.$filename.'.php', '--force' => true]);` – InDevX Apr 19 '23 at 18:43

1 Answers1

0

If you do php artisan migrate --help, it shows you the list of options:

$ art migrate --help
Description:
  Run the database migrations

Usage:
  migrate [options]

Options:
      --database[=DATABASE]        The database connection to use
      --force                      Force the operation to run when in production
      --path[=PATH]                The path(s) to the migrations files to be executed (multiple values allowed)
      --realpath                   Indicate any provided migration file paths are pre-resolved absolute paths
      --schema-path[=SCHEMA-PATH]  The path to a schema dump file
      --pretend                    Dump the SQL queries that would be run
      --seed                       Indicates if the seed task should be re-run
      --seeder[=SEEDER]            The class name of the root seeder
      --step                       Force the migrations to be run so they can be rolled back individually
      --isolated[=ISOLATED]        Do not run the command if another instance of the command is already running [default: false]
  -h, --help                       Display help for the given command. When no command is given display help for the list command
  -q, --quiet                      Do not output any message
  -V, --version                    Display this application version
      --ansi|--no-ansi             Force (or disable --no-ansi) ANSI output
  -n, --no-interaction             Do not ask any interactive question
      --env[=ENV]                  The environment the command should run under
  -v|vv|vvv, --verbose             Increase the verbosity of messages: 1 for normal output, 2 for more verbose output and 3 for debug

So you need to pass --force in with your call

Artisan::call('migrate', ['--path' => 'database/migrations/'.$filename.'.php', '--force']);
aynber
  • 22,380
  • 8
  • 50
  • 63
  • Thanks! I'm pretty sure that will fix my problem. I'll be able to test it in a few days. Does the command need a " => true" or will it run without one? – Hoyt Jolly Apr 19 '23 at 19:09
  • I think it should run without it, but you can try it both ways. – aynber Apr 19 '23 at 19:59