Programing

Laravel에서 특정 마이그레이션 하나를 롤백

lottogame 2020. 5. 12. 07:54
반응형

Laravel에서 특정 마이그레이션 하나를 롤백


내가 원하는

롤백 만하려면 :

Rolled back: 2015_05_15_195423_alter_table_web_directories


난 달린다

php artisan migrate:rollback, 내 마이그레이션 중 3 개가 롤백됩니다.

Rolled back: 2015_05_15_195423_alter_table_web_directories
Rolled back: 2015_05_13_135240_create_web_directories_table
Rolled back: 2015_05_13_134411_create_contacts_table

삭제

web_directoriescontacts의도 와 내 테이블 모두 나는 그런 일이 일어나기를 원하지 않으며, 그 특정한 것만 롤백 할 수 있다면이 재난은 결코 일어나지 않을 것입니다.


migrations를 살펴보면 각 마이그레이션에 배치 번호가 있음을 알 수 있습니다. 따라서 롤백 할 때 마지막 배치의 일부인 각 마이그레이션을 롤백합니다.

마지막 마이그레이션 만 롤백하려면 배치 번호를 1 씩 늘리십시오. 그런 다음 다음에 rollback명령 을 실행할 때 자체 "일괄 처리"에있는 한 번의 마이그레이션 만 롤백합니다.


라 라벨 5.3+

한 단계 롤백. 기본적으로

php artisan migrate:rollback --step=1

다음은 매뉴얼 페이지입니다. docs .


라 라벨 5.2 이전

번거 로움 없이는 할 수 없습니다. 자세한 내용은 Martin Bean의 답변을 확인하십시오 .


롤백 할 때마다 마지막 마이그레이션 배치를 얻습니다. 명령을 사용

php artisan migrate:rollback --step=1

@Martin Bean이 말한 것을 할 수 없다면 다른 트릭을 시도해보십시오.

새 마이그레이션을 작성하고 up () 메소드에서 해당 파일에 롤백하려는 마이그레이션의 down () 메소드에있는 내용을 삽입하고 down () 메소드에있는 up () 메소드에 내용을 삽입하십시오.

예를 들어 원래 마이그레이션이 다음과 같은 경우

public function up()
{
    Schema::create('users', function(Blueprint $table)
    {
        $table->increments('id')->unsigned();
        $table->string('name');
    });
}
public function down()
{
    Schema::drop('users');
}

그런 다음 새 마이그레이션 파일 에서이 작업을 수행하십시오.

public function up()
{
    Schema::drop('users');
}
public function down()
{
    Schema::create('users', function(Blueprint $table)
    {
        $table->increments('id')->unsigned();
        $table->string('name');
    });
}

그런 다음 마이그레이션을 실행하면 테이블이 삭제됩니다. 다시 되돌리려면 롤백하십시오.


이 질문에 대답하는 데 약간 늦을 수 있지만 여기에 내가 생각하는 아주 좋고 깨끗하며 효율적인 방법이 있습니다. 최대한 철저하게 노력하겠습니다.

마이그레이션을 작성하기 전에 다음과 같이 다른 디렉토리를 작성하십시오.

    database
       | 
       migrations
            |
            batch_1
            batch_2
            batch_3

Then, when creating your migrations run the following command (using your tables as an example):

     php artisan make:migration alter_table_web_directories --path=database/migrations/batch_1

or

     php artisan make:migration alter_table_web_directories --path=database/migrations/batch_2

or

     php artisan make:migration alter_table_web_directories --path=database/migrations/batch_3

The commands above will make the migration file within the given directory path. Then you can simply run the following command to migrate your files via their assigned directories.

    php artisan migrate alter_table_web_directories --path=database/migrations/batch_1

*Note: You can change batch_1 to batch_2 or batch_3 or whatever folder name you're storing the migration files in. As long as it remains within the database/migrations directory or some specified directory.

Next if you need to rollback your specific migrations you can rollback by batch as shown below:

    php artisan migrate:rollback --step=1
                    or try
php artisan migrate:rollback alter_table_web_directories --path=database/migrations/batch_1

or

    php artisan migrate:rollback --step=2
                    or try
php artisan migrate:rollback alter_table_web_directories --path=database/migrations/batch_2

or

    php artisan migrate:rollback --step=3
                    or try
php artisan migrate:rollback alter_table_web_directories --path=database/migrations/batch_3

Using these techniques will allow you more flexibility and control over your database(s) and any modifications made to your schema.


better to used refresh migrate

You may rollback & re-migrate a limited number of migrations by providing the step option to the refresh command. For example, the following command will rollback & re-migrate the last two migrations:

php artisan migrate:refresh --step=2

otherwise used rollback migrate

You may rollback a limited number of migrations by providing the step option to the rollback command. For example, the following command will rollback the last three migrations:

php artisan migrate:rollback --step=3

for more detail about migration see


Rollback one step. Natively.

php artisan migrate:rollback --step=1

Rollback two step. Natively.

php artisan migrate:rollback --step=2

Use command "php artisan migrate:rollback --step=1" to rollback migration to 1 step back.

For more info check the link :- https://laravel.com/docs/master/migrations#running-migrations


Migrate tables one by one.

Change the batch number of the migration you want to rollback to the highest.

Run migrate:rollback.

May not be the most comfortable way to deal with larger projects.


If you want modify original migration file and migrate it again, you can use this package to migrate. (Applicable to Laravel 5.4 or later)

First, install package in your Laravel project:

composer require caloskao/migrate-specific

Register command at app/Console/Kernel.php :

protected $commands = [
    \CalosKao\MigrateSpecific::class
];

Now, run this command to migrate your file

php artisan migrate:specific database/migrations/table.php

INSERT INTO homestead.bb_migrations (`migration`, `batch`)  VALUES ('2016_01_21_064436_create_victory_point_balance_table', '2')

something like this


1.) Inside the database, head to the migrations table and delete the entry of the migration related to the table you want to drop.

Migration table image example

2.) Next, delete the table related to the migration you just deleted from instruction 1.

Delete table image example

3.) Finally, do the changes you want to the migration file of the table you deleted from instruction no. 2 then run php artisan migrate to migrate the table again.


As stated in the Laravel manual, you may roll back specific number of migrations using the --step option

php artisan migrate:rollback --step=5

Best way is to create a new migration and make required changes in that.

Worst case workaround (if you have access to DB plus you are okay with a RESET of that table's data):

  1. Go to DB and delete/rename the migration entry for your-specific-migration
  2. Drop the table created by your-specific-migration
  3. Run php artisan migrate --path=/database/migrations/your-specific-migration.php

This will force laravel to run that specific migration as no entry about it exists in Laravel's migration history

참고URL : https://stackoverflow.com/questions/30287896/rollback-one-specific-migration-in-laravel

반응형