Andrew
Andrew

Reputation: 20081

Mark migration as run in laravel

My migration failed, so I just ran a query manually in MySQL Workbench.

Is there an artisan command to mark a migration as complete? So that when I run future migrations, it skips the ones I know don't need to be run.

I can list them and their status with this command and see which ones have run and not:

php artisan migrate:status 

I can manually insert an entry into the database migrations table probably if no command. ( but not with a migration at the moment :P )

I suppose I could also... delete the migration.

Upvotes: 13

Views: 7219

Answers (4)

Fer
Fer

Reputation: 3347

You wrote that you run the migration's sql directly in the database, so I assume that for you running another 2 statements would be fine too.

With laravel 5.7 (and probably some lower versions) you need to populate the migration information in the database (in the migrations table).

The information you need is the migration (the filename without the .php extension) and the batch number (to decide which migrations to run at once when rollbacking a migration for instance)

First you have to see wich the last batch number is:

SELECT MAX(batch) from migrations;

Let's say the above query returns 42, then we have to insert the data with batch number 42 + 1 = 43

Assuming that the migration that you want to mark as run is database/migrations/2019_01_29_091850_update_jobs_table_for_laravel_5_3.php,

to populate the information you need to run:

-- note that you should not include the .php extension
INSERT INTO migrations VALUES ("2019_01_29_091850_update_jobs_table_for_laravel_5_3", 43);

Then running php artisan migrate:status will report the migration as run.

May be you can accomplish the task with one query... I'll leave that task open for the comments from the sql experts.

Upvotes: 9

pilat
pilat

Reputation: 1162

I've just did the following:

  1. Commented out the content of YourMigrationClass::up() method. Like this:

    class AddSessionIdToActivitiesTable extends Migration
    {
    
        public function up()
        {
            /* commenting this out:
            Schema::table('activities', function (Blueprint $table) {
                $table->integer('session_id')->nullable();
            });
            */
        }
    
  2. Run: php artisan migrate

  3. Check it's marked as "Ran?": php artisan migrate:status

  4. (optional) Uncomment the method's content for it to serve as documentation in your VCS.

Upvotes: 20

Tim
Tim

Reputation: 456

You can always create your own artisan commands.

Upvotes: 1

Aken Roberts
Aken Roberts

Reputation: 13467

At present (Laravel <= 5.6), There is no command to update the database to mark a migration as run. You'll just have to do so manually.

Upvotes: 1

Related Questions