Skip to content

Commit cdff793

Browse files
authored
Added method Hyperf\Database\Schema::getTables().
1 parent b27d37d commit cdff793

File tree

4 files changed

+92
-0
lines changed

4 files changed

+92
-0
lines changed

src/Schema/Grammars/PostgresGrammar.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,17 @@ public function compileDropDatabaseIfExists($name): string
7777
);
7878
}
7979

80+
/**
81+
* Compile the query to determine the tables.
82+
*/
83+
public function compileTables(): string
84+
{
85+
return 'select c.relname as name, n.nspname as schema, pg_total_relation_size(c.oid) as size, '
86+
. "obj_description(c.oid, 'pg_class') as comment from pg_class c, pg_namespace n "
87+
. "where c.relkind in ('r', 'p') and n.oid = c.relnamespace and n.nspname not in ('pg_catalog', 'information_schema') "
88+
. 'order by c.relname';
89+
}
90+
8091
/**
8192
* Compile the query to determine if a table exists.
8293
*/

src/Schema/PostgresBuilder.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,16 @@ public function hasTable($table): bool
6060
)) > 0;
6161
}
6262

63+
/**
64+
* Get the tables that belong to the database.
65+
*/
66+
public function getTables(): array
67+
{
68+
return $this->connection->getPostProcessor()->processTables(
69+
$this->connection->selectFromWriteConnection($this->grammar->compileTables())
70+
);
71+
}
72+
6373
/**
6474
* Drop all tables from the database.
6575
*/

tests/Cases/DatabasePostgresSchemaGrammarTest.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1046,6 +1046,14 @@ public function testGrammarsAreMacroable()
10461046
$this->assertTrue($c);
10471047
}
10481048

1049+
public function testCompileTables(): void
1050+
{
1051+
$this->assertSame('select c.relname as name, n.nspname as schema, pg_total_relation_size(c.oid) as size, '
1052+
. "obj_description(c.oid, 'pg_class') as comment from pg_class c, pg_namespace n "
1053+
. "where c.relkind in ('r', 'p') and n.oid = c.relnamespace and n.nspname not in ('pg_catalog', 'information_schema') "
1054+
. 'order by c.relname', $this->getGrammar()->compileTables());
1055+
}
1056+
10491057
public function testAddingFulltextIndexMultipleColumns()
10501058
{
10511059
$blueprint = new Blueprint('users');

tests/Cases/SchemaBuilderTest.php

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
/**
5+
* This file is part of Hyperf.
6+
*
7+
* @link https://www.hyperf.io
8+
* @document https://hyperf.wiki
9+
* @contact group@hyperf.io
10+
* @license https://github.com/hyperf/hyperf/blob/master/LICENSE
11+
*/
12+
13+
namespace HyperfTest\Database\PgSQL\Cases;
14+
15+
use Hyperf\Database\Schema\Blueprint;
16+
use Hyperf\Database\Schema\Schema;
17+
use Hyperf\DbConnection\Db;
18+
use HyperfTest\Database\PgSQL\Stubs\ContainerStub;
19+
use PHPUnit\Framework\Attributes\RequiresPhpExtension;
20+
use PHPUnit\Framework\TestCase;
21+
22+
/**
23+
* @internal
24+
* @coversNothing
25+
*/
26+
#[RequiresPhpExtension('swoole', '< 6.0')]
27+
class SchemaBuilderTest extends TestCase
28+
{
29+
protected function setUp(): void
30+
{
31+
$container = ContainerStub::getContainer();
32+
$container->allows('get')->with(Db::class)->andReturns(new Db($container));
33+
}
34+
35+
protected function tearDown(): void
36+
{
37+
Schema::drop('foo');
38+
Schema::drop('bar');
39+
Schema::drop('baz');
40+
}
41+
42+
public function testGetTables(): void
43+
{
44+
Schema::create('foo', static function (Blueprint $table) {
45+
$table->comment('This is a comment');
46+
$table->id();
47+
});
48+
49+
Schema::create('bar', static function (Blueprint $table) {
50+
$table->id('name');
51+
});
52+
53+
Schema::create('baz', static function (Blueprint $table) {
54+
$table->id('votes');
55+
});
56+
57+
$tables = Schema::getTables();
58+
$this->assertEmpty(array_diff(['foo', 'bar', 'baz'], array_column($tables, 'name')));
59+
$this->assertNotEmpty(array_filter($tables, static function ($table) {
60+
return $table['name'] === 'foo';
61+
}));
62+
}
63+
}

0 commit comments

Comments
 (0)