From 60e0d59438a1ea04f2e2b2593394449b12b768e2 Mon Sep 17 00:00:00 2001 From: Chetan Thapliyal Date: Wed, 24 Jun 2020 17:40:47 +0200 Subject: [PATCH] [TASK] Use Doctrine-DBAL based API for database communication --- Classes/Command/T3DeployCommandController.php | 11 ++- .../class.tx_t3deploy_databaseController.php | 13 +++- Tests/Functional/DatabaseControllerTest.php | 68 +++++++++++++++---- composer.json | 4 +- 4 files changed, 74 insertions(+), 22 deletions(-) diff --git a/Classes/Command/T3DeployCommandController.php b/Classes/Command/T3DeployCommandController.php index 49220a7..c2cd040 100644 --- a/Classes/Command/T3DeployCommandController.php +++ b/Classes/Command/T3DeployCommandController.php @@ -4,7 +4,7 @@ /*************************************************************** * Copyright notice * - * (c) 2019 AOE GmbH + * (c) 2020 AOE GmbH * * All rights reserved * @@ -26,6 +26,7 @@ ***************************************************************/ use AOE\T3Deploy\Utility\SqlStatementUtility; +use RuntimeException; use TYPO3\CMS\Core\Category\CategoryRegistry; use TYPO3\CMS\Core\Utility\GeneralUtility; use TYPO3\CMS\Extbase\Mvc\Controller\CommandController; @@ -239,8 +240,12 @@ private function executeUpdateStructure($arguments, $allowKeyModifications = fal $statements = SqlStatementUtility::sortStatements($statements); if ($isExecuteEnabled) { - foreach ($statements as $statement) { - $GLOBALS['TYPO3_DB']->admin_query($statement); + $res = $this->schemaMigrationService->performUpdateQueries($statements, array_keys($statements)); + if (is_array($res)) { + throw new RuntimeException( + 'Database operation failure' . PHP_EOL . print_r($res, true) . PHP_EOL, + 1592990670 + ); } } diff --git a/Classes/class.tx_t3deploy_databaseController.php b/Classes/class.tx_t3deploy_databaseController.php index 5eff19e..f3d850e 100644 --- a/Classes/class.tx_t3deploy_databaseController.php +++ b/Classes/class.tx_t3deploy_databaseController.php @@ -2,7 +2,7 @@ /*************************************************************** * Copyright notice * - * (c) 2019 AOE GmbH + * (c) 2020 AOE GmbH * * All rights reserved * @@ -244,8 +244,15 @@ protected function executeUpdateStructure(array $arguments, $allowKeyModificatio $statements = $this->sortStatements($statements); if ($isExecuteEnabled) { - foreach ($statements as $statement) { - $GLOBALS['TYPO3_DB']->admin_query($statement); + $res = $this->schemaMigrationService->performUpdateQueries( + $statements, + array_fill_keys(array_keys($statements), true) + ); + if (is_array($res)) { + throw new RuntimeException( + 'Database operation failure' . PHP_EOL . print_r($res, true) . PHP_EOL, + 1592989007 + ); } } diff --git a/Tests/Functional/DatabaseControllerTest.php b/Tests/Functional/DatabaseControllerTest.php index 9b21096..26b4137 100644 --- a/Tests/Functional/DatabaseControllerTest.php +++ b/Tests/Functional/DatabaseControllerTest.php @@ -2,7 +2,7 @@ /*************************************************************** * Copyright notice * - * (c) 2019 AOE GmbH + * (c) 2020 AOE GmbH * * All rights reserved * @@ -23,7 +23,11 @@ * This copyright notice MUST APPEAR in all copies of the script! ***************************************************************/ +use Doctrine\DBAL\Schema\Column; +use Doctrine\DBAL\Types\Type as ColumnTypes; use Nimut\TestingFramework\TestCase\FunctionalTestCase; +use TYPO3\CMS\Core\Database\Connection; +use TYPO3\CMS\Core\Database\ConnectionPool; use TYPO3\CMS\Core\Utility\GeneralUtility; use TYPO3\CMS\Install\Service\SqlExpectedSchemaService; @@ -98,10 +102,12 @@ public function doesUpdateStructureActionReportChanges() $result = $this->controller->updateStructureAction($arguments); // Assert that nothing has been created, this is just for reporting: - $tables = $GLOBALS['TYPO3_DB']->admin_get_tables(); - $pagesFields = $GLOBALS['TYPO3_DB']->admin_get_fields('pages'); + $tables = $this->getTables(); + $pagesFields = $this->getTableColumns('pages'); + $this->assertFalse(isset($tables['tx_testextension_test'])); - $this->assertNotEquals('varchar(255)', strtolower($pagesFields['alias']['Type'])); + $this->assertEquals(ColumnTypes::STRING, $pagesFields['alias']->getType()->getName()); + $this->assertEquals(33, $pagesFields['alias']->getLength()); // Assert that changes are reported: $this->assertContains('ALTER TABLE pages ADD tx_testextension_field_test', $result); @@ -122,12 +128,14 @@ public function doesUpdateStructureActionExecuteChanges() $result = $this->controller->updateStructureAction($arguments); // Assert that tables have been created: - $tables = $GLOBALS['TYPO3_DB']->admin_get_tables(); - $pagesFields = $GLOBALS['TYPO3_DB']->admin_get_fields('pages'); + $tables = $this->getTables(); + $pagesFields = $this->getTableColumns('pages'); + $this->assertTrue(isset($tables['tx_testextension'])); $this->assertTrue(isset($tables['tx_testextension_test'])); $this->assertTrue(isset($pagesFields['tx_testextension_field_test'])); - $this->assertEquals('varchar(255)', strtolower($pagesFields['alias']['Type'])); + $this->assertEquals(ColumnTypes::STRING, $pagesFields['alias']->getType()->getName()); + $this->assertEquals(255, $pagesFields['alias']->getLength()); // Assert that nothing is reported we just want to execute: $this->assertEmpty($result); @@ -149,9 +157,10 @@ public function doesUpdateStructureActionReportRemovals() $result = $this->controller->updateStructureAction($arguments); // Assert that nothing has been removed, this is just for reporting: - $tables = $GLOBALS['TYPO3_DB']->admin_get_tables(); + $tables = $this->getTables(); $this->assertTrue(isset($tables['tx_testextension'])); - $pagesFields = $GLOBALS['TYPO3_DB']->admin_get_fields('pages'); + + $pagesFields = $this->getTableColumns('pages'); $this->assertTrue(isset($pagesFields['tx_testextension_field'])); // Assert that removals are reported: @@ -175,9 +184,10 @@ public function doesUpdateStructureActionExecuteRemovals() $result = $this->controller->updateStructureAction($arguments); // Assert that tables and columns have been removed: - $tables = $GLOBALS['TYPO3_DB']->admin_get_tables(); + $tables = $this->getTables(); $this->assertFalse(isset($tables['tx_testextension'])); - $pagesFields = $GLOBALS['TYPO3_DB']->admin_get_fields('pages'); + + $pagesFields = $this->getTableColumns('pages'); $this->assertFalse(isset($pagesFields['tx_testextension_field'])); // Assert that nothing is reported we just want to execute: @@ -200,9 +210,10 @@ public function doesUpdateStructureActionReportDropKeys() $result = $this->controller->updateStructureAction($arguments); // Assert that nothing has been removed, this is just for reporting: - $tables = $GLOBALS['TYPO3_DB']->admin_get_tables(); + $tables = $this->getTables(); $this->assertTrue(isset($tables['tx_testextension'])); - $pagesFields = $GLOBALS['TYPO3_DB']->admin_get_fields('pages'); + + $pagesFields = $this->getTableColumns('pages'); $this->assertTrue(isset($pagesFields['tx_testextension_field'])); // Assert that removals are reported: @@ -233,6 +244,7 @@ public function doesUpdateStructureActionDumpChangesToFile() $this->assertFileExists($testDumpFile); $testDumpFileContent = file_get_contents($testDumpFile); + // Assert that changes are dumped: $this->assertContains('ALTER TABLE pages ADD tx_testextension_field_test', $testDumpFileContent); $this->assertContains('ALTER TABLE pages CHANGE alias alias varchar(255)', $testDumpFileContent); @@ -241,4 +253,32 @@ public function doesUpdateStructureActionDumpChangesToFile() // Assert that dump result is reported: $this->assertNotEmpty($result); } -} \ No newline at end of file + + /** + * @return string[] + */ + private function getTables() + { + /** @var Connection $connection */ + $connection = GeneralUtility::makeInstance(ConnectionPool::class)->getConnectionByName('Default'); + $tables = $connection->getSchemaManager()->listTableNames(); + + return array_flip($tables); + } + + /** + * @param string $table + * @return Column[] + */ + private function getTableColumns(string $table) + { + /** @var Connection $connection */ + $connection = GeneralUtility::makeInstance(ConnectionPool::class)->getConnectionByName('Default'); + $tableColumns = []; + foreach ($connection->getSchemaManager()->listTableColumns($table) as $column) { + $tableColumns[$column->getName()] = $column; + } + + return $tableColumns; + } +} diff --git a/composer.json b/composer.json index d1989e1..64f55bf 100644 --- a/composer.json +++ b/composer.json @@ -15,8 +15,8 @@ "helhum/typo3-console": ">=4.0" }, "require-dev": { - "typo3/cms": "^7.6", - "nimut/testing-framework": "^4.1" + "typo3/cms": "^8.7", + "nimut/testing-framework": "^5.0" }, "autoload": { "psr-4": {