Use one Laravel migrations table per database(每个数据库使用一个 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
中为每个数据库(在您的情况下为 db1
和 db2
)设置单独的目录并存储每个目录中的适当迁移.然后你可以像这样运行迁移:
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 迁移表
基础教程推荐
- Libpuzzle 索引数百万张图片? 2022-01-01
- 在 PHP 中强制下载文件 - 在 Joomla 框架内 2022-01-01
- 通过 PHP SoapClient 请求发送原始 XML 2021-01-01
- XAMPP 服务器不加载 CSS 文件 2022-01-01
- WooCommerce 中选定产品类别的自定义产品价格后缀 2021-01-01
- 在 Woocommerce 中根据运输方式和付款方式添加费用 2021-01-01
- 超薄框架REST服务两次获得输出 2022-01-01
- 在多维数组中查找最大值 2021-01-01
- mysqli_insert_id 是否有可能在高流量应用程序中返回 2021-01-01
- 如何在 PHP 中的请求之间持久化对象 2022-01-01