Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I run migrations for a specific environment in laravel

Tags:

php

laravel

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

like image 514
duellsy Avatar asked Nov 21 '12 10:11

duellsy


People also ask

How do I run a specific Laravel migration?

To run the specific migration in Laravel, you need to use --path option with the php artisan migrate command. Let's take a simple example, we have '2019_12_04_131405_create_payments_table. php' migration in the database/migrations directory and we would like to run this migration.

How do I change environment in Laravel?

php has been deprecated since Laravel 5. Now, in L5, we have single . env file, where you store all your environment configuration. To define your environment, you should put APP_ENV=local to this file.

What is migrate command in Laravel?

Migrations are like version control for your database, allowing a team to easily modify and share the application's database schema. Migrations are typically paired with Laravel's schema builder to easily build your application's database schema.

How do I refresh a particular migration in Laravel?

If you tried to run migration#2 without every having run #1 then it would fail. When you run artisan migrate then it will only run migrations that haven't already been applied unless you use migrate:refresh in which case it will reset and re-run all migrations.


Video Answer


2 Answers

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] 
like image 154
Jason Lewis Avatar answered Sep 20 '22 22:09

Jason Lewis


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 :)

like image 26
hungneox Avatar answered Sep 22 '22 22:09

hungneox