tklustig
tklustig

Reputation: 503

Switching databases between development and deployment

Hy guys, how to switch probably databases from development to deployment in yii2-advanced! I use following config-file:

.
.
.
     'db_developpment' => [
            'class' => 'yii\db\Connection',
            'dsn' => 'mysql:host=localhost;dbname=yii2_widget',
            'username' => 'root',
            'password' => '',
            'charset' => 'utf8',
        ],
        'db_deployment' => [
            'class' => 'yii\db\Connection',
            'dsn' => 'mysql:host="http:/tklustig.ddns.net";dbname=yii2_widget',
            'username' => 'my_name',
            'password' => 'my_password',
            'charset' => 'utf8',
        ],
.
.
.

I suppose, it's necessary to code an IF-statement in order to difference between developpment and deployment settings. How to code this IF-statement correctly?

Upvotes: 0

Views: 34

Answers (1)

Bizley
Bizley

Reputation: 18021

Do you really need two DB connections at the same time? It makes everything more complicated.

The preferred way is to keep several environments configuration. Each environment uses it's own DB connection setup.

See the Yii 2 Advanced Template Project docs about this.

Basically you prepare two different configs:

  • /environments/dev/common/config/main-local.php
  • /environments/prod/common/config/main-local.php

Inside there is db component configured for each environment. When deploying app you run console command init where you choose environment to be initialized so the proper main-local.php file is copied to folder and now db component uses the environment-based configuration so you just use one active connection.

Upvotes: 2

Related Questions