From cdff7937a52b9b6e07d2b77c0f5bf4aa8f52ec4c Mon Sep 17 00:00:00 2001 From: zds <49744633+zds-s@users.noreply.github.com> Date: Fri, 7 Jun 2024 19:54:00 +0800 Subject: [PATCH] Added method `Hyperf\Database\Schema::getTables()`. --- src/Schema/Grammars/PostgresGrammar.php | 11 ++++ src/Schema/PostgresBuilder.php | 10 +++ .../DatabasePostgresSchemaGrammarTest.php | 8 +++ tests/Cases/SchemaBuilderTest.php | 63 +++++++++++++++++++ 4 files changed, 92 insertions(+) create mode 100644 tests/Cases/SchemaBuilderTest.php diff --git a/src/Schema/Grammars/PostgresGrammar.php b/src/Schema/Grammars/PostgresGrammar.php index 762c4ac..a2ea5c7 100644 --- a/src/Schema/Grammars/PostgresGrammar.php +++ b/src/Schema/Grammars/PostgresGrammar.php @@ -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. */ diff --git a/src/Schema/PostgresBuilder.php b/src/Schema/PostgresBuilder.php index 59f08e3..4912edd 100644 --- a/src/Schema/PostgresBuilder.php +++ b/src/Schema/PostgresBuilder.php @@ -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. */ diff --git a/tests/Cases/DatabasePostgresSchemaGrammarTest.php b/tests/Cases/DatabasePostgresSchemaGrammarTest.php index 7800b3a..d316bee 100644 --- a/tests/Cases/DatabasePostgresSchemaGrammarTest.php +++ b/tests/Cases/DatabasePostgresSchemaGrammarTest.php @@ -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'); diff --git a/tests/Cases/SchemaBuilderTest.php b/tests/Cases/SchemaBuilderTest.php new file mode 100644 index 0000000..24fb4cc --- /dev/null +++ b/tests/Cases/SchemaBuilderTest.php @@ -0,0 +1,63 @@ +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'; + })); + } +}