每个数据库使用一个Laravel迁移表 [英] Use one Laravel migrations table per database

查看:278
本文介绍了每个数据库使用一个Laravel迁移表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在一个使用多个数据库的项目中工作.看来Laravel仅使用默认数据库中的migrations-table.我想要每个数据库一个迁移表,该表记录已完成到该特定数据库的迁移.这可能吗?

I work in a project that uses multiple databases. It seems like Laravel only uses the migrations-table in the database that is set as default. I would like one migrations table per database that logs the migrations that have been done to that specific database. Is this possible?

我已经在配置中定义了数据库,如下所示:

I have defined the databases in the config like this:

'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)设置为默认数据库

I also made the first database (db1) the default one

'default' => 'db1'

我在两个数据库上都安装了迁移表

I install the migrations table on both databases

artisan migrate:install --database=db1
artisan migrate:install --database=db2

此后,我继续创建几个数据库特定的迁移

After that i proceed to create a couple of database specifc migrations

在db1数据库中创建表test1:

Create table test1 in db1 database:

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:

Create table test2 in db2 database:

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.migrations

+-----------------------------+-------+
| migration                   | batch |
+-----------------------------+-------+
| create_test1_table_in_db1   |     1 |
+-----------------------------+-------+

db2.migrations

+-----------------------------+-------+
| migration                   | batch |
+-----------------------------+-------+
| create_test2_table_in_db2   |     1 |
+-----------------------------+-------+

实际结果

db1.migrations

+-----------------------------+-------+
| migration                   | batch |
+-----------------------------+-------+
| create_test1_table_in_db1   |     1 |
| create_test2_table_in_db2   |     1 |
+-----------------------------+-------+

db2.migrations

+-----------------------------+-------+
| migration                   | batch |
+-----------------------------+-------+
Empty set

推荐答案

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

Use the --database parameter with the migrate command and store the migrations for each database in separate directories.

对于每个数据库(在您的情况下为db1db2),您都可以在app/database/migrations中具有单独的目录,并将适当的迁移存储在每个目录中.然后,您可以像这样运行迁移:

You could have separate directories in app/database/migrations for each of your database (in your case db1 and db2) and store the appropriate migrations in each directory. Then you could run the migrations like this:

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

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

This way your migrations table will be independent for each database.

如果您想进一步努力并实现流程自动化,则可以创建自定义命令,该命令将一次运行所有迁移.您可以这样创建命令(对于Laravel 5.0到5.2,请使用make:console;对于Laravel 5.2+,请使用make:command):

If you want to go the extra mile and automate the process you could create your custom command that will run all the migrations at once. You can create the command like this (use make:console for Laravel 5.0 up to 5.2 or make:command for Laravel 5.2+):

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

这将创建一个新文件app/commands/MigrateAllCommand.php.您命令的fire方法如下所示:

This will create a new file app/commands/MigrateAllCommand.php. Your command's fire method would look something like this:

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));
    }
}

这将起作用,只要数据库配置键的名称与迁移目录名称相同即可.然后,您可以这样称呼它:

This will work provided the name of the database configuration key is the same as the migration directory name. You can then just call it like this:

artisan migrate:all

您可以查看 Laravel命令文档以获取更多信息.

You can check the Laravel Command Docs for more info.

这篇关于每个数据库使用一个Laravel迁移表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