douiwn6941 2019-07-04 12:28
浏览 66
已采纳

运行数据库迁移时使用特定的数据库连接

I have the following database migration that I use it for testing:

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

The whole idea is to use the migrations in order to create and nuking databases when I run phpunit integration tests. Because of no prior migration script existance I created a schema-only database dump in resources/database_dumps/my_website.sql.

And I use that dump in order to load the schema into my database. BUT my application has 2 database connections:

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,
        ],
    ],
];

And I want to run this migration into the seth_rollins database connection. Do you have any idea how to do that? As you can see my default database is the brock_lestnar one.

  • 写回答

1条回答 默认 最新

  • dongshuo6503 2019-07-05 14:49
    关注

    Assuming you have this toy Migration:

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

    You can specify via the Schema::connection('seth_rollins'); your connection you want to run your migration. Also you can specify a folder as well on where the tests will run as well. Assuming you made the folder then run:

      php artisan make:migration --path './database/migrations/seth_rollins' ^migration_name^ --table=^table_name^ 
    

    And you can run migrations as seen in this asnwer.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化
  • ¥15 Mirare PLUS 进行密钥认证?(详解)
  • ¥15 物体双站RCS和其组成阵列后的双站RCS关系验证
  • ¥20 想用ollama做一个自己的AI数据库
  • ¥15 关于qualoth编辑及缝合服装领子的问题解决方案探寻
  • ¥15 请问怎么才能复现这样的图呀