Launch one specific Laravel migration (single file)

I do not want to run all the outstanding migrants on laravel 4. I have 5 migrations. Now I just want to do one migration. instead of executing: php artisan migrate I would like to perform one specific migration, for example: php artisan migrate MY_MIGRATION_TO_RUN

+60
migration laravel laravel-4
Sep 30 '13 at 19:27
source share
17 answers

It looks like you are doing it wrong.

Migrations were performed by Laravel one by one, in the order in which they were created , so it can track the execution and execution order. Thus, Laravel will be able to FREE rollback the migration package, without risking breaking your database.

Providing the user with the ability to execute them manually does not allow (for sure) to learn how to undo changes in your database.

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

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

EDIT:

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

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

2013_01_20_221554_table 

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

 2013_01_19_221554_myFirstMigration 
+49
Sep 30 '13 at 19:45
source share

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

+38
Feb 04 '14 at 16:21
source share

You can transfer the migration to other folders and run something like:

 php artisan migrate --path=/app/database/migrations/my_migrations 
+27
Feb 27 '16 at 9:59
source share

A nice little snippet to alleviate any concerns when starting the Laravel 4 php artisan migrate --pretend . This will result in SQL output that would run if you did the actual migration.

It seems that the initial 4 migrations are already running. I would suggest that when you php artisan migrate , it will only perform a new, recent migration.

Tip: make sure all of your up () and down () functions work as you expect. I like to run (), down (), up () when I run my migrations to check them out. It would be terrible if you got 5-6 transitions and realized that you cannot roll them back without the hassle, because you do not match down () with up () 100% percent.

Just my two cents! We hope that --pretend will help.

+23
Dec 24 '13 at 16:01
source share

The only way to re-migrate is dirty. You need to open your database and delete the row in the migration table that represents your migration.

Then run php artisan again.

+15
Feb 04 '14 at 15:33
source share

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

 mkdir /database/migrations/my_migrations 

And then move the specific transfer you want to run into this directory and run this command:

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

Hope this helps!

+4
Sep 04 '17 at 17:19 on
source share

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

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

This will change the packet column to 1, 2, 3, 4, etc. Add WHERE batch>=... condition there (and update the initial @a value) if you only want to influence some migrations.

After that, you can artisan migrate:rollback as many as you need, and it will go through the migrations one at a time.

+3
Aug 27 '15 at 16:23
source share

There is one simple way that I know to do this is only possible for you only on the local host

  • Modify the migration file as needed
  • open your phpMyAdmin or whatever you use to see your database table.
  • find the table you need and release it
  • Find the migration table and open it.
  • in this table, in the migration field, find the desired table name and delete its row
  • run php artisan migrate command from command line or terminal. this will only result in the migration of tables that do not exist in the migration table in the database.

This method is absolutely safe and will not make any mistakes or problems as long as it looks unprofessional, but it still works great.

luck

+2
Jul 25 '17 at 13:22
source share

If it's just for testing, here's how I do it:

In my case, I have several migrations, one of which contains App-Settings options.

While I'm testing the application, and not all migrations are already configured, I just move them to the new "future" folder. This warehouse will not be affected by the master, and it will only perform the migration that you want.

A dirty workaround, but it works ...

+1
Jul 05 '15 at 9:13
source share

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

 php artisan migrate 

You can also go back before adding a hyphen with:

 php artisan migrate: rollback 
+1
Dec 18 '15 at 16:25
source share

I have the same problem. Copy the table creation codes into the first migration file, as shown 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(); }); } 

You can also change (decrease) the batch column number in the migrations table;)

And then run php artisan migrate .

+1
Oct 13 '16 at 17:33
source share

Throw an exception in the migration if you do not want to apply it, and this will stop the entire migration process.

Using this approach, you can break down your migration group into steps.

+1
Apr 26 '17 at 5:49 on
source share

so simple...! just go to the transfer folder. move all migration files to another folder. then return all the migrations one by one to the migration folder and perform the migration for one of them (php artisan). when you paste a bad migration file into the main migration folder and start php artisan migration at the command line, there will be an error.

0
Aug 03 '16 at 6:48
source share

