26

I'm setting up a new app with laravel (Laravel 4), and having some issues setting up the database via migrations.

I made a migration file with:

artisan migrate:make --table="jobs" --create jobs

Which created a file in database/migrations as expected, I made some mods to this, and went to fire it up using

artisan migrate --env=local

But I'm getting the reply of "Nothing to migrate"

If I try run without --env=local, it uses the database.php in the config folder (not in the local / staging / production folder) which we don't want to use, as it won't be environment specific.

My first thought was OK, maybe I need to put the env flag on the migrate:make call, so I tried that, but got an error saying it couldn't create the migration file. Then I thought it doesn't make sense to make env based migrations anyway... they should be created generic, and simply run on a per env basis, so in the end, all environments are using the same migration scripts.

So I'm a bit stuck now on where to go to from here

duellsy
  • 8,497
  • 2
  • 36
  • 60
  • Depending on Laravel version, see this bug: https://github.com/laravel/framework/issues/12944#issuecomment-214751735 – Ryan Aug 31 '19 at 17:57

4 Answers4

34

You need to specify the environment before the migrate command.

artisan --env=local migrate

Running artisan help shows you the format in which commands are to follow.

artisan help

Usage:
  [options] command [arguments]
Jason Lewis
  • 18,537
  • 4
  • 61
  • 64
  • 5
    sadly, this had no affect... still getting the `Nothing to migrate` message – duellsy Nov 21 '12 at 11:07
  • Well, according to the help that's how you specify the options. Take a look at `artisan help` and you'll see it's in the format of `[options] command [arguments]`. My advice now is to make sure your environments are all setup correctly. I just checked, and it works fine for me. So you're missing something else. – Jason Lewis Nov 21 '12 at 11:36
  • Unfortunately this is totally wrong (at least for Laravel versions I'm familiar with, such as 5.2+). You may have even called the wrong help file. `php artisan help migrate` says "Usage: `migrate [options]`" and gives options examples such as `--database[=DATABASE]` and `--env[=ENV]`. – Ryan Aug 31 '19 at 13:15
  • You should add options after artisan command. For example, I want to drop database and start migration from beginning in my testing environment : `php artisan migrate:fresh --env=testing`. Tested on Laravel 5.5. – ibnɘꟻ Jul 06 '20 at 05:04
5

I figured out a solution to run migrate for different databases. Basically, the command artisan migrate --env=local doesn't work. But we can define a new connection string in config\database.php. For example:

<?php
   'mysql_testing' => [
        'driver'    => 'mysql',
        'host'      => env('DB_TESTING_HOST'),
        'database'  => env('DB_TESTING_DATABASE'),
        'username'  => env('DB_TESTING_USERNAME'),
        'password'  => env('DB_TESTING_PASSWORD'),
        'charset'   => 'utf8',
        'collation' => 'utf8_unicode_ci',
        'prefix'    => 'prefix_',
    ],

And specify the --database when we run artisan migrate like this:

php artisan migrate --database=mysql_testing

Hope this helps :)

hungneox
  • 9,333
  • 12
  • 49
  • 66
3

Resolved.

Solution was simply to edit the local/database.php (or production/database.php etc), making sure that the migrations path variable is pointing to the location that migrate:make is creating the migration files, just change

'application' => __DIR__.'/../database/migrations',

to

'application' => DIR.'/../../database/migrations',
duellsy
  • 8,497
  • 2
  • 36
  • 60
1

If you modify a migration after running it, you first need to rollback the migration.

php artisan migrate:rollback

Keep running that until the migration you've changed is rolled back. Alternatively, you can reset your entire schema with

php artisan migrate:reset

But you will then need to call your migrations like normal to bring them up to date.

php artisan migrate

Finally you can reset and then migrate by calling

php artisan rebuild

Also note that it is generally bad practice to modify your migrations after they have been made, unless you literally just made it. Once it is deployed you should not modify it, and instead create a new migration file.

Hope this helps.

Edit: I somehow missed the Laravel 4 indicator. Most of these commands still work I believe, but you may need to adjust.

William Cahill-Manley
  • 2,395
  • 19
  • 18
  • Sorry, when I said "made some mods" I just meant I opened the migration file that was just generated, and added in my migration code. I wasn't editing an already run migration. This is the first ever migration for this project – duellsy Nov 21 '12 at 11:10
  • it is also possible to tear down and rebuild migrations in one commando using php artisan migrate:refresh – Atheist Apr 17 '15 at 22:16