我有以下用于测试的数据库迁移:
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facade\App;
use Illuminate\Database\Migrations\Migration;
class TestWebsitedbMigration extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
if(App::environment() == "testing")
{
ini_set('memory_limit', '-1');
DB::unprepared( file_get_contents( "resources/database_dumps/my_website.sql" ) );
}
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
if(App::environment() == "testing")
{
// Burn it down!!!!
// But needs some im plementation first
}
}
}
整个想法是在我运行phpunit
集成测试时使用迁移来创建和创建数据库。由于之前没有迁移脚本,因此我在resources/database_dumps/my_website.sql
中创建了仅模式的数据库转储。
然后使用该转储将模式加载到数据库中。但是我的应用程序有2个数据库连接:
return [
'default' => env('DB_CONNECTION', 'brock_lesnar'),
'connections' => [
'seth_rollins' => [
'driver' => 'pgsql',
'host' => env('DB_API_HOST', '192.168.10.70'),
'port' => env('DB_API_PORT', '5432'),
'database' => env('DB_API_DATABASE', 'etable_api'),
'username' => env('DB_API_USERNAME', ''),
'password' => env('DB_API_PASSWORD', ''),
'charset' => 'utf8',
'prefix' => '',
'schema' => 'public',
'sslmode' => 'prefer',
],
'brock_lesnar' => [
'driver' => 'pgsql',
'host' => env('BROCK_DB_HOST', '192.168.10.70'),
'port' => env('BROCK_DB_PORT', '5432'),
'database' => env('BROCK_DB_DATABASE', 'etable'),
'username' => env('BROCK_DB_USERNAME', ''),
'password' => env('BROCK_DB_PASSWORD', ''),
'charset' => 'utf8',
'prefix' => '',
'schema' => 'public',
'sslmode' => 'prefer',
],
],
'migrations' => 'migrations',
/*
|--------------------------------------------------------------------------
| Redis Databases
|--------------------------------------------------------------------------
|
| Redis is an open source, fast, and advanced key-value store that also
| provides a richer set of commands than a typical key-value systems
| such as APC or Memcached. Laravel makes it easy to dig right in.
|
*/
'redis' => [
'client' => 'predis',
'default' => [
'host' => env('REDIS_HOST', '127.0.0.1'),
'password' => env('REDIS_PASSWORD', NULL),
'port' => env('REDIS_PORT', 6379),
'database' => 0,
],
],
];
我想将此迁移运行到seth_rollins
数据库连接中。你有什么想法吗?如您所见,我的默认数据库是brock_lestnar
。
答案 0 :(得分:0)
假设您有这个玩具迁移:
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class MakeDummyTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
$schema=Schema::connection('seth_rollins');
$schema->create('dummy_table', function (Blueprint $table) {
$table->increments('id');
$table->string('lover')->nullable()->default('Becky Lynch')
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
// Destroy it here
}
}
您可以通过Schema::connection('seth_rollins');
指定要运行迁移的连接。您也可以在运行测试的地方指定一个文件夹。假设您创建了该文件夹,然后运行:
php artisan make:migration --path './database/migrations/seth_rollins' ^migration_name^ --table=^table_name^
您可以运行此asnwer所示的迁移。