I used return on line 1, so previous databases are saved as they are.

 <?php use Illuminate\Support\Facades\Schema; use Illuminate\Database\Schema\Blueprint; use Illuminate\Database\Migrations\Migration; class CreateUsersTable extends Migration { /** * Run the migrations. * * @return void */ public function up() { return; // This Line Schema::create('users', function (Blueprint $table) { $table->increments('id'); $table->string('name', 50); $table->string('slug', 50)->unique(); $table->integer('role_id')->default(1); $table->string('email', 50)->unique(); $table->timestamp('email_verified_at')->nullable(); $table->string('mobile', 10)->unique(); $table->timestamp('mobile_verified_at')->nullable(); $table->text('password'); $table->integer('can_login')->default(1); $table->rememberToken(); $table->timestamps(); }); } /** * Reverse the migrations. * * @return void */ public function down() { return;// This Line Schema::dropIfExists('users'); } } 
0
Feb 23 '19 at 9:33
source share

This is a bad approach that I use. I delete the migration files other than the specific file I want to transfer, then run PHP artisan migrate after the migration is complete, I will go to my trash and recover the deleted files.

0
May 20 '19 at
source share

For everyone who is still interested in this, the Laravel 5: Laravel update has implemented the ability to run one migration file at a time (in version 5.7).

Now you can run this: php artisan migrate --path=/database/migrations/my_migration.php (as answered here )

Since Illuminate\Database\Migrations\Migrator::getMigrationFiles() now contains this code: return Str::endsWith($path, '.php')? [$path]: $this->files->glob($path.'/*_*.php'); return Str::endsWith($path, '.php')? [$path]: $this->files->glob($path.'/*_*.php'); return Str::endsWith($path, '.php')? [$path]: $this->files->glob($path.'/*_*.php'); (see source code .)




But in my case, I really wanted to run a set of migrations at the same time, and not just one or all .

So I followed the Laravel path and registered another Migrator implementation that decides which files to use:

 /** * A migrator that can run multiple specifically chosen migrations. */ class MigrationsSetEnabledMigrator extends Migrator { /** * @param Migrator $migrator */ public function __construct(Migrator $migrator) { parent::__construct($migrator->repository, $migrator->resolver, $migrator->files); // Compatibility with versions >= 5.8 if (isset($migrator->events)) { $this->events = $migrator->events; } } /** * Get all of the migration files in a given path. * * @param string|array $paths * @return array */ public function getMigrationFiles($paths) { return Collection::make($paths)->flatMap(function ($path) { return Str::endsWith($path, ']') ? $this->parseArrayOfPaths($path) : (Str::endsWith($path, '.php') ? [$path] : $this->files->glob($path . '/*_*.php')); })->filter()->sortBy(function ($file) { return $this->getMigrationName($file); })->values()->keyBy(function ($file) { return $this->getMigrationName($file); })->all(); } public function parseArrayOfPaths($path) { $prefix = explode('[', $path)[0]; $filePaths = explode('[', $path)[1]; $filePaths = rtrim($filePaths, ']'); return Collection::make(explode(',', $filePaths))->map(function ($filePath) use ($prefix) { return $prefix . $filePath; })->all(); } } 

We need to register it in the container as 'migrator' (so that it is available as $app['migrator'] ), because that's how the Migrate team accesses it when it registers itself in IoC. To do this, we put this code in the service provider (in my case, it is DatabaseServiceProvider ):

  public function register() { $this->app->extend('migrator', function ($migrator, $app) { return new MultipleSpecificMigrationsEnabledMigrator($migrator); }); // We reset the command.migrate bind, which uses the migrator - to // force refresh of the migrator instance. $this->app->instance('command.migrate', null); } 

Then you can run this:

php artisan migrate --path=[database/migrations/my_migration.php,database/migrations/another_migration.php]

Pay attention to several comma-separated migration files.

It is tested and works in Laravel 5.4 and should be compatible with Laravel 5.8.

What for?

For all who are interested: the use case updates the version of the database along with its data.

Imagine, for example, that you want to combine the street and house number of all users into a new column, let's call it street_and_house . And imagine that you want to do this on several installations in a safe and proven way - you will probably create a script for this (in my case, I create data version control commands - artisan teams).

To perform this operation, you must first load users into memory; then start the migration to remove the old columns and add new ones; then assign street_and_house=$street. " ". $house_no to each user street_and_house=$street. " ". $house_no street_and_house=$street. " ". $house_no street_and_house=$street. " ". $house_no street_and_house=$street. " ". $house_no street_and_house=$street. " ". $house_no street_and_house=$street. " ". $house_no street_and_house=$street. " ". $house_no street_and_house=$street. " ". $house_no street_and_house=$street. " ". $house_no and save users. (I am simplifying here, but you can certainly imagine other scenarios)

And I do not want to rely on the fact that I can start all migrations at any given time. Imagine that you want to update it, say, from 1.0.0 to 1.2.0, and there were several packages of such updates - performing any other migrations can damage your data, because these migrations must be processed by their own dedicated update command. Therefore, I want to run only selected known migrations with which this update knows how to work, then perform data operations, and then, possibly, execute the following data update command. (I want to be as protective as possible).

To achieve this, I need the aforementioned mechanism and define a fixed set of migrations that must be performed for such a team to work.

Note: I would prefer to use a simple decorator using the __call magic method and avoid inheritance (a similar mechanism that Laravel uses in \Illuminate\Database\Eloquent\Builder to migrate \Illuminate\Database\Query\Builder ), but MigrateCommand unfortunately, MigrateCommand requires the presence of a Migrator instance in its constructor.




One final note: I wanted to post this answer to the question " How to start a specific migration in laravel , since it is specific to Laravel 5?". But I can’t - since this question is marked as a duplicate of this (although this question is marked as Laravel 4).

0
Jun 16 '19 at 18:33
source share

You can use the solution below:

  1. create your migration.
  2. check the migration status as: php artisan migrate:status .
  3. copy the full name of the new migration and do the following: php artisan migrate:rollback --path:2018_07_13_070910_table_tests .
  4. and then do this php artisan migrate .

finally you wrap a specific table. Good luck.

0
Jul 13 '19 at 7:24
source share



All Articles