每个数据库使用一个Laravel迁移表


Use one Laravel migrations table per database

我在一个使用多个数据库的项目中工作。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

关于在Laravel中使用多个数据库的文章-https://stackcoder.in/posts/laravel-7x-multiple-database-connections-migrations-relationships-querying

--database参数与migrate命令一起使用,并将每个数据库的迁移存储在单独的目录中。

您可以在app/database/migrations中为每个数据库(在您的案例中是db1db2)设置单独的目录,并在每个目录中存储适当的迁移。然后你可以像这样运行迁移:

artisan migrate --database="db1" --path="app/database/migrations/db1"
artisan migrate --database="db2" --path="app/database/migrations/db2"

这样,migrations表对于每个数据库都是独立的。

如果您想更进一步并自动化该过程,您可以创建一个自定义命令,该命令将同时运行所有迁移。您可以创建这样的命令(对于Laravel 5.0至5.2使用make:console,对于Laravel5.2+使用make:command):

artisan command:make MigrateAllCommand --command=migrate:all

这将创建一个新文件app/commands/MigrateAllCommand.php。你的命令的fire方法看起来像这样:

public function fire()
{
    foreach (Config::get('database.connections') as $name => $details)
    {
        $this->info('Running migration for "' . $name . '"');
        $this->call('migrate', array('--database' => $name, '--path' => 'app/database/migrations/' . $name));
    }
}

如果数据库配置密钥的名称与迁移目录名称相同,这将起作用。然后你可以这样称呼它:

artisan migrate:all

您可以查看Laravel命令文档了解更多信息。