Welcome Guest, Not a member yet? Register   Sign In
Run migration on multiple databases
#1

I am working multi database tenancy application.
I have created custom command to run migration to all tenant database.

When I run command, migration is added on only first database it is not adding migration to other databases.

Below is result when I run command.
Code:
λ php spark company:migrate                                                           
                                                                                     
                                                                                     
CodeIgniter CLI Tool - Version 4.0.4 - Server-Time: 2021-06-25 01:26:53am             
                                                                                     
running Ortega Traders migration                                                     
-----------Done------------                                                           
running shriram migration                                                             
-----------Done------------                                                           
running Padilla and Avila Trading migration                                           
-----------Done------------                                                           
running DC Inc migration                                                             
-----------Done------------                                                           
running pragnesh migration                                                           
-----------Done------------                                                           


Custom command code.
PHP Code:
<?php

namespace App\Commands;

use 
CodeIgniter\CLI\BaseCommand;

class 
Company extends BaseCommand
{
    /**
    * The Command's Group
    *
    * @var string
    */
    protected $group 'Company';

    /**
    * The Command's Name
    *
    * @var string
    */
    protected $name 'company:migrate';

    /**
    * The Command's Description
    *
    * @var string
    */
    protected $description 'This command will run company migrations';

    /**
    * The Command's Usage
    *
    * @var string
    */
    protected $usage 'company:migrate - Run company migration';

    /**
    * The Command's Arguments
    *
    * @var array
    */
    protected $arguments = [];

    /**
    * The Command's Options
    *
    * @var array
    */
    protected $options = [];

    /**
    * Actually execute a command.
    *
    * @param array $params
    */
    public function run(array $params)
    {
        $db null;
        $user = new \App\Models\UsersModel();
        $companies $user->where('db_name <>''')->findAll();
        if (!empty($companies)) {
            $database config('Database');
            foreach ($companies as $company) {
                $database->company = [
                    'DSN'      => 'mysql:host=' $company['db_host'] . ';dbname=' $company['db_name'],
                    'hostname' => $company['db_host'],
                    'username' => $company['db_user'],
                    'password' => $company['db_password'],
                    'database' => $company['db_name'],
                    'DBDriver' => 'MySQLi',
                    'DBPrefix' => '',
                    'pConnect' => false,
                    'DBDebug'  => (ENVIRONMENT !== 'production'),
                    'charset'  => 'utf8',
                    'DBCollat' => 'utf8_general_ci',
                    'swapPre'  => '',
                    'encrypt'  => false,
                    'compress' => false,
                    'strictOn' => false,
                    'failover' => [],
                    'port'    => $company['db_port'],
                ];

                $db = \Config\Database::connect('company');
                echo "running " $company['company_name'] . " migration \n";
                //echo command('migrate -n Company');
                $migration = \Config\Services::migrations(null$db);
                try {
                    $migration->setNamespace('Tenant')
                        ->latest('company');
                } catch (\Throwable $e) {
                    log_message('error'$e->getMessage());
                }

                $db->close();
                echo "-----------Done------------ \n";
            }
        }
    }


Autoload.php
Code:
public $psr4 = [
APP_NAMESPACE => APPPATH, // For custom app namespace
'Config'      => APPPATH . 'Config',
'Dompdf'      => APPPATH . 'ThirdParty/dompdf/src',
'Stripe'      => APPPATH . 'Stripe',
'Paypal'      => APPPATH . 'Paypal/Paypal.php',
'Tenant' => ROOTPATH . 'Tenant',
];

migration file
PHP Code:
<?php

namespace Tenant\Database\Migrations;

use 
CodeIgniter\Database\Migration;

class 
CiSettings extends Migration
{
 protected 
$DBGroup 'company';
 public function 
up()
 {
 
$this->forge->addField([
 
'setting_id' => [
 
'type' => 'INT',
 
'constraint' => 11,
 
'unsigned' => true,
 
'auto_increment' => true,
 ],
 
'key' => [
 
'type' => 'VARCHAR',
 
'constraint' => '191',
 ],
 
'value' => [
 
'type' => 'TEXT',
 ],
 ]);
 
$this->forge->addKey('setting_id'true);
 
$this->forge->createTable('ci_settings');
 }

 
//--------------------------------------------------------------------

 
public function down()
 {
 
$this->forge->dropTable('ci_settings');
 }



Database config:
Code:
public $default = [
'DSN'      => 'mysql:host=localhost;dbname=mycompdata',
'hostname' => 'localhost',
'username' => 'root',
'password' => '',
'database' => 'ci_hrmsv4',
'DBDriver' => 'MySQLi',
'DBPrefix' => '',
'pConnect' => true,
'DBDebug'  => (ENVIRONMENT !== 'production'),
'cacheOn'  => false,
'cacheDir' => '',
'charset'  => 'utf8',
'DBCollat' => 'utf8_general_ci',
'swapPre'  => '',
'encrypt'  => false,
'compress' => false,
'strictOn' => false,
'failover' => [],
'port'    => 3306,
];

public $company = [
'DSN'      => 'mysql:host=localhost;dbname=',
'hostname' => 'localhost',
'username' => 'root',
'password' => '',
'database' => '',
'DBDriver' => 'MySQLi',
'DBPrefix' => '',
'pConnect' => true,
'DBDebug'  => (ENVIRONMENT !== 'production'),
'cacheOn'  => false,
'cacheDir' => '',
'charset'  => 'utf8',
'DBCollat' => 'utf8_general_ci',
'swapPre'  => '',
'encrypt'  => false,
'compress' => false,
'strictOn' => false,
'failover' => [],
'port'    => 3306,
];
Reply
#2

by adding shared instance to false, I am able to run migration for all tables

Code:
$db = \Config\Database::connect('company', false);

$migration = \Config\Services::migrations(null, $db, false);
Reply
#3

Glad you got it working. Please add [SOLVED] to you topic title at the end so other users can find it.
What did you Try? What did you Get? What did you Expect?

Joined CodeIgniter Community 2009.  ( Skype: insitfx )
Reply




Theme © iAndrew 2016 - Forum software by © MyBB