Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 221
Andrew Avatar asked Feb 24 '18 02:02

Andrew


3 Answers

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.

like image 169
pilat Avatar answered Sep 20 '22 12:09

pilat


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.

like image 41
Fer Avatar answered Sep 19 '22 12:09

Fer


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.

like image 34
Aken Roberts Avatar answered Sep 16 '22 12:09

Aken Roberts