Skip to content

Commit

Permalink
[10.x] Include partitioned tables on PostgreSQL when retrieving tables (
Browse files Browse the repository at this point in the history
#49326)

* wip

* include partitioned tables on getTables query

* fix tests

* fix tests

* fix getTables on legacy sqlite version

* fix tests
  • Loading branch information
hafezdivandari committed Dec 11, 2023
1 parent 4fe214d commit cc5d95c
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 3 deletions.
4 changes: 2 additions & 2 deletions src/Illuminate/Database/Schema/Grammars/PostgresGrammar.php
Expand Up @@ -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 in ('r', 'p') and n.oid = c.relnamespace and n.nspname not in ('pg_catalog', 'information_schema')"
.'order by c.relname';
}

Expand All @@ -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";
}

/**
Expand Down
2 changes: 1 addition & 1 deletion src/Illuminate/Database/Schema/SQLiteBuilder.php
Expand Up @@ -37,7 +37,7 @@ public function dropDatabaseIfExists($name)
*/
public function getTables()
{
$withSize = $this->connection->scalar($this->grammar->compileDbstatExists());
$withSize = rescue(fn () => $this->connection->scalar($this->grammar->compileDbstatExists()), false, false);

return $this->connection->getPostProcessor()->processTables(
$this->connection->selectFromWriteConnection($this->grammar->compileTables($withSize))
Expand Down
23 changes: 23 additions & 0 deletions tests/Integration/Database/Postgres/PostgresSchemaBuilderTest.php
Expand Up @@ -165,6 +165,29 @@ public function testGetViews()
}));
}

public function testDropPartitionedTables()
{
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 = array_column(Schema::getTables(), 'name');

$this->assertContains('groups', $tables);
$this->assertContains('groups_1', $tables);
$this->assertContains('groups_2', $tables);

Schema::dropAllTables();

$this->artisan('migrate:install');

$tables = array_column(Schema::getTables(), 'name');

$this->assertNotContains('groups', $tables);
$this->assertNotContains('groups_1', $tables);
$this->assertNotContains('groups_2', $tables);
}

protected function hasView($schema, $table)
{
return DB::table('information_schema.views')
Expand Down

0 comments on commit cc5d95c

Please sign in to comment.