Mark migration as run in laravel
Asked Answered
A

4

13

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.

Abranchiate answered 24/2, 2018 at 2:7 Comment(0)
D
19

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.

Descartes answered 17/10, 2018 at 19:4 Comment(0)
J
9

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.

Jac answered 29/1, 2019 at 10:21 Comment(1)
A tip: you'll want to ensure that the timestamp portion of the date that's prepended to your migration name exactly matches what your migration:status shows. I made the mistake of simply using the time I saw on my migration file, which was something like 2021_07_29_150329 (prepended to "_my_migration_name"). But I kept getting status of "not run". I saw that "migration:status" was showing 2021_07_29_183341 so when I updated my table entry, the migration status then showed as "run" correctly. Apparently even that part of the name matters.Lashondra
C
1

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.

Cardiovascular answered 24/2, 2018 at 2:16 Comment(0)
P
1

You can always create your own artisan commands.

Pummel answered 24/2, 2018 at 10:44 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.