Yiic migration for multiple database

937 views Asked by At

Is it possible to run Yiic migrate create command for multiple databases ?

I have a Multi-Tenant Data Architecture, one source code and multiple database for client. each client will use same source code and but a separate database.

Ex. Andrew.Digital.com, Samson.Digital.com will have two database but points to same source code.

So i just want to know is there any possibility to use Yiic migration for multiple databases ?

What is SAAS (Multi-Tenant) https://msdn.microsoft.com/en-us/library/aa479086.aspx

2

There are 2 answers

0
crafter On BEST ANSWER

You can configure more than one connection on your protected/config/console.php

Then, when running the migration, specify the connection id

/yiic migrate --connectionID=db         # The default
/yiic migrate --connectionID=andrew     # connection andrew
/yiic migrate --connectionID=client3    # connection client3
0
Nadeem Latif On

You can configure multiple database connections and mention in migrate command as mentioned above by @crafter, OR you can override DB connection at the run time and make it dynamic.

Here are some steps to override default Yii migrate controller class and used multiple database migrations.

1: Create a new controller class (MigrateController) under console/controllers/MigrateController

2: Add below content into above-created class (MigrateController) (console/controllers/MigrateController).

```

<?php
/**
 * Yii MigrateController class that will override default up functionality & used dynamic connection based on your settings, or DB lists
 * @author Nadeem Akhtar <[email protected]>
 * Date: 1/19/16
 * Time: 4:34 PM
 */


namespace console\controllers;

use Yii;
use yii\console\controllers\MigrateController as BaseMigrateController;
use yii\helpers\Console;

/**
 * Test controller
 */
class MigrateController extends BaseMigrateController {

    /*
     * init function
     *
     *
     * */
    public function init() {
        parent::init();
    }

    /**
     * Upgrades the application by applying new migrations.
     * For example,
     *
     * ```
     * yii migrate     # apply all new migrations
     * yii migrate 3   # apply the first 3 new migrations
     * ```
     *
     * @param integer $limit the number of new migrations to be applied. If 0, it means
     * applying all available new migrations.
     *
     * @return integer the status of the action execution. 0 means normal, other values mean abnormal.
     * Example call: **./yii migrate/migrate-up**
     */
    public function actionMigrateUp($limit = 0)
    {
        // Get all databases from companies settings
        $companies = Companies::find()->where('settings')->all();

        foreach($companies as $company) {

            $this->setRunTimeConnection($company['settings']); // This will set dynamic connection based on your record in DB

            $this->stdout("New migration founds for ".$company['name'].".\n", Console::FG_GREEN); // print message to show which migration is runing for which company 
            $this->actionUp($limit);
        }
    }

    /*
     * Dynamic connection
     * Override default **Yii::$app->db** settings         
     *
     * */

    public function setRunTimeConnection($setting) {

        $host = $setting['DB_host'];
        $dbName = $setting['DB_name'];

        $dsn = "mysql:host=$host;dbname=$dbName"; //Host & Database
        Yii::$app->db->dsn = $dsn;
        Yii::$app->db->username = $setting['DB_username'];
        Yii::$app->db->password = $setting['DB_password'];
    }
}

3: Add migrate controller map in your console/config/main.php

.
.
.
'controllerMap' => [
        'migrate'   => 'console\controllers\MigrateController',
    ],

4: Now create some dummy migrations and add three or four databases in your $array lists

5: You have done all!!!

You can play more on this according to your requirements.