Laravel 5.4 Specific Table Migration


Laravel 5.4 Specific Table Migration



Hi read all the included documentation here in https://laravel.com/docs/5.4/migrations.



Is there a way on how to migrate a certain migration file (1 migration only), cause right now every time there is a change I use php artisan migrate:refresh and all fields are getting reset.


php artisan migrate:refresh




7 Answers
7



Just look at the migrations table in your database, there will be a list of migration file name and batch number value.


migrations



Suppose you have 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 want to just rollback 2016_09_07_103432_create_tabel_roles migration,
change it's migration batch value to 2 which is highest among all and then just execute following.


2016_09_07_103432_create_tabel_roles


php artisan migrate:rollback



Here only table with batch value 2 will be rolled back. Now make changes to that table and run following console command.


php artisan migrate



Batch value in the migrations table defines order of the migrations. when you rollback, migrations that are latest or have highest batch value are rolled back at first and then others. So, you can change the value in database and then rollback a particular migration file.


migrations



Although it's not a good idea to change batch number every time because of relationship among the table structure, we can use this case for some cases where single table rollback doesn't violates the integrity among the tables.



Hope you understand.





Thanks for this, it clears up things. @sagar gautam
– Martney Acha
Aug 4 '17 at 2:00






@MartneyAcha I'm Happy that you've got solution to your problem Cheers !!
– Sagar Gautam
Aug 4 '17 at 2:14



First you should create one migration file for your table like:


migration


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 create test folder in migrations folder then newly created migration moved/copied in test folder and run below command in your terminal/cmd like:


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



Hope my answer is work for you !!!





Thanks. This is exactly what I was looking for.
– sadiq
Jan 23 at 12:41





php artisan migrate --path=database/migrations/test/ worked for me
– Joyal
Mar 23 at 11:08



Delete the table and remove its record from migration table.



After that you just run migration again:


php artisan migrate



You can only rollback:


php artisan migrate:rollback



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



You can specify how many migrations to roll back to using the 'step' option:


php artisan migrate:rollback --step=1



Some tricks are available here:



Rollback one specific migration in Laravel



You could try to use the --path= option to define the specific sub-folder you're wanting to execute and place specific migrations in there.



Alternatively you would need to remove reference and tables from the DB and migrations tables which isn't ideal :/



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



If you create an migration named users_table with id, first_name, last_name. You can create an migration file like


users_table


id, first_name, last_name


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 like "status" without migrate:refresh. 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 migrate the new migration file.


php artitsan migration



But if you add filed "status" into the first mgration file (users_table) and run migration. It's nothing to migrate. You need to run php artisan migrate:refresh.


php artisan migrate:refresh



Hope this help.



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






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Comments

Popular posts from this blog

paramiko-expect timeout is happening after executing the command

how to run turtle graphics in Colaboratory

Export result set on Dbeaver to CSV