Laravel 5.4 Concrete table migration

Hi everyone, including the documentation at https://laravel.com/docs/5.4/migrations .

Is there a way to migrate a specific migration file (for 1 migration only), call right now every time there is a change. I use php artisan migrate:refresh

and all fields are reset.

+28


source to share


14 replies


You must first create one file migration

for your table, for example:

public function up()
    {
        Schema::create('test', function (Blueprint $table) {
            $table->increments('id');
            $table->string('fname',255);
            $table->string('lname',255);
            $table->rememberToken();
            $table->timestamps();
        });
    }

      



After creating a test folder in the migrations folder , then the newly created migration is moved / copied to the test folder and run under below command in your terminal / cmd like:

php artisan migrate --path=/database/migrations/test/

      

+50


source


Look at the table migrations

in your database, a list of the migration file name and batch number will appear.

Suppose you have the following structure,

id     migration                                           batch

1      2014_10_12_000000_create_users_table                  1 
2      2014_10_12_100000_create_password_resets_table        1
3      2016_09_07_103432_create_tabel_roles                  1

      

If you just want to 2016_09_07_103432_create_tabel_roles

migrate 2016_09_07_103432_create_tabel_roles

, change its migration value to 2, which is the highest of all, and then just do the following.

php artisan migrate:rollback

      



Only the table with batch value 2 will be rolled back here. Now modify this table and run the following console command.

php artisan migrate

      

The batch value in the table migrations

determines the order of migration. when you roll back migrations that are the last or most significant of the batch are carried over first, and then others. This way you can change the value in the database and then drop a specific migration file.

Although it is not always recommended to change the batch number every time due to the relationship between the table structure, we can use this case for some cases where rolling back one table does not violate the integrity of the tables.

I hope you understand.

+40


source


You have to add the path to the migration file to update only this table and run

php artisan migrate:refresh --path=/database/migrations/fileName.php

      

+11


source


Just wanted to post another solution that I think is worth mentioning.

  1. Find the line with your migration name in the migrations table and DELETE it. It should look like this: 2016_06_01_000001_create_oauth_auth_codes_table
  2. Drop your table from the database, e.g. DROP TABLE oauth_auth_codes
  3. Run php artisan migrate

It will only move the table you need and will not touch anything else

+5


source


You can only rollback:

php artisan migrate:rollback

      

https://laravel.com/docs/5.4/migrations#rolling-back-migrations

You can specify how many migrations need to be done to revert to using the "step" option:

php artisan migrate:rollback --step=1

      

Some tricks are available here:

Rolling back one specific migration in Laravel

+3


source


Drop the table and delete its entry from the migration table.

After that, you migrate again:

php artisan migrate

      

+3


source


php artisan help migrate

      

You will see an option:

--path [= PATH] Path to migration files to be executed

By the way, you can specify the root folder of the file you want to transfer:

php artisan migrate --path=/database/migrations/sample.php

      

Or you can create a new folder in migrations and then move all the migration files you want inside it:

php artisan migrate --path=/database/migrations/new_folder

      

+3


source


You need to put the file in a new directory (ex: selected) and then apply

php artisan migrate  --path=/database/migrations/selected

      

if you need a rollback:

php artisan migrate:rollback  --path=/database/migrations/selected

      

Notes:

php artisan migrate:refresh

      

this is rolling back and then migrating all migration files in the default directory (/ database / migrations)

+3


source


Or you can simply delete the name of the migration file from your database, into the "migrations" table, and then run: php artitsan migration

+2


source


You can try using the -path = parameter to define the specific subfolder you want to run and place specific migrations there.

Alternatively, you will need to remove the link and tables from the DB tables and migration tables which are not ideal: /

+1


source


If you want to create another table, just create a new migration file. It will work.

If you create a transfer named users_table

with id, first_name, last_name

. You can create a migration file like

    public function up()
    {
        Schema::create('users', function (Blueprint $table) {
            $table->increments('id');
            $table->string('first_name',255);
            $table->string('last_name',255);
            $table->rememberToken();
            $table->timestamps();
        });
    }
    public function down()
    {
        Schema::dropIfExists('users');
    }

      

If you want to add another filed as "status" without migration: update. You can create another migration file like "add_status_filed_to_users_table"

public function up()
{
    Schema::table('users', function($table) {
        $table->integer('status');
    });
} 

      

And don't forget to add the rollback option:

public function down()
{
    Schema::table('users', function($table) {
        $table->dropColumn('status');
    });
}

      

And when you run migrate with php artitsan migration

, it just migrates the new migration file.

But if you add "status" to the first mgration file (users_table) and run the migration. Nothing to migrate. You need to run php artisan migrate:refresh

.

Hope for this help.

+1


source


Correction- remove slash in front of database

$ php artisan migrate --path=database/migrations/migration.php

      

+1


source


0


source


The easiest way is to drop tables on PHP MyAdmin or Mongodb and then run php artisan migrate

0


source







All Articles