每个数据库使用一个 Laravel 迁移表
2022-08-30 22:56:48
我在一个使用多个数据库的项目中工作。Laravel 似乎只使用数据库中设置为默认值的迁移表。我希望每个数据库都有一个迁移表,用于记录已对该特定数据库进行的迁移。这可能吗?
我已经在配置中定义了数据库,如下所示:
'connections' => [
'db1' => array(
'driver' => 'mysql',
'host' => 'db1.host',
'database' => 'db1',
'username' => 'username',
'password' => 'password',
),
'db2' => [
'driver' => 'mysql',
'host' => 'db2.host',
'database' => 'db2',
'username' => 'username',
'password' => 'password',
]
],
我还使第一个数据库(db1)成为默认数据库
'default' => 'db1'
我在两个数据库上都安装了迁移表
artisan migrate:install --database=db1
artisan migrate:install --database=db2
之后,我继续创建几个数据库特定迁移
在 db1 数据库中创建表 test1:
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class CreateTest1Table extends Migration
{
public function up()
{
Schema::connection('db1')->create('test1', function(Blueprint $table)
{
$table->increments('id')->unsigned();
});
}
public function down()
{
Schema::connection('db1')->drop('test1');
}
}
在 db2 数据库中创建表 test2:
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class CreateTest2Table extends Migration
{
public function up()
{
Schema::connection('db2')->create('test2', function(Blueprint $table)
{
$table->increments('id')->unsigned();
});
}
public function down()
{
Schema::connection('db2')->drop('test2');
}
}
我现在运行迁移
artisan migrate
预期成果
db1.迁移
+-----------------------------+-------+
| migration | batch |
+-----------------------------+-------+
| create_test1_table_in_db1 | 1 |
+-----------------------------+-------+
db2.迁移
+-----------------------------+-------+
| migration | batch |
+-----------------------------+-------+
| create_test2_table_in_db2 | 1 |
+-----------------------------+-------+
实际结果
db1.迁移
+-----------------------------+-------+
| migration | batch |
+-----------------------------+-------+
| create_test1_table_in_db1 | 1 |
| create_test2_table_in_db2 | 1 |
+-----------------------------+-------+
db2.迁移
+-----------------------------+-------+
| migration | batch |
+-----------------------------+-------+
Empty set