Home » Php » Laravel: Run migrations on another database

Laravel: Run migrations on another database

Posted by: admin November 29, 2017 Leave a comment

Questions:

In my app every user has its own database that created when user registered. Connection and database data (database name, username, password) are saved in a table in default database.

try{
    DB::transaction(function() {

        $website = new Website();
        $website->user_id = Auth::get()->id;
        $website->save();

        $database_name = 'website_'.$website->id;

        DB::statement(DB::raw('CREATE DATABASE ' . $database_name));

        $websiteDatabase = new WebsiteDatabase();
        $websiteDatabase->website_id = $website->id;
        $websiteDatabase->database_name = $database_name;
        $websiteDatabase->save();

    });
} catch(\Exception $e) {
    echo $e->getMessage();
}

Now I want to run some migrations on new user’s database after its creation.

Is it possible?

thanks

Answers:

In your app/config/database.php you have to:

<?php
return array(

    'default' => 'mysql',

    'connections' => array(

        # Our primary database connection
        'mysql' => array(
            'driver'    => 'mysql',
            'host'      => 'host1',
            'database'  => 'database1',
            'username'  => 'user1',
            'password'  => 'pass1'
            'charset'   => 'utf8',
            'collation' => 'utf8_unicode_ci',
            'prefix'    => '',
        ),

        # Our secondary database connection
        'mysql2' => array(
            'driver'    => 'mysql',
            'host'      => 'host2',
            'database'  => 'database2',
            'username'  => 'user2',
            'password'  => 'pass2'
            'charset'   => 'utf8',
            'collation' => 'utf8_unicode_ci',
            'prefix'    => '',
        ),
    ),
);

Now that you prepared two database connections in your migration you can do:

Schema::connection('mysql2')->create('some_table', function($table)
{
    $table->increments('id'):
});

This should work.
More infos on: http://fideloper.com/laravel-multiple-database-connections

Questions:
Answers:

If you place database config on the database.php file, this can help you:

php artisan migrate --database=**otherDatabase**

Questions:
Answers:

I think I finally figured out this mess… This solution doesn’t need a configuration for each tenant’s database and has to be run only once.

class MigrationBlah extends Migration {
  public function up() {
    $dbs = DB::connection('tenants')->table('tenants')->get();
    foreach ($dbs as $db) {
      Schema::table($db->database . '.bodegausuarios', function($table){
        $table->foreign('usuario')->references('usuarioid')->on('authusuarios');
      });
    }
  }
}

Where I have a connection named “tenants” on my database.php, which contains the database name of all of my tenants. I have the default connection set to my tenants database as well. That database is the one responsible for taking care of the migrations table.

With the foreach statement, it goes through the tenant databases and runs the migration on each one.

On your default connection, you should configure a user that has access to all tenant’s databases for it to work.