From 804c63752c6d3679d6a106dd3f5c1aedaf6a68bc Mon Sep 17 00:00:00 2001 From: Dmitriy Gritsenko Date: Wed, 25 Dec 2024 08:41:49 +0500 Subject: [PATCH 01/17] Fix issue #762 --- src/Schema/AbstractSchema.php | 38 +++++++++++++++++++++++++++++++ src/Schema/SchemaInterface.php | 28 +++++++++++++++++++++++ tests/Common/CommonSchemaTest.php | 33 +++++++++++++++++++++++++++ tests/Db/Schema/SchemaTest.php | 14 ++++++++++++ 4 files changed, 113 insertions(+) diff --git a/src/Schema/AbstractSchema.php b/src/Schema/AbstractSchema.php index 78b42cc95..5a52d9322 100644 --- a/src/Schema/AbstractSchema.php +++ b/src/Schema/AbstractSchema.php @@ -574,4 +574,42 @@ public function getViewNames(string $schema = '', bool $refresh = false): array return (array) $this->viewNames[$schema]; } + + /** + * @param string $tableName The table name. + * @param string $schema The schema of the tables. + * @return bool + * @throws Throwable + */ + public function hasTableName(string $tableName, string $schema = ''): bool + { + $tables = $this->getTableNames($schema); + + return in_array($tableName, $tables); + } + + /** + * @param string $schema The schema name. + * @return bool + * @throws Throwable + */ + public function hasSchemaName(string $schema): bool + { + $schemas = $this->getSchemaNames(); + + return in_array($schema, $schemas); + } + + /** + * @param string $viewName The view name. + * @param string $schema The schema of the views. + * @return bool + * @throws Throwable + */ + public function hasViewName(string $viewName, string $schema = ''): bool + { + $views = $this->getViewNames($schema); + + return in_array($viewName, $views); + } } diff --git a/src/Schema/SchemaInterface.php b/src/Schema/SchemaInterface.php index 2632f16e4..9f05ec54d 100644 --- a/src/Schema/SchemaInterface.php +++ b/src/Schema/SchemaInterface.php @@ -416,4 +416,32 @@ public function enableCache(bool $value): void; * @return array All view names in the database. */ public function getViewNames(string $schema = '', bool $refresh = false): array; + + /** + * Determines if a specified table exists in the database. + * + * @param string $tableName The table name to search for + * @param string $schema The schema of the tables. Defaults to empty string, meaning the current or default schema + * name. If not empty, the table will be searched in the specified schema. + * @return bool Whether table exists or not + */ + public function hasTableName(string $tableName, string $schema = ''): bool; + + /** + * Determines if a specified schema exists in the database. + * + * @param string $schema The table name to search for + * @return bool Whether schema exists or not + */ + public function hasSchemaName(string $schema): bool; + + /** + * Determines if a specified view exists in the database. + * + * @param string $viewName The table name to search for + * @param string $schema The schema of the tables. Defaults to empty string, meaning the current or default schema + * name. If not empty, the table will be searched in the specified schema. + * @return bool Whether view exists or not + */ + public function hasViewName(string $viewName, string $schema = ''): bool; } diff --git a/tests/Common/CommonSchemaTest.php b/tests/Common/CommonSchemaTest.php index bbf5447da..5e6ce718d 100644 --- a/tests/Common/CommonSchemaTest.php +++ b/tests/Common/CommonSchemaTest.php @@ -405,6 +405,27 @@ public function testGetTableNames(array $pdoAttributes): void $db->close(); } + public function testHasTableName(array $pdoAttributes): void + { + $db = $this->getConnection(true); + + foreach ($pdoAttributes as $name => $value) { + if ($name === PDO::ATTR_EMULATE_PREPARES) { + continue; + } + + $db->getPDO()?->setAttribute($name, $value); + } + + $schema = $db->getSchema(); + + $this->assertTrue($schema->hasTableName('customer')); + $this->assertTrue($schema->hasTableName('category')); + $this->assertFalse($schema->hasTableName('no_such_table')); + + $db->close(); + } + /** * @dataProvider \Yiisoft\Db\Tests\Provider\SchemaProvider::tableSchema */ @@ -482,6 +503,18 @@ public function testGetViewNames(): void $db->close(); } + public function hasViewName(): void + { + $db = $this->getConnection(true); + + $schema = $db->getSchema(); + + $this->assertTrue($schema->hasViewName('animal_view')); + $this->assertFalse($schema->hasViewName('no_such_view')); + + $db->close(); + } + public function testNegativeDefaultValues(): void { $db = $this->getConnection(true); diff --git a/tests/Db/Schema/SchemaTest.php b/tests/Db/Schema/SchemaTest.php index 365a97d72..e4e7453ca 100644 --- a/tests/Db/Schema/SchemaTest.php +++ b/tests/Db/Schema/SchemaTest.php @@ -202,6 +202,20 @@ public function testGetSchemaNamesWithSchema(): void $this->assertSame(['dbo', 'public'], $schema->getSchemaNames()); } + public function testHasSchemaName(): void + { + $db = $this->getConnection(); + + $schema = $db->getSchema(); + Assert::setInaccessibleProperty($schema, 'schemaNames', ['dbo', 'public']); + + $this->assertTrue($schema->hasSchemaName('dbo')); + $this->assertTrue($schema->hasSchemaName('public')); + $this->assertFalse($schema->hasSchemaName('no_such_schema')); + + $db->close(); + } + /** * @throws NotSupportedException */ From 13f23c726a67393b19130aa6c4b0c987b9b275c1 Mon Sep 17 00:00:00 2001 From: Dmitriy Gritsenko Date: Wed, 25 Dec 2024 14:57:23 +0500 Subject: [PATCH 02/17] Fix issue #762 --- src/Schema/AbstractSchema.php | 14 +++----------- src/Schema/SchemaInterface.php | 6 +++--- tests/Common/CommonSchemaTest.php | 14 +++++++------- tests/Db/Schema/SchemaTest.php | 8 ++++---- 4 files changed, 17 insertions(+), 25 deletions(-) diff --git a/src/Schema/AbstractSchema.php b/src/Schema/AbstractSchema.php index 5a52d9322..264c8902d 100644 --- a/src/Schema/AbstractSchema.php +++ b/src/Schema/AbstractSchema.php @@ -576,12 +576,9 @@ public function getViewNames(string $schema = '', bool $refresh = false): array } /** - * @param string $tableName The table name. - * @param string $schema The schema of the tables. - * @return bool * @throws Throwable */ - public function hasTableName(string $tableName, string $schema = ''): bool + public function hasTable(string $tableName, string $schema = ''): bool { $tables = $this->getTableNames($schema); @@ -589,11 +586,9 @@ public function hasTableName(string $tableName, string $schema = ''): bool } /** - * @param string $schema The schema name. - * @return bool * @throws Throwable */ - public function hasSchemaName(string $schema): bool + public function hasSchema(string $schema): bool { $schemas = $this->getSchemaNames(); @@ -601,12 +596,9 @@ public function hasSchemaName(string $schema): bool } /** - * @param string $viewName The view name. - * @param string $schema The schema of the views. - * @return bool * @throws Throwable */ - public function hasViewName(string $viewName, string $schema = ''): bool + public function hasView(string $viewName, string $schema = ''): bool { $views = $this->getViewNames($schema); diff --git a/src/Schema/SchemaInterface.php b/src/Schema/SchemaInterface.php index 9f05ec54d..422518564 100644 --- a/src/Schema/SchemaInterface.php +++ b/src/Schema/SchemaInterface.php @@ -425,7 +425,7 @@ public function getViewNames(string $schema = '', bool $refresh = false): array; * name. If not empty, the table will be searched in the specified schema. * @return bool Whether table exists or not */ - public function hasTableName(string $tableName, string $schema = ''): bool; + public function hasTable(string $tableName, string $schema = ''): bool; /** * Determines if a specified schema exists in the database. @@ -433,7 +433,7 @@ public function hasTableName(string $tableName, string $schema = ''): bool; * @param string $schema The table name to search for * @return bool Whether schema exists or not */ - public function hasSchemaName(string $schema): bool; + public function hasSchema(string $schema): bool; /** * Determines if a specified view exists in the database. @@ -443,5 +443,5 @@ public function hasSchemaName(string $schema): bool; * name. If not empty, the table will be searched in the specified schema. * @return bool Whether view exists or not */ - public function hasViewName(string $viewName, string $schema = ''): bool; + public function hasView(string $viewName, string $schema = ''): bool; } diff --git a/tests/Common/CommonSchemaTest.php b/tests/Common/CommonSchemaTest.php index 5e6ce718d..1541dde65 100644 --- a/tests/Common/CommonSchemaTest.php +++ b/tests/Common/CommonSchemaTest.php @@ -405,7 +405,7 @@ public function testGetTableNames(array $pdoAttributes): void $db->close(); } - public function testHasTableName(array $pdoAttributes): void + public function testHasTable(array $pdoAttributes): void { $db = $this->getConnection(true); @@ -419,9 +419,9 @@ public function testHasTableName(array $pdoAttributes): void $schema = $db->getSchema(); - $this->assertTrue($schema->hasTableName('customer')); - $this->assertTrue($schema->hasTableName('category')); - $this->assertFalse($schema->hasTableName('no_such_table')); + $this->assertTrue($schema->hasTable('customer')); + $this->assertTrue($schema->hasTable('category')); + $this->assertFalse($schema->hasTable('no_such_table')); $db->close(); } @@ -503,14 +503,14 @@ public function testGetViewNames(): void $db->close(); } - public function hasViewName(): void + public function hasView(): void { $db = $this->getConnection(true); $schema = $db->getSchema(); - $this->assertTrue($schema->hasViewName('animal_view')); - $this->assertFalse($schema->hasViewName('no_such_view')); + $this->assertTrue($schema->hasView('animal_view')); + $this->assertFalse($schema->hasView('no_such_view')); $db->close(); } diff --git a/tests/Db/Schema/SchemaTest.php b/tests/Db/Schema/SchemaTest.php index e4e7453ca..60cd54cb0 100644 --- a/tests/Db/Schema/SchemaTest.php +++ b/tests/Db/Schema/SchemaTest.php @@ -202,16 +202,16 @@ public function testGetSchemaNamesWithSchema(): void $this->assertSame(['dbo', 'public'], $schema->getSchemaNames()); } - public function testHasSchemaName(): void + public function testHasSchema(): void { $db = $this->getConnection(); $schema = $db->getSchema(); Assert::setInaccessibleProperty($schema, 'schemaNames', ['dbo', 'public']); - $this->assertTrue($schema->hasSchemaName('dbo')); - $this->assertTrue($schema->hasSchemaName('public')); - $this->assertFalse($schema->hasSchemaName('no_such_schema')); + $this->assertTrue($schema->hasSchema('dbo')); + $this->assertTrue($schema->hasSchema('public')); + $this->assertFalse($schema->hasSchema('no_such_schema')); $db->close(); } From fd253ea6f1f18ba27b5c86a3cdd9224aa017681a Mon Sep 17 00:00:00 2001 From: Dmitriy Gritsenko Date: Wed, 25 Dec 2024 14:59:03 +0500 Subject: [PATCH 03/17] Fix issue #762 --- tests/Common/CommonSchemaTest.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/Common/CommonSchemaTest.php b/tests/Common/CommonSchemaTest.php index 1541dde65..6429691b0 100644 --- a/tests/Common/CommonSchemaTest.php +++ b/tests/Common/CommonSchemaTest.php @@ -503,7 +503,7 @@ public function testGetViewNames(): void $db->close(); } - public function hasView(): void + public function testHasView(): void { $db = $this->getConnection(true); From 966e2149e2ddb06d8cde3c0490781d38f386aa5c Mon Sep 17 00:00:00 2001 From: Dmitriy Gritsenko Date: Wed, 25 Dec 2024 20:58:06 +0500 Subject: [PATCH 04/17] Fix issue #762 --- src/Schema/AbstractSchema.php | 12 ++++++------ src/Schema/SchemaInterface.php | 15 ++++++++++++--- 2 files changed, 18 insertions(+), 9 deletions(-) diff --git a/src/Schema/AbstractSchema.php b/src/Schema/AbstractSchema.php index 264c8902d..890835e1c 100644 --- a/src/Schema/AbstractSchema.php +++ b/src/Schema/AbstractSchema.php @@ -578,9 +578,9 @@ public function getViewNames(string $schema = '', bool $refresh = false): array /** * @throws Throwable */ - public function hasTable(string $tableName, string $schema = ''): bool + public function hasTable(string $tableName, string $schema = '', bool $refresh = false): bool { - $tables = $this->getTableNames($schema); + $tables = $this->getTableNames($schema, $refresh); return in_array($tableName, $tables); } @@ -588,9 +588,9 @@ public function hasTable(string $tableName, string $schema = ''): bool /** * @throws Throwable */ - public function hasSchema(string $schema): bool + public function hasSchema(string $schema, bool $refresh = false): bool { - $schemas = $this->getSchemaNames(); + $schemas = $this->getSchemaNames($refresh); return in_array($schema, $schemas); } @@ -598,9 +598,9 @@ public function hasSchema(string $schema): bool /** * @throws Throwable */ - public function hasView(string $viewName, string $schema = ''): bool + public function hasView(string $viewName, string $schema = '', bool $refresh = false): bool { - $views = $this->getViewNames($schema); + $views = $this->getViewNames($schema, $refresh); return in_array($viewName, $views); } diff --git a/src/Schema/SchemaInterface.php b/src/Schema/SchemaInterface.php index 422518564..2d4216127 100644 --- a/src/Schema/SchemaInterface.php +++ b/src/Schema/SchemaInterface.php @@ -423,17 +423,23 @@ public function getViewNames(string $schema = '', bool $refresh = false): array; * @param string $tableName The table name to search for * @param string $schema The schema of the tables. Defaults to empty string, meaning the current or default schema * name. If not empty, the table will be searched in the specified schema. + * @param bool $refresh Whether to fetch the latest available table names. If this is false, view names fetched + * before (if available) will be returned. + * * @return bool Whether table exists or not */ - public function hasTable(string $tableName, string $schema = ''): bool; + public function hasTable(string $tableName, string $schema = '', bool $refresh = false): bool; /** * Determines if a specified schema exists in the database. * * @param string $schema The table name to search for + * @param bool $refresh Whether to fetch the latest available schema names. If this is false, view names fetched + * before (if available) will be returned. + * * @return bool Whether schema exists or not */ - public function hasSchema(string $schema): bool; + public function hasSchema(string $schema, bool $refresh = false): bool; /** * Determines if a specified view exists in the database. @@ -441,7 +447,10 @@ public function hasSchema(string $schema): bool; * @param string $viewName The table name to search for * @param string $schema The schema of the tables. Defaults to empty string, meaning the current or default schema * name. If not empty, the table will be searched in the specified schema. + * @param bool $refresh Whether to fetch the latest available view names. If this is false, view names fetched + * before (if available) will be returned. + * * @return bool Whether view exists or not */ - public function hasView(string $viewName, string $schema = ''): bool; + public function hasView(string $viewName, string $schema = '', bool $refresh = false): bool; } From 7a5d737fbfe94b2e60a0914992ccd78add2ef737 Mon Sep 17 00:00:00 2001 From: Dmitriy Gritsenko Date: Thu, 26 Dec 2024 05:11:54 +0500 Subject: [PATCH 05/17] Update src/Schema/SchemaInterface.php Co-authored-by: Alexander Makarov --- src/Schema/SchemaInterface.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Schema/SchemaInterface.php b/src/Schema/SchemaInterface.php index 2d4216127..0ff924dfe 100644 --- a/src/Schema/SchemaInterface.php +++ b/src/Schema/SchemaInterface.php @@ -420,7 +420,7 @@ public function getViewNames(string $schema = '', bool $refresh = false): array; /** * Determines if a specified table exists in the database. * - * @param string $tableName The table name to search for + * @param string $tableName The table name to search for. * @param string $schema The schema of the tables. Defaults to empty string, meaning the current or default schema * name. If not empty, the table will be searched in the specified schema. * @param bool $refresh Whether to fetch the latest available table names. If this is false, view names fetched From 1e281ab1437f30a677cdbcffc872d419abdc8522 Mon Sep 17 00:00:00 2001 From: Dmitriy Gritsenko Date: Thu, 26 Dec 2024 05:26:43 +0500 Subject: [PATCH 06/17] Fix issue #762 --- src/Schema/SchemaInterface.php | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/src/Schema/SchemaInterface.php b/src/Schema/SchemaInterface.php index 0ff924dfe..febf59045 100644 --- a/src/Schema/SchemaInterface.php +++ b/src/Schema/SchemaInterface.php @@ -420,37 +420,37 @@ public function getViewNames(string $schema = '', bool $refresh = false): array; /** * Determines if a specified table exists in the database. * - * @param string $tableName The table name to search for. + * @param string $tableName The table name to search for * @param string $schema The schema of the tables. Defaults to empty string, meaning the current or default schema * name. If not empty, the table will be searched in the specified schema. * @param bool $refresh Whether to fetch the latest available table names. If this is false, view names fetched * before (if available) will be returned. * - * @return bool Whether table exists or not + * @return bool Whether table exists. */ public function hasTable(string $tableName, string $schema = '', bool $refresh = false): bool; /** * Determines if a specified schema exists in the database. * - * @param string $schema The table name to search for + * @param string $schema The schema name to search for * @param bool $refresh Whether to fetch the latest available schema names. If this is false, view names fetched * before (if available) will be returned. * - * @return bool Whether schema exists or not + * @return bool Whether schema exists. */ public function hasSchema(string $schema, bool $refresh = false): bool; /** * Determines if a specified view exists in the database. * - * @param string $viewName The table name to search for + * @param string $viewName The view name to search for * @param string $schema The schema of the tables. Defaults to empty string, meaning the current or default schema * name. If not empty, the table will be searched in the specified schema. * @param bool $refresh Whether to fetch the latest available view names. If this is false, view names fetched * before (if available) will be returned. * - * @return bool Whether view exists or not + * @return bool Whether view exists. */ public function hasView(string $viewName, string $schema = '', bool $refresh = false): bool; } From 2514ee081fa7f4eceb5c0408c318f142075bc476 Mon Sep 17 00:00:00 2001 From: Dmitriy Gritsenko Date: Sat, 4 Jan 2025 20:24:36 +0500 Subject: [PATCH 07/17] Changelog update --- tests/Common/CommonSchemaTest.php | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/tests/Common/CommonSchemaTest.php b/tests/Common/CommonSchemaTest.php index 6429691b0..cc311286b 100644 --- a/tests/Common/CommonSchemaTest.php +++ b/tests/Common/CommonSchemaTest.php @@ -405,15 +405,16 @@ public function testGetTableNames(array $pdoAttributes): void $db->close(); } + /** + * @dataProvider \Yiisoft\Db\Tests\Provider\SchemaProvider::pdoAttributes + * + * @throws NotSupportedException + */ public function testHasTable(array $pdoAttributes): void { $db = $this->getConnection(true); foreach ($pdoAttributes as $name => $value) { - if ($name === PDO::ATTR_EMULATE_PREPARES) { - continue; - } - $db->getPDO()?->setAttribute($name, $value); } From c43d104d85230c63961675ae6ef428be33ac2b2e Mon Sep 17 00:00:00 2001 From: Dmitriy Gritsenko Date: Sun, 5 Jan 2025 09:08:01 +0500 Subject: [PATCH 08/17] Removed redundant PHPDoc block --- tests/Common/CommonSchemaTest.php | 5 ----- 1 file changed, 5 deletions(-) diff --git a/tests/Common/CommonSchemaTest.php b/tests/Common/CommonSchemaTest.php index cc311286b..737f2f478 100644 --- a/tests/Common/CommonSchemaTest.php +++ b/tests/Common/CommonSchemaTest.php @@ -405,11 +405,6 @@ public function testGetTableNames(array $pdoAttributes): void $db->close(); } - /** - * @dataProvider \Yiisoft\Db\Tests\Provider\SchemaProvider::pdoAttributes - * - * @throws NotSupportedException - */ public function testHasTable(array $pdoAttributes): void { $db = $this->getConnection(true); From 64f818c651c2ded43b3f71d230899e655f160c39 Mon Sep 17 00:00:00 2001 From: Dmitriy Gritsenko Date: Sun, 5 Jan 2025 09:15:01 +0500 Subject: [PATCH 09/17] Returned PHPDoc block to fix automated tests --- tests/Common/CommonSchemaTest.php | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/tests/Common/CommonSchemaTest.php b/tests/Common/CommonSchemaTest.php index 737f2f478..cc311286b 100644 --- a/tests/Common/CommonSchemaTest.php +++ b/tests/Common/CommonSchemaTest.php @@ -405,6 +405,11 @@ public function testGetTableNames(array $pdoAttributes): void $db->close(); } + /** + * @dataProvider \Yiisoft\Db\Tests\Provider\SchemaProvider::pdoAttributes + * + * @throws NotSupportedException + */ public function testHasTable(array $pdoAttributes): void { $db = $this->getConnection(true); From 219c7c6f6970224df848dd80f28aecf243c4e821 Mon Sep 17 00:00:00 2001 From: Sergei Tigrov Date: Sun, 5 Jan 2025 11:18:35 +0700 Subject: [PATCH 10/17] Update tests/Common/CommonSchemaTest.php --- tests/Common/CommonSchemaTest.php | 11 +---------- 1 file changed, 1 insertion(+), 10 deletions(-) diff --git a/tests/Common/CommonSchemaTest.php b/tests/Common/CommonSchemaTest.php index cc311286b..03b877e92 100644 --- a/tests/Common/CommonSchemaTest.php +++ b/tests/Common/CommonSchemaTest.php @@ -405,19 +405,10 @@ public function testGetTableNames(array $pdoAttributes): void $db->close(); } - /** - * @dataProvider \Yiisoft\Db\Tests\Provider\SchemaProvider::pdoAttributes - * - * @throws NotSupportedException - */ - public function testHasTable(array $pdoAttributes): void + public function testHasTable(): void { $db = $this->getConnection(true); - foreach ($pdoAttributes as $name => $value) { - $db->getPDO()?->setAttribute($name, $value); - } - $schema = $db->getSchema(); $this->assertTrue($schema->hasTable('customer')); From 23c9235728139a5288f24e1f50efe7376539919b Mon Sep 17 00:00:00 2001 From: Dmitriy Gritsenko Date: Sun, 5 Jan 2025 09:20:42 +0500 Subject: [PATCH 11/17] Removed redundant PHPDoc block --- tests/Common/CommonSchemaTest.php | 11 +---------- 1 file changed, 1 insertion(+), 10 deletions(-) diff --git a/tests/Common/CommonSchemaTest.php b/tests/Common/CommonSchemaTest.php index cc311286b..03b877e92 100644 --- a/tests/Common/CommonSchemaTest.php +++ b/tests/Common/CommonSchemaTest.php @@ -405,19 +405,10 @@ public function testGetTableNames(array $pdoAttributes): void $db->close(); } - /** - * @dataProvider \Yiisoft\Db\Tests\Provider\SchemaProvider::pdoAttributes - * - * @throws NotSupportedException - */ - public function testHasTable(array $pdoAttributes): void + public function testHasTable(): void { $db = $this->getConnection(true); - foreach ($pdoAttributes as $name => $value) { - $db->getPDO()?->setAttribute($name, $value); - } - $schema = $db->getSchema(); $this->assertTrue($schema->hasTable('customer')); From 028baf80199bc2c79ab6740aa5a15cff62e38b3a Mon Sep 17 00:00:00 2001 From: Dmitriy Gritsenko Date: Sun, 5 Jan 2025 10:45:40 +0500 Subject: [PATCH 12/17] Fixed hasSchema tests --- tests/Common/CommonSchemaTest.php | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/tests/Common/CommonSchemaTest.php b/tests/Common/CommonSchemaTest.php index 03b877e92..a91ec4f69 100644 --- a/tests/Common/CommonSchemaTest.php +++ b/tests/Common/CommonSchemaTest.php @@ -415,6 +415,10 @@ public function testHasTable(): void $this->assertTrue($schema->hasTable('category')); $this->assertFalse($schema->hasTable('no_such_table')); + $this->assertTrue($schema->hasTable('customer', '', true)); + $this->assertTrue($schema->hasTable('category', '', true)); + $this->assertFalse($schema->hasTable('no_such_table', '', true)); + $db->close(); } @@ -504,6 +508,9 @@ public function testHasView(): void $this->assertTrue($schema->hasView('animal_view')); $this->assertFalse($schema->hasView('no_such_view')); + $this->assertTrue($schema->hasView('animal_view', '', true)); + $this->assertFalse($schema->hasView('no_such_view', '', true)); + $db->close(); } From e42d4b67dd4d9be428173f46257fe74abc24b933 Mon Sep 17 00:00:00 2001 From: Dmitriy Gritsenko Date: Sun, 5 Jan 2025 11:11:33 +0500 Subject: [PATCH 13/17] Fixed hasSchema tests & formatting --- CHANGELOG.md | 1 + src/Schema/SchemaInterface.php | 10 +++++----- tests/Common/CommonSchemaTest.php | 9 +++++++++ 3 files changed, 15 insertions(+), 5 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 1f488b79a..a5f9bd28e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,7 @@ ## 2.0.0 under development +- Enh #762: Add methods `SchemaInterface::hasSchema()`, `SchemaInterface::hasTable()`, `SchemaInterface::hasView()` (@evil1) - Enh #820: Support `Traversable` values for `AbstractDMLQueryBuilder::batchInsert()` method with empty columns (@Tigrov) - Enh #815: Refactor `Query::column()` method (@Tigrov) - Enh #816: Allow scalar values for `$columns` parameter of `Query::select()` and `Query::addSelect()` methods (@Tigrov) diff --git a/src/Schema/SchemaInterface.php b/src/Schema/SchemaInterface.php index febf59045..ca64365e9 100644 --- a/src/Schema/SchemaInterface.php +++ b/src/Schema/SchemaInterface.php @@ -422,9 +422,9 @@ public function getViewNames(string $schema = '', bool $refresh = false): array; * * @param string $tableName The table name to search for * @param string $schema The schema of the tables. Defaults to empty string, meaning the current or default schema - * name. If not empty, the table will be searched in the specified schema. + * name. If not empty, the table will be searched in the specified schema. * @param bool $refresh Whether to fetch the latest available table names. If this is false, view names fetched - * before (if available) will be returned. + * before (if available) will be returned. * * @return bool Whether table exists. */ @@ -435,7 +435,7 @@ public function hasTable(string $tableName, string $schema = '', bool $refresh = * * @param string $schema The schema name to search for * @param bool $refresh Whether to fetch the latest available schema names. If this is false, view names fetched - * before (if available) will be returned. + * before (if available) will be returned. * * @return bool Whether schema exists. */ @@ -446,9 +446,9 @@ public function hasSchema(string $schema, bool $refresh = false): bool; * * @param string $viewName The view name to search for * @param string $schema The schema of the tables. Defaults to empty string, meaning the current or default schema - * name. If not empty, the table will be searched in the specified schema. + * name. If not empty, the table will be searched in the specified schema. * @param bool $refresh Whether to fetch the latest available view names. If this is false, view names fetched - * before (if available) will be returned. + * before (if available) will be returned. * * @return bool Whether view exists. */ diff --git a/tests/Common/CommonSchemaTest.php b/tests/Common/CommonSchemaTest.php index a91ec4f69..1c321da3b 100644 --- a/tests/Common/CommonSchemaTest.php +++ b/tests/Common/CommonSchemaTest.php @@ -419,6 +419,11 @@ public function testHasTable(): void $this->assertTrue($schema->hasTable('category', '', true)); $this->assertFalse($schema->hasTable('no_such_table', '', true)); + if ($db->getDriverName() !== 'sqlite') { + $this->assertFalse($schema->hasTable('customer', 'no_such_schema', true)); + $this->assertFalse($schema->hasTable('category', 'no_such_schema', true)); + } + $db->close(); } @@ -511,6 +516,10 @@ public function testHasView(): void $this->assertTrue($schema->hasView('animal_view', '', true)); $this->assertFalse($schema->hasView('no_such_view', '', true)); + if ($db->getDriverName() !== 'sqlite') { + $this->assertFalse($schema->hasView('animal_view', 'no_such_schema', true)); + } + $db->close(); } From c19ffde8db96a2b31584f2bba0a4ae4d29ebc19c Mon Sep 17 00:00:00 2001 From: Dmitriy Gritsenko Date: Sun, 5 Jan 2025 11:15:09 +0500 Subject: [PATCH 14/17] Fixed hasSchema tests & formatting --- CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index a5f9bd28e..f003da6fc 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,7 +2,7 @@ ## 2.0.0 under development -- Enh #762: Add methods `SchemaInterface::hasSchema()`, `SchemaInterface::hasTable()`, `SchemaInterface::hasView()` (@evil1) +- Enh #913: Add methods `SchemaInterface::hasSchema()`, `SchemaInterface::hasTable()`, `SchemaInterface::hasView()` (@evil1) - Enh #820: Support `Traversable` values for `AbstractDMLQueryBuilder::batchInsert()` method with empty columns (@Tigrov) - Enh #815: Refactor `Query::column()` method (@Tigrov) - Enh #816: Allow scalar values for `$columns` parameter of `Query::select()` and `Query::addSelect()` methods (@Tigrov) From 3203b8b263b725e4bcef4a0f9d22a4011214d0c5 Mon Sep 17 00:00:00 2001 From: Dmitriy Gritsenko Date: Sun, 5 Jan 2025 11:19:48 +0500 Subject: [PATCH 15/17] Fixed hasSchema tests & formatting --- CHANGELOG.md | 2 +- tests/Common/CommonSchemaTest.php | 9 --------- 2 files changed, 1 insertion(+), 10 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index f003da6fc..acfffb5ce 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,7 +2,7 @@ ## 2.0.0 under development -- Enh #913: Add methods `SchemaInterface::hasSchema()`, `SchemaInterface::hasTable()`, `SchemaInterface::hasView()` (@evil1) +- New #913: Add methods `SchemaInterface::hasSchema()`, `SchemaInterface::hasTable()`, `SchemaInterface::hasView()` (@evil1) - Enh #820: Support `Traversable` values for `AbstractDMLQueryBuilder::batchInsert()` method with empty columns (@Tigrov) - Enh #815: Refactor `Query::column()` method (@Tigrov) - Enh #816: Allow scalar values for `$columns` parameter of `Query::select()` and `Query::addSelect()` methods (@Tigrov) diff --git a/tests/Common/CommonSchemaTest.php b/tests/Common/CommonSchemaTest.php index 1c321da3b..a91ec4f69 100644 --- a/tests/Common/CommonSchemaTest.php +++ b/tests/Common/CommonSchemaTest.php @@ -419,11 +419,6 @@ public function testHasTable(): void $this->assertTrue($schema->hasTable('category', '', true)); $this->assertFalse($schema->hasTable('no_such_table', '', true)); - if ($db->getDriverName() !== 'sqlite') { - $this->assertFalse($schema->hasTable('customer', 'no_such_schema', true)); - $this->assertFalse($schema->hasTable('category', 'no_such_schema', true)); - } - $db->close(); } @@ -516,10 +511,6 @@ public function testHasView(): void $this->assertTrue($schema->hasView('animal_view', '', true)); $this->assertFalse($schema->hasView('no_such_view', '', true)); - if ($db->getDriverName() !== 'sqlite') { - $this->assertFalse($schema->hasView('animal_view', 'no_such_schema', true)); - } - $db->close(); } From 4d6074fee608ef93a1b9a16971d278acfc7126f4 Mon Sep 17 00:00:00 2001 From: Dmitriy Gritsenko Date: Sun, 5 Jan 2025 21:36:32 +0500 Subject: [PATCH 16/17] Fixed hasView and hasTable tests tests --- src/Query/AbstractQueryStatement.php | 35 ++++++++++++++++++++++++++++ tests/Common/CommonSchemaTest.php | 12 ++++++---- 2 files changed, 42 insertions(+), 5 deletions(-) create mode 100644 src/Query/AbstractQueryStatement.php diff --git a/src/Query/AbstractQueryStatement.php b/src/Query/AbstractQueryStatement.php new file mode 100644 index 000000000..d8e1d361d --- /dev/null +++ b/src/Query/AbstractQueryStatement.php @@ -0,0 +1,35 @@ +sql; + } + + public function setSql(string $sql): static + { + $this->sql = $this->getQueryBuilder()->quoter()->quoteSql($sql); + return $this; + } +} diff --git a/tests/Common/CommonSchemaTest.php b/tests/Common/CommonSchemaTest.php index a91ec4f69..7c1453f56 100644 --- a/tests/Common/CommonSchemaTest.php +++ b/tests/Common/CommonSchemaTest.php @@ -415,9 +415,9 @@ public function testHasTable(): void $this->assertTrue($schema->hasTable('category')); $this->assertFalse($schema->hasTable('no_such_table')); - $this->assertTrue($schema->hasTable('customer', '', true)); - $this->assertTrue($schema->hasTable('category', '', true)); - $this->assertFalse($schema->hasTable('no_such_table', '', true)); + $db->createCommand()->dropTable('customer')->execute(); + + $this->assertFalse($schema->hasTable('customer', '', true)); $db->close(); } @@ -508,8 +508,10 @@ public function testHasView(): void $this->assertTrue($schema->hasView('animal_view')); $this->assertFalse($schema->hasView('no_such_view')); - $this->assertTrue($schema->hasView('animal_view', '', true)); - $this->assertFalse($schema->hasView('no_such_view', '', true)); + $db->createCommand()->dropView('animal_view')->execute(); + + $this->assertTrue($schema->hasView('animal_view')); + $this->assertFalse($schema->hasView('animal_view', '', true)); $db->close(); } From 0beb7a20d08292aaeef69001c2f5fd9a3ff865bc Mon Sep 17 00:00:00 2001 From: Dmitriy Gritsenko Date: Tue, 7 Jan 2025 21:09:16 +0500 Subject: [PATCH 17/17] Removed accidental file from the commit --- src/Query/AbstractQueryStatement.php | 35 ---------------------------- 1 file changed, 35 deletions(-) delete mode 100644 src/Query/AbstractQueryStatement.php diff --git a/src/Query/AbstractQueryStatement.php b/src/Query/AbstractQueryStatement.php deleted file mode 100644 index d8e1d361d..000000000 --- a/src/Query/AbstractQueryStatement.php +++ /dev/null @@ -1,35 +0,0 @@ -sql; - } - - public function setSql(string $sql): static - { - $this->sql = $this->getQueryBuilder()->quoter()->quoteSql($sql); - return $this; - } -}