Run Artisan Migrate in dynamic banks

Asked

Viewed 139 times

0

Hail, hail people!

I have a problem in a project with multiple databases, each database corresponds to a user that is created when the user confirms their registration.

I created a specific command for this. and this working perfectly, the user confirms his registration through your email and bingo! the data bank is assigned with its contract number in the suffix.

The difficulty I am facing is to run the migrate at this time. It is known that to work with multi database it is necessary to have a standard schema configured in . env.

so I’m doing the following, where db:create is a command created.

  public function workspaceConfig($id)
    {
        $dataBaseName = 'apl_'.$id;
        Artisan::call("db:create ". $dataBaseName);
        Config::set('database.connections.mysql.database', $dataBaseName);
        DB::reconnect();
        Artisan::call('migrate:refresh', [
            '--force' => true,
        ]);
        Config::set('database.default','mysql'); 
        DB::reconnect();
    }

Returns the following:

"message": "SQLSTATE[42S02]: Base table or view not found: 1146 Table 'apl_15.migrations' doesn't exist (SQL: select `migration` from `migrations` order by `batch` asc, `migration` asc)",

my . env

DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=apl_padrao
DB_USERNAME=dbuser
DB_PASSWORD=minhasenha

Does anyone know how to solve?

  • Why instead of using migrate, you don’t create a DB::query with a query by creating the table?

  • Glenys Mitchell, the project is already ready are more than 100 tables already encoded, the creation of tables through DB::query is outside the scope of the project.

  • Which of the lines is giving error and the current error is that there is no table...

1 answer

1


Great, I figured it out. When the method runs Artisan::call('migrate') it is necessary to call a different connection from the default connection, so it looks like this:

public function workspaceConfig($id)
    {
        $dataBaseName = 'aa_'.$id;
        \Artisan::call("db:create ". $dataBaseName);
        \Config::set('database.connections.client.database', $dataBaseName);
        DB::reconnect('client');
            Artisan::call('migrate', array(
                '--database' => 'client', 
                '--force'=> true
            ));
        DB::purge('tenant');
        DB::reconnect('mysql');
    }

and in config/database.php like this:

'mysql' => [
            'driver' => 'mysql',
            'url' => env('DATABASE_URL'),
            'host' => env('DB_HOST', '127.0.0.1'),
            'port' => env('DB_PORT', '3306'),
            'database' => env('DB_DATABASE', 'forge'),
            'username' => env('DB_USERNAME', 'forge'),
            'password' => env('DB_PASSWORD', ''),
            'unix_socket' => env('DB_SOCKET', ''),
            'charset' => 'utf8mb4',
            'collation' => 'utf8mb4_unicode_ci',
            'prefix' => '',
            'prefix_indexes' => true,
            'strict' => true,
            'engine' => null,
            'options' => extension_loaded('pdo_mysql') ? array_filter([
                PDO::MYSQL_ATTR_SSL_CA => env('MYSQL_ATTR_SSL_CA'),
            ]) : [],
        ],

        'client' => [
            'driver' => 'mysql',
            'url' => env('DATABASE_URL'),
            'host' => env('DB_HOST', '127.0.0.1'),
            'port' => env('DB_PORT', '3306'),
            'database' => '',
            'username' => env('DB_USERNAME', 'forge'),
            'password' => env('DB_PASSWORD', ''),
            'unix_socket' => env('DB_SOCKET', ''),
            'charset' => 'utf8mb4',
            'collation' => 'utf8mb4_unicode_ci',
            'prefix' => '',
            'prefix_indexes' => true,
            'strict' => true,
            'engine' => null,
            'options' => extension_loaded('pdo_mysql') ? array_filter([
                PDO::MYSQL_ATTR_SSL_CA => env('MYSQL_ATTR_SSL_CA'),
            ]) : [],
        ],

and keep the . env in:

DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=apl_padrao
DB_USERNAME=dbuser
DB_PASSWORD=minhasenha

that way it worked!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.