Skip to content

Commit

Permalink
[TASK] Use Doctrine-DBAL based API for database communication
Browse files Browse the repository at this point in the history
  • Loading branch information
chetan-thapliyal committed Jun 24, 2020
1 parent 5d6bc2b commit 60e0d59
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 22 deletions.
11 changes: 8 additions & 3 deletions Classes/Command/T3DeployCommandController.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
/***************************************************************
* Copyright notice
*
* (c) 2019 AOE GmbH <dev@aoe.com>
* (c) 2020 AOE GmbH <dev@aoe.com>
*
* All rights reserved
*
Expand All @@ -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;
Expand Down Expand Up @@ -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
);
}
}

Expand Down
13 changes: 10 additions & 3 deletions Classes/class.tx_t3deploy_databaseController.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
/***************************************************************
* Copyright notice
*
* (c) 2019 AOE GmbH <dev@aoe.com>
* (c) 2020 AOE GmbH <dev@aoe.com>
*
* All rights reserved
*
Expand Down Expand Up @@ -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
);
}
}

Expand Down
68 changes: 54 additions & 14 deletions Tests/Functional/DatabaseControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
/***************************************************************
* Copyright notice
*
* (c) 2019 AOE GmbH <dev@aoe.com>
* (c) 2020 AOE GmbH <dev@aoe.com>
*
* All rights reserved
*
Expand All @@ -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;

Expand Down Expand Up @@ -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);
Expand All @@ -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);
Expand All @@ -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:
Expand All @@ -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:
Expand All @@ -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:
Expand Down Expand Up @@ -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);
Expand All @@ -241,4 +253,32 @@ public function doesUpdateStructureActionDumpChangesToFile()
// Assert that dump result is reported:
$this->assertNotEmpty($result);
}
}

/**
* @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;
}
}
4 changes: 2 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": {
Expand Down

0 comments on commit 60e0d59

Please # to comment.