Run a Single Migration File

Run a single migration file

You can just run the code directly out of the ruby file:

rails console
>> require "db/migrate/20090408054532_add_foos.rb"
>> AddFoos.new.up

Note: Very old versions of rails may require AddFoos.up rather than AddFoos.new.up.

An alternative way (without IRB) which relies on the fact that require returns an array of class names:

script/runner 'require("db/migrate/20090408054532_add_foos.rb").first.constantize.up'

Note that if you do this, it won't update the schema_migrations table, but it seems like that's what you want anyway.

Additionally, if it can't find the file you may need to use require("./db/..." or try require_relative depending on your working directory

How can I run specific migration in laravel

TLDR;

"By the book":

If there are already migrated tables and there is some data stored in those tables, be careful with php artisan migrate:refresh. You will lose all your data!

For this specific question OP has already run the migration and by the book if he wants to run the same migration again, then first he should rollback with php artisan migrate:rollback. This will undo the last migration/s.

Then you can run php artisan migrate and all NOT migrated migrations will be migrated.


If you created more migrations and they are not migrated yet, to run only a specific migration use this:

php artisan migrate --path=/database/migrations/full_migration_file_name_migration.php

And sometimes if there is something messed up and you get errors on migrate, saying that the table already exists you can manually delete that specific entry from migrations AND the table which causes the problem in your DB and run php artisan:migrate to recreate the table.

Codeigniter 4 - How to run one specific migration file?

You can migrate a single file regardless of order or batches using the method force(string $path, string $namespace, ?string $group = null) available within the MigrationRunner class.

force($path, $namespace, $group)

This forces a single file to migrate regardless of order or batches.
Method “up” or “down” is detected based on whether it has already been
migrated.

Note:

This method is recommended only for testing and could cause data
consistency issues.

So in your case, you would just run a for loop through the files as you pass the expected parameters in the force(...) method. I.e:

Assuming the array keys of your $files array represent the various 'modules' of the project:

$migrate = \Config\Services::migrations();
$psr4 = config(\Config\Autoload::class)->psr4;

foreach ($files as $module => $filenames) {

$namespace = "Modules\\" . ucwords($module);

foreach ($filenames as $filename) {
try {
$migrate->force($psr4[$namespace] . "\\" . $filename, $namespace);
} catch (\Throwable $e) {
// Do something with the error here...
}
}
}

NOTES:

The above solution assumes that you already mapped your $psr4 namespaces in your application modules to their respective locations on the file system: I.e:

File: app/Config/Autoload.php

// ...

public $psr4 = [
APP_NAMESPACE => APPPATH, // For custom app namespace
'Config' => APPPATH . 'Config',
'Modules\Blog' => ROOTPATH . 'module/blog',
'Modules\Storage' => ROOTPATH . 'module/storage',
'Modules\Sales' => ROOTPATH . 'module/sales',
];

// ...

Laravel Migrate Specific File(s) from Migrations

First you should create one migration file for your table like:

public function up()
{
Schema::create('test', function (Blueprint $table) {
$table->increments('id');
$table->string('fname',255);
$table->string('lname',255);
$table->rememberToken();
$table->timestamps();
});
}

After create test folder in migrations folder then newly created migration moved/copied in test folder and run below command in your terminal/cmd like:

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

How to run a specific migration of an accidentally dropped table

I got it to work doing the following:

  • On MySql command line: create table my_table_name (hello text);
  • In terminal: knex migrate:down name_of_migration_file.js
  • In terminal: knex migrate:up name_of_migration_file.js

rails run specific migration

rake db:migrate:redo VERSION=my_version

Or you can go up or down from a specific version:

db:migrate:up VERSION=my_version
db:migrate:down VERSION=my_version

Yii2 run only specific migration

Run migrate/history to list migrations have been applied:

./yii migrate/history 

Copy the name of migration you want to return to later (lets say it is 'm160101_185401_initial_migration'). Save it somewhere because you're going to need this later.

Mark migration history at the one just before the one you need to run:

./yii migrate/mark m170101_185401_create_news_table

Run one migration:

./yii migrate 1

Reset migration history:

./yii migrate/mark m160101_185401_initial_migration


Related Topics



Leave a reply



Submit