Non puoi selezionare più di 25 argomenti
Gli argomenti devono iniziare con una lettera o un numero, possono includere trattini ('-') e possono essere lunghi fino a 35 caratteri.
76 righe
2.5 KiB
76 righe
2.5 KiB
<?php |
|
|
|
namespace App\Console\Commands\Database\Tenant; |
|
|
|
use Illuminate\Console\Command; |
|
use Illuminate\Support\Facades\DB; |
|
use Illuminate\Support\Facades\Config; |
|
use Illuminate\Support\Facades\Artisan; |
|
|
|
class CreateCommmand extends Command |
|
{ |
|
/** |
|
* The name and signature of the console command. |
|
* |
|
* @var string |
|
*/ |
|
protected $signature = 'tenantdb:create {tenant : Tenant id} |
|
{--force : Force the operation to run when in production}'; |
|
|
|
/** |
|
* The console command description. |
|
* |
|
* @var string |
|
*/ |
|
protected $description = 'Create tenant db'; |
|
|
|
/** |
|
* Create a new command instance. |
|
* |
|
* @return void |
|
*/ |
|
public function __construct() |
|
{ |
|
if (\file_exists(\dirname(base_path()) . "/config.json")) { |
|
$this->config = \json_decode(\file_get_contents(\dirname(base_path()) . "/config.json"), 1); |
|
|
|
Config::set('database.connections.mysql.host', $this->config["db_host"]); |
|
Config::set('database.connections.mysql.username', $this->config["db_root_user"]); |
|
Config::set('database.connections.mysql.password', $this->config["db_root_pass"]); |
|
|
|
Config::set('database.connections.mysql-tenant.host', $this->config["db_host"]); |
|
Config::set('database.connections.mysql-tenant.username', $this->config["db_root_user"]); |
|
Config::set('database.connections.mysql-tenant.password', $this->config["db_root_pass"]); |
|
|
|
} |
|
parent::__construct(); |
|
} |
|
|
|
/** |
|
* Execute the console command. |
|
* |
|
* @return int |
|
*/ |
|
public function handle() |
|
{ |
|
$tenant = $this->argument('tenant'); |
|
$database = "{$this->config["prefix_db"]}_ente{$tenant}"; |
|
$options = [ |
|
"--database" => "mysql-tenant", |
|
"--path" => "database/migrations/tenant" |
|
]; |
|
if(!empty( $this->option('force'))) { $options['--force'] = true; } |
|
|
|
Config::set('database.connections.mysql-tenant.database', $database); |
|
|
|
DB::purge('mysql'); |
|
DB::purge('mysql-tenant'); |
|
|
|
DB::connection('mysql')->statement("CREATE DATABASE IF NOT EXISTS {$database} CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;"); |
|
DB::connection('mysql')->statement("GRANT ALTER, CREATE, DELETE, INDEX, INSERT, REFERENCES, SELECT, SHOW VIEW, UPDATE ON {$this->config["prefix_db"]}_ente{$tenant}.* TO '{$this->config["db_user"]}'@'%';"); |
|
DB::connection('mysql')->statement("FLUSH PRIVILEGES;"); |
|
|
|
$this->call("migrate", $options); |
|
|
|
} |
|
}
|
|
|