Running one specific laravel 4 migration (single f

2019-01-13 19:44发布

问题:

I don't want to run All Outstanding Migrations on laravel 4. I have 5 migrations. Now I just want to run one migration. instead of doing : php artisan migrate I would like to run one specific migration like : php artisan migrate MY_MIGRATION_TO_RUN

回答1:

Looks like you're doing it wrong.

Migrations were made to be executed by Laravel one by one, in the exact order they were created, so it can keep track of execution and order of execution. That way Laravel will be able to SAFELY rollback a batch of migrations, without risking breaking your database.

Giving the user the power to execute them manually, make it impossible to know (for sure) how to rollback changes in your database.

If your really need to execute something in your database, you better create a DDL script and manually execute it on your webserver.

Or just create a new migration and execute it using artisan.

EDIT:

If you need to run it first, you need to create it first.

If you just need to reorder them, rename the file to be the first. Migrations are created with a timestemp:

2013_01_20_221554_table

To create a new migration before this one you can name it

2013_01_19_221554_myFirstMigration


回答2:

Just move the already run migrations out of the app/config/database/migrations/ folder . Then run the command php artisan migrate . Worked like a charm for me .



回答3:

A nice little snippet to ease any fears when running Laravel 4 migrations php artisan migrate --pretend . This will only output the SQL that would have been run if you ran the actual migration.

It sounds like your initial 4 migrations have already been run. I would guess that when you php artisan migrate it will only run the new, recent migration.

Word of advice: makes sure all of your up() and down() work how you expect them to. I like to run up(), down(), up() when I run my migrations, just to test them. It would be awful for you to get 5-6 migrations in and realize you can't roll them back without hassle, because you didn't match the down() with the up() 100% percent.

Just my two cents! Hope the --pretend helps.



回答4:

You can put migrations in more folders and run something like:

php artisan migrate --path=/app/database/migrations/my_migrations


回答5:

The only way to re run a migrattion is a dirty one. You need to open your database and delete the line in the migrations table that represents your migration.

Then run php artisan migrate again.



回答6:

You can create a separate directory for your migrations from your terminal as follows:

mkdir /database/migrations/my_migrations

And then move the specific migration you want to run to that directory and run this command:

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

Hope this helps!



回答7:

I gave this answer on another post, but you can do this: run artisan migrate to run all the migrations, then the following SQL commands to update the migrations table, making it look like the migrations were run one at a time:

SET @a = 0;  
UPDATE migrations SET batch = @a:=@a+1;

That will change the batch column to 1, 2, 3, 4 .. etc. Add a WHERE batch>=... condition on there (and update the initial value of @a) if you only want to affect certain migrations.

After this, you can artisan migrate:rollback as much as is required, and it'll step through the migrations one at a time.



回答8:

There is one easy way I know to do this can only be available for you on just local host

  1. Modify your migration file as needed
  2. open your phpMyAdmin or whatever you use to see your database table
  3. find the desired table and drop it
  4. find migrations table and open it
  5. in this table under migration field find your desired table name and delete its row
  6. finally run the command php artisan migrate from your command line or terminal. this will only migrate that tables which not exists in the migrations table in database.

This way is completely safe and will not make any errors or problems while it looks like un-professional way but it still works perfectly.

good luck



回答9:

If it's just for testing purposes, this is how i do it:

For my case, i have several migrations, one of them contains App-Settings.

While I'm testing the App and not all of the migrations are already setup i simply move them into a new folder "future". This folde won't be touched by artisan and it will only execute the migration you want.

Dirty workaround, but it works...



回答10:

If you want to run your latest migration file you would do the following:

php artisan migrate

You can also revert back to before you added the migration with:

php artisan migrate: rollback


回答11:

I have same problem. Copy table creation codes in first migration file something like below:

  public function up()
    {
        Schema::create('posts', function(Blueprint $table){
            $table->increments('id');
            // Other columns...
            $table->timestamps();
        });
        Schema::create('users', function (Blueprint $table) {
            $table->increments('id');
            // Other columns...
            $table->softDeletes()->nullable();
        });
    }

Also you can change(decrease) batch column number in migrations table ;)

And then run php artisan migrate.



回答12:

Throw an exception in a migration, if you don't want to apply it, and it would stop the whole process of migration.

Using this approach you can split your bunch of migration into steps.



回答13:

so simple...! just go to your migration folder. move all migration files into another folder. then return all migration one by one into migration folder and run migration for one of them(php artisan). when you insert bad migration file into master migration folder and run php artisan migrate in command prompt will be error.