我正在使用具有多个数据库模式的Laravel - 每个客户端一个。我想做的是复制/克隆/复制整个客户端数据库。我的客户端模型设置为在创建时自动创建数据库,如下所示:
static::created(function($client) {
$createJob = new ClientCreate($client);
app('Illuminate\Contracts\Bus\Dispatcher')->dispatch($createJob);
});
DB::connection('admin')->statement("DROP SCHEMA IF EXISTS {$dbName}_{$this->client->code} CASCADE");
DB::connection('admin')->statement("CREATE SCHEMA {$dbName}_{$this->client->code}");
DB::connection('admin')->statement("ALTER SCHEMA {$dbName}_{$this->client->code} OWNER TO postgres");
DB::connection('admin')->statement("GRANT USAGE ON SCHEMA {$dbName}_{$this->client->code} TO {$dbName}_{$this->client->code}");
DB::connection('admin')->statement("GRANT ALL PRIVILEGES ON SCHEMA {$dbName}_{$this->client->code} TO {$dbName}_dba WITH GRANT OPTION");
DB::connection('admin')->statement("GRANT ALL PRIVILEGES ON SCHEMA {$dbName}_{$this->client->code} TO postgres WITH GRANT OPTION");
DB::connection('admin')->statement("ALTER DEFAULT PRIVILEGES IN SCHEMA {$dbName}_{$this->client->code} GRANT select, insert, update, delete ON TABLES TO {$dbName}_{$this->client->code}");
DB::connection('admin')->statement("ALTER DEFAULT PRIVILEGES IN SCHEMA {$dbName}_{$this->client->code} GRANT select, insert, update, delete ON TABLES TO {$dbName}_{$this->client->code}");
DB::connection('admin')->statement("ALTER DEFAULT PRIVILEGES FOR USER postgres IN SCHEMA {$dbName}_{$this->client->code} GRANT select, insert, update, delete ON TABLES TO {$dbName}_{$this->client->code}");
DB::connection('admin')->statement("ALTER DEFAULT PRIVILEGES IN SCHEMA {$dbName}_{$this->client->code} GRANT usage, select, update ON SEQUENCES TO {$dbName}_{$this->client->code}");
DB::connection('admin')->statement("ALTER DEFAULT PRIVILEGES IN SCHEMA {$dbName}_{$this->client->code} GRANT usage, select, update ON SEQUENCES TO {$dbName}_{$this->client->code}");
DB::connection('admin')->statement("ALTER DEFAULT PRIVILEGES FOR USER postgres IN SCHEMA {$dbName}_{$this->client->code} GRANT usage, select, update ON SEQUENCES TO {$dbName}_{$this->client->code}");
DB::connection('admin')->statement("GRANT select, insert, update, delete ON public.users TO {$dbName}_{$this->client->code}");
DB::connection('admin')->statement("GRANT select, insert, update, delete ON public.client_user TO {$dbName}_{$this->client->code}");
Artisan::call('migrate:client', [
'client' => $this->client->code,
]);
我还可以通过调用客户端模型上的方法来激活相应的数据库:
$dbName = Config::get('database.connections.main.database');
Config::set('database.connections.client.schema', $dbName . '_' . $this->code);
Config::set('database.connections.client.username', $dbName . '_' . $this->code);
Config::set('database.connections.client.password', $dbName . '_' . $this->code);
Config::set('app.client', $this->code);
// reconnect to activate the new database settings
DB::reconnect('client');
// set the client as active
App::instance('App\Models\client', $this);
这允许我创建一个填充旧旧客户端的新客户端。但是,它不会复制数据库模式中的表中的所有数据。要实现这一点,我必须通过或模型或表格并手动复制它们。
我的问题是:有没有办法复制整个数据库架构,这样我就可以在所有表中拥有两个相同的客户端?