Skip to content

Commit

Permalink
Added method Hyperf\Database\Schema::getTables().
Browse files Browse the repository at this point in the history
  • Loading branch information
zds-s authored Jun 7, 2024
1 parent b27d37d commit cdff793
Show file tree
Hide file tree
Showing 4 changed files with 92 additions and 0 deletions.
11 changes: 11 additions & 0 deletions src/Schema/Grammars/PostgresGrammar.php
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,17 @@ public function compileDropDatabaseIfExists($name): string
);
}

/**
* Compile the query to determine the tables.
*/
public function compileTables(): string
{
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 in ('r', 'p') and n.oid = c.relnamespace and n.nspname not in ('pg_catalog', 'information_schema') "
. 'order by c.relname';
}

/**
* Compile the query to determine if a table exists.
*/
Expand Down
10 changes: 10 additions & 0 deletions src/Schema/PostgresBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,16 @@ public function hasTable($table): bool
)) > 0;
}

/**
* Get the tables that belong to the database.
*/
public function getTables(): array
{
return $this->connection->getPostProcessor()->processTables(
$this->connection->selectFromWriteConnection($this->grammar->compileTables())
);
}

/**
* Drop all tables from the database.
*/
Expand Down
8 changes: 8 additions & 0 deletions tests/Cases/DatabasePostgresSchemaGrammarTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1046,6 +1046,14 @@ public function testGrammarsAreMacroable()
$this->assertTrue($c);
}

public function testCompileTables(): void
{
$this->assertSame('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 in ('r', 'p') and n.oid = c.relnamespace and n.nspname not in ('pg_catalog', 'information_schema') "
. 'order by c.relname', $this->getGrammar()->compileTables());
}

public function testAddingFulltextIndexMultipleColumns()
{
$blueprint = new Blueprint('users');
Expand Down
63 changes: 63 additions & 0 deletions tests/Cases/SchemaBuilderTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
<?php

declare(strict_types=1);
/**
* This file is part of Hyperf.
*
* @link https://www.hyperf.io
* @document https://hyperf.wiki
* @contact [email protected]
* @license https://github.com/hyperf/hyperf/blob/master/LICENSE
*/

namespace HyperfTest\Database\PgSQL\Cases;

use Hyperf\Database\Schema\Blueprint;
use Hyperf\Database\Schema\Schema;
use Hyperf\DbConnection\Db;
use HyperfTest\Database\PgSQL\Stubs\ContainerStub;
use PHPUnit\Framework\Attributes\RequiresPhpExtension;
use PHPUnit\Framework\TestCase;

/**
* @internal
* @coversNothing
*/
#[RequiresPhpExtension('swoole', '< 6.0')]
class SchemaBuilderTest extends TestCase
{
protected function setUp(): void
{
$container = ContainerStub::getContainer();
$container->allows('get')->with(Db::class)->andReturns(new Db($container));
}

protected function tearDown(): void
{
Schema::drop('foo');
Schema::drop('bar');
Schema::drop('baz');
}

public function testGetTables(): void
{
Schema::create('foo', static function (Blueprint $table) {
$table->comment('This is a comment');
$table->id();
});

Schema::create('bar', static function (Blueprint $table) {
$table->id('name');
});

Schema::create('baz', static function (Blueprint $table) {
$table->id('votes');
});

$tables = Schema::getTables();
$this->assertEmpty(array_diff(['foo', 'bar', 'baz'], array_column($tables, 'name')));
$this->assertNotEmpty(array_filter($tables, static function ($table) {
return $table['name'] === 'foo';
}));
}
}

0 comments on commit cdff793

Please sign in to comment.