📜 ⬆️ ⬇️

We use Yii2 migrations for working with multiple databases

Migrations are a convenient tool for changing the structure of the database and keeping it up to date.

Yii2 supports migrations out of the box. The use of migrations is described in detail in the documentation . Manage migrations from the command line.

The article describes the use of migrations to manage multiple database schemas. As it turned out, it is quite easy to implement, but for some reason did not find a single description.
Often I meet projects where more than one database is used. In this case, I want to manage each base separately. Alternatively, you can specify in each migration which database to apply it to, or explicitly indicate the database in each request. But this is not very convenient and there is a high probability that the migration to the wrong base will happen by chance. In addition, the migration table will still be only in the main database.
')
Unfortunately, I did not find a ready-made solution, so let's look at the framework code and see how the migrations work.

The yii migrate command uses yii \ console \ controllers \ MigrateController

To create a new migration, use the migration.php template.

All migrations are inherited from yii \ db \ Migration .

1. To begin with, we redefine the base migration class for each database. In the components folder, create the classes CustomMigrationDb and CustomMigrationDb2

CustomMigrationDb2.php
<?php namespace app\components; use yii\db\Migration; class CustomMigrationDb2 extends Migration{ public function init() { $this->db = 'db2'; parent::init(); } } 


2. In the \ views \ migrations folder we create the files migration_db.php and migration_db2.php - templates for new migrations. Just copy the base template and change the base class to the ones we just created.

migration_db2.php

 <?php /** * This view is used by console/controllers/MigrateController.php * The following variables are available in this view: */ /* @var $className string the new migration class name */ echo "<?php\n"; ?> use yii\db\Schema; use app\components\CustomMigrationDb2; class <?= $className ?> extends CustomMigrationDb2 { public function up() { } public function down() { echo "<?= $className ?> cannot be reverted.\n"; return false; } /* // Use safeUp/safeDown to run migration code within a transaction public function safeUp() { } public function safeDown() { } */ } 


3. Now we create controllers. In the commands folder, create the MigrateDbController and MigrateDb2Controller classes. We inherit them from yii \ console \ controller \ MigrateController.

MigrateDb2Controller.php
 <?php namespace app\commands; use yii\console\controllers\MigrateController; class MigrateDb2Controller extends MigrateController { public $db = 'db2'; public $templateFile = '@app/views/migrations/migration_db2.php'; public $migrationPath = '@app/migrations/db2'; } 


Here we specify the database in which the table with the migrations will be created, the template for creating new migrations and the path to the folder with the migrations.

4. Disable the standard yii migrate, instead we will use one of the created controllers. To do this, add the following code to the file \ config \ console.php:

 'controllerMap' => [ ... 'migrate' => [ 'class' => 'app\commands\MigrateDbController', ], ], 


UPD: zvirusz offers all the migration controllers to render in the controllerMap. To do this, add this code to \ config \ console.php.


 'controllerMap' => [ ... 'migrate' => [ // Fixture generation command line. 'class' => 'yii\console\controllers\MigrateController', 'db' => 'db', 'templateFile' => '@app/views/migrations/migration_db.php', 'migrationPath' => '@app/migrations/db' ], 'migrate-db' => [ // Fixture generation command line. 'class' => 'yii\console\controllers\MigrateController', 'db' => 'db', 'templateFile' => '@app/views/migrations/migration_db.php', 'migrationPath' => '@app/migrations/db' ], 'migrate-db2' => [ // Fixture generation command line. 'class' => 'yii\console\controllers\MigrateController', 'db' => 'db2', 'templateFile' => '@app/views/migrations/migration_db2.php', 'migrationPath' => '@app/migrations/db2' ], ], 


As a result, we obtain independent migrations for each database. Migrations are run by commands.

yii migrate-db
yii migrate-db2


Work as normal migrations. New migrations are created in separate folders for each database commands.

yii migrate-db/create
yii migrate-db2/create


Wrote a small application for demonstration. Laid out on github.
Thanks for attention.

Source: https://habr.com/ru/post/279701/


All Articles