peterguo 2 сар өмнө
parent
commit
12d3b78580

+ 16 - 4
app/Console/Commands/CreateTenant.php

@@ -36,13 +36,25 @@ class CreateTenant extends Command
         }
 
         $this->info("creating tenant $tenantCode");
-        Tenant::create([
+        $tenant = [
             'id' => $tenantCode,
-            'tenancy_db_connection' => $this->option('db-connection') ?? config('tenancy.database.central_connection'),
-            'tenancy_db_name' => $this->option('db-name') ?? $tenantCode,
             'tenancy_db_username' => $this->option('db-username') ?? $tenantCode,
             'tenancy_db_password' => $this->option('db-password') ?? $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配置
+        if (!empty($this->option('db-connection'))) {
+            $tenant['tenancy_db_connection'] = $this->option('db-connection');
+        }
+        try {
+            Tenant::create($tenant);
+        } catch (\Exception $e) {
+            // TODO: handle exception
+            $this->error("create tenant $tenantCode fail:" . $e->getMessage());
+        }
         $this->info("create tenant $tenantCode success");
     }
 }

+ 4 - 4
app/Models/Tenant.php

@@ -16,16 +16,16 @@ class Tenant extends BaseTenant implements TenantWithDatabase
     use HasDatabase, HasDomains;
 
     protected $casts = [
-        'db_username' => 'encrypted',
-        'db_password' => 'encrypted',
+//        'tenancy_db_username' => 'encrypted',
+//        'tenancy_db_password' => 'encrypted',
     ];
 
     public static function getCustomColumns(): array
     {
         return [
             'id',
-            'db_username' => 'encrypted',
-            'db_password' => 'encrypted',
+//            'tenancy_db_username' => 'encrypted',
+//            'tenancy_db_password' => 'encrypted',
         ];
     }
 }

+ 1 - 1
config/tenancy.php

@@ -45,7 +45,7 @@ return [
          * Connection used as a "template" for the dynamically created tenant database connection.
          * Note: don't name your template connection tenant. That name is reserved by package.
          */
-        'template_tenant_connection' => null,
+        'template_tenant_connection' => env('CENTRAL_DB_CONNECTION', 'central'),
 
         /**
          * Tenant database names are created like this: