沃梦达 / 编程问答 / php问题 / 正文

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

Use one Laravel migrations table per database(每个数据库使用一个 Laravel 迁移表)

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

问题描述

我在一个使用多个数据库的项目中工作.Laravel 似乎只使用数据库中默认设置的迁移表.我希望每个数据库都有一个迁移表,用于记录对该特定数据库所做的迁移.这可能吗?

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 IlluminateDatabaseSchemaBlueprint;
use IlluminateDatabaseMigrationsMigration;

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 IlluminateDatabaseSchemaBlueprint;
use IlluminateDatabaseMigrationsMigration;

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

推荐答案

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

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

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

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

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 迁移表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:每个数据库使用一个 Laravel 迁移表

基础教程推荐