diff --git a/src/Schema/MySQLSchemaManager.php b/src/Schema/MySQLSchemaManager.php index e581926faf..8468dce639 100644 --- a/src/Schema/MySQLSchemaManager.php +++ b/src/Schema/MySQLSchemaManager.php @@ -332,7 +332,7 @@ public function createComparator(/* ComparatorConfig $config = new ComparatorCon protected function selectTableNames(string $databaseName): Result { $sql = <<<'SQL' -SELECT TABLE_NAME +SELECT CONCAT(TABLE_SCHEMA, '.', TABLE_NAME) AS TABLE_NAME FROM information_schema.TABLES WHERE TABLE_SCHEMA = ? AND TABLE_TYPE = 'BASE TABLE' @@ -358,7 +358,7 @@ protected function selectTableColumns(string $databaseName, ?string $tableName = $sql = sprintf( <<<'SQL' SELECT - c.TABLE_NAME, + CONCAT(c.TABLE_SCHEMA, '.', c.TABLE_NAME) AS TABLE_NAME, c.COLUMN_NAME AS field, %s AS type, c.COLUMN_TYPE, @@ -401,7 +401,7 @@ protected function selectIndexColumns(string $databaseName, ?string $tableName = $sql = sprintf( <<<'SQL' SELECT - TABLE_NAME, + CONCAT(TABLE_SCHEMA, '.', TABLE_NAME) AS TABLE_NAME, NON_UNIQUE AS Non_Unique, INDEX_NAME AS Key_name, COLUMN_NAME AS Column_Name, @@ -434,7 +434,7 @@ protected function selectForeignKeyColumns(string $databaseName, ?string $tableN $sql = sprintf( <<<'SQL' SELECT - k.TABLE_NAME, + CONCAT(k.TABLE_SCHEMA, '.', k.TABLE_NAME) AS TABLE_NAME, k.CONSTRAINT_NAME, k.COLUMN_NAME, k.REFERENCED_TABLE_NAME, diff --git a/tests/Functional/LockMode/NoneTest.php b/tests/Functional/LockMode/NoneTest.php index c54597a86c..b9571f3e46 100644 --- a/tests/Functional/LockMode/NoneTest.php +++ b/tests/Functional/LockMode/NoneTest.php @@ -8,6 +8,7 @@ use Doctrine\DBAL\DriverManager; use Doctrine\DBAL\Exception; use Doctrine\DBAL\LockMode; +use Doctrine\DBAL\Platforms\AbstractMySQLPlatform; use Doctrine\DBAL\Platforms\SQLitePlatform; use Doctrine\DBAL\Platforms\SQLServerPlatform; use Doctrine\DBAL\Schema\Column; @@ -60,7 +61,13 @@ public function setUp(): void $this->connection2 = DriverManager::getConnection($params); - if ($this->connection2->createSchemaManager()->tableExists('users')) { + if ( + $this->connection2->createSchemaManager()->tableExists( + $this->connection2->getDatabasePlatform() instanceof AbstractMySQLPlatform + ? 'doctrine_tests.users' + : 'users', + ) + ) { return; } diff --git a/tests/Functional/NamedParametersTest.php b/tests/Functional/NamedParametersTest.php index a7e67c4dff..eb2eb0e84a 100644 --- a/tests/Functional/NamedParametersTest.php +++ b/tests/Functional/NamedParametersTest.php @@ -7,6 +7,7 @@ use Doctrine\DBAL\ArrayParameterType; use Doctrine\DBAL\Connection; use Doctrine\DBAL\ParameterType; +use Doctrine\DBAL\Platforms\AbstractMySQLPlatform; use Doctrine\DBAL\Schema\Column; use Doctrine\DBAL\Schema\PrimaryKeyConstraint; use Doctrine\DBAL\Schema\Table; @@ -163,7 +164,13 @@ public static function ticketProvider(): iterable protected function setUp(): void { - if ($this->connection->createSchemaManager()->tableExists('ddc1372_foobar')) { + if ( + $this->connection->createSchemaManager()->tableExists( + $this->connection->getDatabasePlatform() instanceof AbstractMySQLPlatform + ? 'doctrine_tests.ddc1372_foobar' + : 'ddc1372_foobar', + ) + ) { return; } diff --git a/tests/Functional/Schema/MySQLSchemaManagerTest.php b/tests/Functional/Schema/MySQLSchemaManagerTest.php index eb5dad7fc8..be4d4d3063 100644 --- a/tests/Functional/Schema/MySQLSchemaManagerTest.php +++ b/tests/Functional/Schema/MySQLSchemaManagerTest.php @@ -875,6 +875,84 @@ public function testSchemaDiffWithCustomColumnTypeWhileDatabaseTypeDiffers(): vo ); } + public function testListTables(): void + { + $this->createTestTable('list_tables_test'); + $tables = $this->schemaManager->listTables(); + + $table = $this->findTableByName($tables, 'doctrine_tests.list_tables_test'); + self::assertNotNull($table); + + self::assertTrue($table->hasColumn('id')); + self::assertTrue($table->hasColumn('test')); + self::assertTrue($table->hasColumn('foreign_key_test')); + } + + /** @return iterable */ + public static function tableFilterProvider(): iterable + { + yield 'One table' => ['doctrine_tests.filter_test_1', 1]; + yield 'Two tables' => ['doctrine_tests.filter_test_', 2]; + } + + public function testRenameTable(): void + { + $this->createTestTable('old_name'); + $this->schemaManager->renameTable('old_name', 'new_name'); + + self::assertFalse($this->schemaManager->tablesExist(['doctrine_tests.old_name'])); + self::assertTrue($this->schemaManager->tablesExist(['doctrine_tests.new_name'])); + } + + public function testSchemaIntrospection(): void + { + $this->createTestTable('test_table'); + + $schema = $this->schemaManager->introspectSchema(); + self::assertTrue($schema->hasTable('doctrine_tests.test_table')); + } + + public function testMigrateSchema(): void + { + $this->createTestTable('table_to_alter'); + $this->createTestTable('table_to_drop'); + + $schema = $this->schemaManager->introspectSchema(); + + $tableToAlter = $schema->getTable('doctrine_tests.table_to_alter'); + $tableToAlter->dropColumn('foreign_key_test'); + $tableToAlter->addColumn('number', Types::INTEGER); + + $schema->dropTable('doctrine_tests.table_to_drop'); + + $tableToCreate = $schema->createTable('table_to_create'); + $tableToCreate->addColumn('id', Types::INTEGER, ['notnull' => true]); + $tableToCreate->setPrimaryKey(['id']); + + $this->schemaManager->migrateSchema($schema); + + $schema = $this->schemaManager->introspectSchema(); + + self::assertTrue($schema->hasTable('doctrine_tests.table_to_alter')); + self::assertFalse($schema->getTable('doctrine_tests.table_to_alter')->hasColumn('foreign_key_test')); + self::assertTrue($schema->getTable('doctrine_tests.table_to_alter')->hasColumn('number')); + self::assertFalse($schema->hasTable('doctrine_tests.table_to_drop')); + self::assertTrue($schema->hasTable('doctrine_tests.table_to_create')); + } + + public function testIntrospectReservedKeywordTableViaListTables(): void + { + $this->createReservedKeywordTables(); + + $tables = $this->schemaManager->listTables(); + + $user = $this->findTableByName($tables, 'doctrine_tests.user'); + self::assertNotNull($user); + self::assertCount(2, $user->getColumns()); + self::assertCount(2, $user->getIndexes()); + self::assertCount(1, $user->getForeignKeys()); + } + public function getExpectedDefaultSchemaName(): ?string { return null; diff --git a/tests/Functional/Schema/SchemaManagerFunctionalTestCase.php b/tests/Functional/Schema/SchemaManagerFunctionalTestCase.php index dbfe5071cd..9328d4e5fb 100644 --- a/tests/Functional/Schema/SchemaManagerFunctionalTestCase.php +++ b/tests/Functional/Schema/SchemaManagerFunctionalTestCase.php @@ -1562,7 +1562,7 @@ public function testIntrospectReservedKeywordTableViaListTables(): void self::assertCount(1, $user->getForeignKeys()); } - private function createReservedKeywordTables(): void + protected function createReservedKeywordTables(): void { $platform = $this->connection->getDatabasePlatform();