From 3f3a716a2ab3773f2500d6d547348f7fce1309ea Mon Sep 17 00:00:00 2001 From: Hafez Divandari Date: Mon, 11 Dec 2023 17:39:44 +0330 Subject: [PATCH 1/6] wip --- .../Schema/Grammars/PostgresGrammar.php | 4 +- .../Postgres/PostgresSchemaBuilderTest.php | 51 +++++++++++++++++++ 2 files changed, 53 insertions(+), 2 deletions(-) diff --git a/src/Illuminate/Database/Schema/Grammars/PostgresGrammar.php b/src/Illuminate/Database/Schema/Grammars/PostgresGrammar.php index 2995e339d0d5..ae936300516e 100755 --- a/src/Illuminate/Database/Schema/Grammars/PostgresGrammar.php +++ b/src/Illuminate/Database/Schema/Grammars/PostgresGrammar.php @@ -87,7 +87,7 @@ public function compileTables() { return 'select c.relname as name, n.nspname as schema, pg_total_relation_size(c.oid) as size, ' ."obj_description(c.oid, 'pg_class') as comment from pg_class c, pg_namespace n " - ."where c.relkind = 'r' and n.oid = c.relnamespace " + ."where c.relkind = 'r' and n.oid = c.relnamespace and n.nspname not in ('pg_catalog', 'information_schema')" .'order by c.relname'; } @@ -98,7 +98,7 @@ public function compileTables() */ public function compileViews() { - return 'select viewname as name, schemaname as schema, definition from pg_views order by viewname'; + return "select viewname as name, schemaname as schema, definition from pg_views where schemaname not in ('pg_catalog', 'information_schema') order by viewname"; } /** diff --git a/tests/Integration/Database/Postgres/PostgresSchemaBuilderTest.php b/tests/Integration/Database/Postgres/PostgresSchemaBuilderTest.php index 99240f81b665..b5e638448da9 100644 --- a/tests/Integration/Database/Postgres/PostgresSchemaBuilderTest.php +++ b/tests/Integration/Database/Postgres/PostgresSchemaBuilderTest.php @@ -165,6 +165,57 @@ public function testGetViews() })); } + public function testDropPartitionedTables() + { + DB::unprepared(<<<'SQL' + CREATE TABLE groups ( + id BIGSERIAL, + tenant_id BIGINT, + name VARCHAR, + PRIMARY KEY (id, tenant_id) + ) PARTITION BY HASH (tenant_id); + + CREATE TABLE groups_1 + PARTITION OF groups + FOR VALUES WITH (MODULUS 2, REMAINDER 0); + + CREATE TABLE groups_2 + PARTITION OF groups + FOR VALUES WITH (MODULUS 2, REMAINDER 1); + SQL); + + $tables = Schema::getTables(); + + var_dump($tables); + + $tables = array_column($tables, 'name'); + + $this->assertContains('groups', $tables); + $this->assertContains('groups_1', $tables); + $this->assertContains('groups_2', $tables); + + Schema::dropAllTables(); + + $this->assertEmpty(Schema::getTables()); + + DB::unprepared(<<<'SQL' + CREATE TABLE groups ( + id BIGSERIAL, + tenant_id BIGINT, + name VARCHAR, + PRIMARY KEY (id, tenant_id) + ) PARTITION BY HASH (tenant_id); + + CREATE TABLE groups_1 + PARTITION OF groups + FOR VALUES WITH (MODULUS 2, REMAINDER 0); + + CREATE TABLE groups_2 + PARTITION OF groups + FOR VALUES WITH (MODULUS 2, REMAINDER 1); + SQL); + } + protected function hasView($schema, $table) { return DB::table('information_schema.views') From 97ca9a448a123b72990a0e6c64cda5bfa1f1aab4 Mon Sep 17 00:00:00 2001 From: Hafez Divandari Date: Mon, 11 Dec 2023 17:55:22 +0330 Subject: [PATCH 2/6] include partitioned tables on getTables query --- .../Schema/Grammars/PostgresGrammar.php | 2 +- .../Postgres/PostgresSchemaBuilderTest.php | 42 ++----------------- 2 files changed, 5 insertions(+), 39 deletions(-) diff --git a/src/Illuminate/Database/Schema/Grammars/PostgresGrammar.php b/src/Illuminate/Database/Schema/Grammars/PostgresGrammar.php index ae936300516e..9e18f2d9a773 100755 --- a/src/Illuminate/Database/Schema/Grammars/PostgresGrammar.php +++ b/src/Illuminate/Database/Schema/Grammars/PostgresGrammar.php @@ -87,7 +87,7 @@ public function compileTables() { return 'select c.relname as name, n.nspname as schema, pg_total_relation_size(c.oid) as size, ' ."obj_description(c.oid, 'pg_class') as comment from pg_class c, pg_namespace n " - ."where c.relkind = 'r' and n.oid = c.relnamespace and n.nspname not in ('pg_catalog', 'information_schema')" + ."where c.relkind in ('r', 'p') and n.oid = c.relnamespace and n.nspname not in ('pg_catalog', 'information_schema')" .'order by c.relname'; } diff --git a/tests/Integration/Database/Postgres/PostgresSchemaBuilderTest.php b/tests/Integration/Database/Postgres/PostgresSchemaBuilderTest.php index b5e638448da9..2fde6ff4dc83 100644 --- a/tests/Integration/Database/Postgres/PostgresSchemaBuilderTest.php +++ b/tests/Integration/Database/Postgres/PostgresSchemaBuilderTest.php @@ -167,28 +167,11 @@ public function testGetViews() public function testDropPartitionedTables() { - DB::unprepared(<<<'SQL' - CREATE TABLE groups ( - id BIGSERIAL, - tenant_id BIGINT, - name VARCHAR, - PRIMARY KEY (id, tenant_id) - ) PARTITION BY HASH (tenant_id); - - CREATE TABLE groups_1 - PARTITION OF groups - FOR VALUES WITH (MODULUS 2, REMAINDER 0); - - CREATE TABLE groups_2 - PARTITION OF groups - FOR VALUES WITH (MODULUS 2, REMAINDER 1); - SQL); + DB::statement('create table groups (id bigserial, tenant_id bigint, name varchar, primary key (id, tenant_id)) partition by hash (tenant_id)'); + DB::statement('create table groups_1 partition of groups for values with (modulus 2, remainder 0)'); + DB::statement('create table groups_2 partition of groups for values with (modulus 2, remainder 1)'); - $tables = Schema::getTables(); - - var_dump($tables); - - $tables = array_column($tables, 'name'); + $tables = array_column(Schema::getTables(), 'name'); $this->assertContains('groups', $tables); $this->assertContains('groups_1', $tables); @@ -197,23 +180,6 @@ public function testDropPartitionedTables() Schema::dropAllTables(); $this->assertEmpty(Schema::getTables()); - - DB::unprepared(<<<'SQL' - CREATE TABLE groups ( - id BIGSERIAL, - tenant_id BIGINT, - name VARCHAR, - PRIMARY KEY (id, tenant_id) - ) PARTITION BY HASH (tenant_id); - - CREATE TABLE groups_1 - PARTITION OF groups - FOR VALUES WITH (MODULUS 2, REMAINDER 0); - - CREATE TABLE groups_2 - PARTITION OF groups - FOR VALUES WITH (MODULUS 2, REMAINDER 1); - SQL); } protected function hasView($schema, $table) From 5fcdc4ba6dac491ffda75740956fd6d92906358a Mon Sep 17 00:00:00 2001 From: Hafez Divandari Date: Mon, 11 Dec 2023 18:05:03 +0330 Subject: [PATCH 3/6] fix tests --- .../Database/Postgres/PostgresSchemaBuilderTest.php | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/tests/Integration/Database/Postgres/PostgresSchemaBuilderTest.php b/tests/Integration/Database/Postgres/PostgresSchemaBuilderTest.php index 2fde6ff4dc83..ccfcbbf55a1a 100644 --- a/tests/Integration/Database/Postgres/PostgresSchemaBuilderTest.php +++ b/tests/Integration/Database/Postgres/PostgresSchemaBuilderTest.php @@ -179,7 +179,11 @@ public function testDropPartitionedTables() Schema::dropAllTables(); - $this->assertEmpty(Schema::getTables()); + $this->artisan('migrate:install'); + + $this->assertNotContains('groups', $tables); + $this->assertNotContains('groups_1', $tables); + $this->assertNotContains('groups_2', $tables); } protected function hasView($schema, $table) From 020f83c0d55cf0b5d2b01aad5ddc5e8b4461a5bc Mon Sep 17 00:00:00 2001 From: Hafez Divandari Date: Mon, 11 Dec 2023 18:09:54 +0330 Subject: [PATCH 4/6] fix tests --- .../Integration/Database/Postgres/PostgresSchemaBuilderTest.php | 2 ++ 1 file changed, 2 insertions(+) diff --git a/tests/Integration/Database/Postgres/PostgresSchemaBuilderTest.php b/tests/Integration/Database/Postgres/PostgresSchemaBuilderTest.php index ccfcbbf55a1a..6f7f042fa405 100644 --- a/tests/Integration/Database/Postgres/PostgresSchemaBuilderTest.php +++ b/tests/Integration/Database/Postgres/PostgresSchemaBuilderTest.php @@ -181,6 +181,8 @@ public function testDropPartitionedTables() $this->artisan('migrate:install'); + $tables = array_column(Schema::getTables(), 'name'); + $this->assertNotContains('groups', $tables); $this->assertNotContains('groups_1', $tables); $this->assertNotContains('groups_2', $tables); From 8dad70c106c1d547485ef6f1b7354df826d28086 Mon Sep 17 00:00:00 2001 From: Hafez Divandari Date: Mon, 11 Dec 2023 18:10:38 +0330 Subject: [PATCH 5/6] fix getTables on legacy sqlite version --- src/Illuminate/Database/Schema/SQLiteBuilder.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Illuminate/Database/Schema/SQLiteBuilder.php b/src/Illuminate/Database/Schema/SQLiteBuilder.php index 9e7960ba3a65..f90d015d1fb8 100644 --- a/src/Illuminate/Database/Schema/SQLiteBuilder.php +++ b/src/Illuminate/Database/Schema/SQLiteBuilder.php @@ -37,7 +37,7 @@ public function dropDatabaseIfExists($name) */ public function getTables() { - $withSize = $this->connection->scalar($this->grammar->compileDbstatExists()); + $withSize = rescue($this->connection->scalar($this->grammar->compileDbstatExists()), false, false); return $this->connection->getPostProcessor()->processTables( $this->connection->selectFromWriteConnection($this->grammar->compileTables($withSize)) From 60200bfea9cd2a0f1d09d4450b4db913ed416ff0 Mon Sep 17 00:00:00 2001 From: Hafez Divandari Date: Mon, 11 Dec 2023 18:15:33 +0330 Subject: [PATCH 6/6] fix tests --- src/Illuminate/Database/Schema/SQLiteBuilder.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Illuminate/Database/Schema/SQLiteBuilder.php b/src/Illuminate/Database/Schema/SQLiteBuilder.php index f90d015d1fb8..e2cae2c07c9d 100644 --- a/src/Illuminate/Database/Schema/SQLiteBuilder.php +++ b/src/Illuminate/Database/Schema/SQLiteBuilder.php @@ -37,7 +37,7 @@ public function dropDatabaseIfExists($name) */ public function getTables() { - $withSize = rescue($this->connection->scalar($this->grammar->compileDbstatExists()), false, false); + $withSize = rescue(fn () => $this->connection->scalar($this->grammar->compileDbstatExists()), false, false); return $this->connection->getPostProcessor()->processTables( $this->connection->selectFromWriteConnection($this->grammar->compileTables($withSize))