From cf25621969f5d3d4a0b0bf2e9d64504c7f216001 Mon Sep 17 00:00:00 2001 From: Mike Bronner Date: Tue, 28 Jul 2020 09:01:15 -0700 Subject: [PATCH 1/4] Only create database schema if it doesn't already exist. This is useful if you are recreating the same client without having deleted the previous schema. --- src/Generators/Webserver/Database/Drivers/PostgresSchema.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Generators/Webserver/Database/Drivers/PostgresSchema.php b/src/Generators/Webserver/Database/Drivers/PostgresSchema.php index 76b9ffb5..6b2ce515 100644 --- a/src/Generators/Webserver/Database/Drivers/PostgresSchema.php +++ b/src/Generators/Webserver/Database/Drivers/PostgresSchema.php @@ -24,7 +24,7 @@ class PostgresSchema extends PostgreSQL { protected function createDatabase(IlluminateConnection $connection, array $config) { - return $connection->statement("CREATE SCHEMA \"{$config['schema']}\""); + return $connection->statement("CREATE SCHEMA IF NOT EXISTS \"{$config['schema']}\""); } protected function grantPrivileges(IlluminateConnection $connection, array $config) From 0962897bd8e0f02cf023f833608dee5050fe9353 Mon Sep 17 00:00:00 2001 From: Mike Bronner Date: Tue, 28 Jul 2020 09:49:19 -0700 Subject: [PATCH 2/4] Drop all tables from the schema automatically. --- src/Generators/Webserver/Database/Drivers/PostgresSchema.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Generators/Webserver/Database/Drivers/PostgresSchema.php b/src/Generators/Webserver/Database/Drivers/PostgresSchema.php index 6b2ce515..06cbe937 100644 --- a/src/Generators/Webserver/Database/Drivers/PostgresSchema.php +++ b/src/Generators/Webserver/Database/Drivers/PostgresSchema.php @@ -54,6 +54,6 @@ public function updated(Updated $event, array $config, Connection $connection): protected function dropDatabase(IlluminateConnection $connection, array $config) { - return $connection->statement("DROP SCHEMA IF EXISTS \"{$config['schema']}\""); + return $connection->statement("DROP SCHEMA IF EXISTS \"{$config['schema']}\" CASCADE"); } } From b9524f952c77c2ed96078026402fd4598bb439f1 Mon Sep 17 00:00:00 2001 From: Mike Bronner Date: Tue, 28 Jul 2020 09:51:38 -0700 Subject: [PATCH 3/4] Do not drop privileges when using schema. Dropping privileges when using a single database user across all tenants will attempt to drop all items in the main database and other client schemas as well. --- src/Generators/Webserver/Database/Drivers/PostgreSQL.php | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/Generators/Webserver/Database/Drivers/PostgreSQL.php b/src/Generators/Webserver/Database/Drivers/PostgreSQL.php index 7407fa52..a140cbe3 100644 --- a/src/Generators/Webserver/Database/Drivers/PostgreSQL.php +++ b/src/Generators/Webserver/Database/Drivers/PostgreSQL.php @@ -129,6 +129,10 @@ protected function flushConnection(IlluminateConnection $connection, array $conf protected function dropPriviliges(IlluminateConnection $connection, array $config) { + if (config("tenancy.db.tenant-division-mode") === Connection::DIVISION_MODE_SEPARATE_SCHEMA) { + return true; + } + if ($this->userExists($connection, $config['username'])) { return $connection->statement("DROP OWNED BY \"{$config['username']}\""); } From a505636b9a83efc15441a5443d3d6624ebe741cb Mon Sep 17 00:00:00 2001 From: Mike Bronner Date: Tue, 28 Jul 2020 09:55:56 -0700 Subject: [PATCH 4/4] Update PostgreSQL.php --- src/Generators/Webserver/Database/Drivers/PostgreSQL.php | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/Generators/Webserver/Database/Drivers/PostgreSQL.php b/src/Generators/Webserver/Database/Drivers/PostgreSQL.php index a140cbe3..a60287f7 100644 --- a/src/Generators/Webserver/Database/Drivers/PostgreSQL.php +++ b/src/Generators/Webserver/Database/Drivers/PostgreSQL.php @@ -147,8 +147,9 @@ protected function dropDatabase(IlluminateConnection $connection, array $config) protected function dropUser(IlluminateConnection $connection, array $config) { - if (config('tenancy.db.auto-delete-tenant-database-user') && $this->userExists($connection, - $config['username'])) { + if (config('tenancy.db.auto-delete-tenant-database-user') + && $this->userExists($connection, $config['username']) + ) { return $connection->statement("DROP USER \"{$config['username']}\""); }