|
@@ -11,11 +11,11 @@ class CreateTenant extends Command
|
|
|
{
|
|
|
/**
|
|
|
* The name and signature of the console command.
|
|
|
- * use php artisan create-tenant {tenantCode} {--db-connection=} {--db-name=} {--db-username=} {--db-password=}
|
|
|
- * eg: php artisan create-tenant test --db-connection=central --db-name=tenant_test --db-username=root --db-password=root
|
|
|
+ * use php artisan create-tenant {tenantCode} {--db-connection=} {--db-name=}
|
|
|
+ * eg: php artisan create-tenant test --db-connection=central --db-name=tenant_test
|
|
|
* @var string
|
|
|
*/
|
|
|
- protected $signature = 'create-tenant {tenantCode} {--db-connection=} {--db-name=} {--db-username=} {--db-password=}';
|
|
|
+ protected $signature = 'create-tenant {tenantCode} {--db-connection=} {--db-name=}';
|
|
|
|
|
|
/**
|
|
|
* The console command description.
|
|
@@ -37,24 +37,29 @@ class CreateTenant extends Command
|
|
|
|
|
|
$this->info("creating tenant $tenantCode");
|
|
|
$tenant = [
|
|
|
- 'id' => $tenantCode,
|
|
|
- 'tenancy_db_username' => $this->option('db-username') ?? $tenantCode,
|
|
|
- 'tenancy_db_password' => $this->option('db-password') ?? $tenantCode,
|
|
|
+ 'id' => $tenantCode
|
|
|
];
|
|
|
- // 默认值为 tenancy.database.prefix配置 + 租户 ID +tenancy.database.suffix配置
|
|
|
- if (!empty($this->option('db-name'))) {
|
|
|
- $tenant['tenancy_db_name'] = $this->option('db-name');
|
|
|
- }
|
|
|
// 默认值为 tenancy.database.template_connection配置
|
|
|
+ $tenant['tenancy_db_connection'] = config('tenancy.database.template_connection');
|
|
|
+
|
|
|
if (!empty($this->option('db-connection'))) {
|
|
|
$tenant['tenancy_db_connection'] = $this->option('db-connection');
|
|
|
}
|
|
|
+
|
|
|
+ $tenant['tenancy_db_username'] = config("database.connections.{$tenant['tenancy_db_connection']}.username");
|
|
|
+ $tenant['tenancy_db_password'] = config("database.connections.{$tenant['tenancy_db_connection']}.password");
|
|
|
+
|
|
|
+ // 默认值为 tenancy.database.prefix配置 + 租户 ID +tenancy.database.suffix配置
|
|
|
+ if (!empty($this->option('db-name'))) {
|
|
|
+ $tenant['tenancy_db_name'] = $this->option('db-name');
|
|
|
+ }
|
|
|
+
|
|
|
try {
|
|
|
Tenant::create($tenant);
|
|
|
+ $this->info("create tenant $tenantCode success");
|
|
|
+ $this->info("database info: " . json_encode($tenant, 256));
|
|
|
} catch (\Exception $e) {
|
|
|
- // TODO: handle exception
|
|
|
$this->error("create tenant $tenantCode fail:" . $e->getMessage());
|
|
|
}
|
|
|
- $this->info("create tenant $tenantCode success");
|
|
|
}
|
|
|
}
|