Skip to content

Commit cc5d95c

Browse files
[10.x] Include partitioned tables on PostgreSQL when retrieving tables (#49326)
* wip * include partitioned tables on getTables query * fix tests * fix tests * fix getTables on legacy sqlite version * fix tests
1 parent 4fe214d commit cc5d95c

File tree

3 files changed

+26
-3
lines changed

3 files changed

+26
-3
lines changed

src/Illuminate/Database/Schema/Grammars/PostgresGrammar.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ public function compileTables()
8787
{
8888
return 'select c.relname as name, n.nspname as schema, pg_total_relation_size(c.oid) as size, '
8989
."obj_description(c.oid, 'pg_class') as comment from pg_class c, pg_namespace n "
90-
."where c.relkind = 'r' and n.oid = c.relnamespace "
90+
."where c.relkind in ('r', 'p') and n.oid = c.relnamespace and n.nspname not in ('pg_catalog', 'information_schema')"
9191
.'order by c.relname';
9292
}
9393

@@ -98,7 +98,7 @@ public function compileTables()
9898
*/
9999
public function compileViews()
100100
{
101-
return 'select viewname as name, schemaname as schema, definition from pg_views order by viewname';
101+
return "select viewname as name, schemaname as schema, definition from pg_views where schemaname not in ('pg_catalog', 'information_schema') order by viewname";
102102
}
103103

104104
/**

src/Illuminate/Database/Schema/SQLiteBuilder.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ public function dropDatabaseIfExists($name)
3737
*/
3838
public function getTables()
3939
{
40-
$withSize = $this->connection->scalar($this->grammar->compileDbstatExists());
40+
$withSize = rescue(fn () => $this->connection->scalar($this->grammar->compileDbstatExists()), false, false);
4141

4242
return $this->connection->getPostProcessor()->processTables(
4343
$this->connection->selectFromWriteConnection($this->grammar->compileTables($withSize))

tests/Integration/Database/Postgres/PostgresSchemaBuilderTest.php

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,29 @@ public function testGetViews()
165165
}));
166166
}
167167

168+
public function testDropPartitionedTables()
169+
{
170+
DB::statement('create table groups (id bigserial, tenant_id bigint, name varchar, primary key (id, tenant_id)) partition by hash (tenant_id)');
171+
DB::statement('create table groups_1 partition of groups for values with (modulus 2, remainder 0)');
172+
DB::statement('create table groups_2 partition of groups for values with (modulus 2, remainder 1)');
173+
174+
$tables = array_column(Schema::getTables(), 'name');
175+
176+
$this->assertContains('groups', $tables);
177+
$this->assertContains('groups_1', $tables);
178+
$this->assertContains('groups_2', $tables);
179+
180+
Schema::dropAllTables();
181+
182+
$this->artisan('migrate:install');
183+
184+
$tables = array_column(Schema::getTables(), 'name');
185+
186+
$this->assertNotContains('groups', $tables);
187+
$this->assertNotContains('groups_1', $tables);
188+
$this->assertNotContains('groups_2', $tables);
189+
}
190+
168191
protected function hasView($schema, $table)
169192
{
170193
return DB::table('information_schema.views')

0 commit comments

Comments
 (0)