Yiic 多数据库迁移
Yiic migration for multiple database
是否可以运行 Yiic migrate create 命令用于多个数据库?
我有一个多租户数据架构,一个源代码和多个客户端数据库。每个客户端将使用相同的源代码和一个单独的数据库。
例如。 Andrew.Digital.com、Samson.Digital.com 将有两个数据库但指向相同的源代码。
所以我只想知道是否有可能对多个数据库使用 Yiic 迁移?
什么是 SAAS(多租户)https://msdn.microsoft.com/en-us/library/aa479086.aspx
您可以在 protected/config/console.php
上配置多个连接
然后,运行迁移时,指定连接id
/yiic migrate --connectionID=db # The default
/yiic migrate --connectionID=andrew # connection andrew
/yiic migrate --connectionID=client3 # connection client3
您可以配置多个数据库连接并在 migrate 命令中提及 @crafter,或者您可以在 运行 时覆盖数据库连接并创建它动态.
这里有一些步骤可以覆盖默认的 Yii 迁移控制器 class 并使用多个数据库迁移。
1:在console/controllers/MigrateController
下新建控制器class(MigrateController)
2:将以下内容添加到上面创建的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 <nadeem@myswich.com>
* 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: 在你的 console/config/main.php
添加 migrate controller map
.
.
.
'controllerMap' => [
'migrate' => 'console\controllers\MigrateController',
],
4:现在创建一些虚拟迁移并在 $array 列表中添加三个或四个数据库
5:你都做到了!!!
你可以根据自己的需要在这上面多玩
是否可以运行 Yiic migrate create 命令用于多个数据库?
我有一个多租户数据架构,一个源代码和多个客户端数据库。每个客户端将使用相同的源代码和一个单独的数据库。
例如。 Andrew.Digital.com、Samson.Digital.com 将有两个数据库但指向相同的源代码。
所以我只想知道是否有可能对多个数据库使用 Yiic 迁移?
什么是 SAAS(多租户)https://msdn.microsoft.com/en-us/library/aa479086.aspx
您可以在 protected/config/console.php
上配置多个连接然后,运行迁移时,指定连接id
/yiic migrate --connectionID=db # The default
/yiic migrate --connectionID=andrew # connection andrew
/yiic migrate --connectionID=client3 # connection client3
您可以配置多个数据库连接并在 migrate 命令中提及 @crafter,或者您可以在 运行 时覆盖数据库连接并创建它动态.
这里有一些步骤可以覆盖默认的 Yii 迁移控制器 class 并使用多个数据库迁移。
1:在console/controllers/MigrateController
下新建控制器class(MigrateController)2:将以下内容添加到上面创建的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 <nadeem@myswich.com>
* 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: 在你的 console/config/main.php
添加 migrate controller map.
.
.
'controllerMap' => [
'migrate' => 'console\controllers\MigrateController',
],
4:现在创建一些虚拟迁移并在 $array 列表中添加三个或四个数据库
5:你都做到了!!!
你可以根据自己的需要在这上面多玩