Skip to content

Commit 60e0d59

Browse files
[TASK] Use Doctrine-DBAL based API for database communication
1 parent 5d6bc2b commit 60e0d59

File tree

4 files changed

+74
-22
lines changed

4 files changed

+74
-22
lines changed

Classes/Command/T3DeployCommandController.php

+8-3
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
/***************************************************************
55
* Copyright notice
66
*
7-
* (c) 2019 AOE GmbH <[email protected]>
7+
* (c) 2020 AOE GmbH <[email protected]>
88
*
99
* All rights reserved
1010
*
@@ -26,6 +26,7 @@
2626
***************************************************************/
2727

2828
use AOE\T3Deploy\Utility\SqlStatementUtility;
29+
use RuntimeException;
2930
use TYPO3\CMS\Core\Category\CategoryRegistry;
3031
use TYPO3\CMS\Core\Utility\GeneralUtility;
3132
use TYPO3\CMS\Extbase\Mvc\Controller\CommandController;
@@ -239,8 +240,12 @@ private function executeUpdateStructure($arguments, $allowKeyModifications = fal
239240
$statements = SqlStatementUtility::sortStatements($statements);
240241

241242
if ($isExecuteEnabled) {
242-
foreach ($statements as $statement) {
243-
$GLOBALS['TYPO3_DB']->admin_query($statement);
243+
$res = $this->schemaMigrationService->performUpdateQueries($statements, array_keys($statements));
244+
if (is_array($res)) {
245+
throw new RuntimeException(
246+
'Database operation failure' . PHP_EOL . print_r($res, true) . PHP_EOL,
247+
1592990670
248+
);
244249
}
245250
}
246251

Classes/class.tx_t3deploy_databaseController.php

+10-3
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
/***************************************************************
33
* Copyright notice
44
*
5-
* (c) 2019 AOE GmbH <[email protected]>
5+
* (c) 2020 AOE GmbH <[email protected]>
66
*
77
* All rights reserved
88
*
@@ -244,8 +244,15 @@ protected function executeUpdateStructure(array $arguments, $allowKeyModificatio
244244
$statements = $this->sortStatements($statements);
245245

246246
if ($isExecuteEnabled) {
247-
foreach ($statements as $statement) {
248-
$GLOBALS['TYPO3_DB']->admin_query($statement);
247+
$res = $this->schemaMigrationService->performUpdateQueries(
248+
$statements,
249+
array_fill_keys(array_keys($statements), true)
250+
);
251+
if (is_array($res)) {
252+
throw new RuntimeException(
253+
'Database operation failure' . PHP_EOL . print_r($res, true) . PHP_EOL,
254+
1592989007
255+
);
249256
}
250257
}
251258

Tests/Functional/DatabaseControllerTest.php

+54-14
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
/***************************************************************
33
* Copyright notice
44
*
5-
* (c) 2019 AOE GmbH <[email protected]>
5+
* (c) 2020 AOE GmbH <[email protected]>
66
*
77
* All rights reserved
88
*
@@ -23,7 +23,11 @@
2323
* This copyright notice MUST APPEAR in all copies of the script!
2424
***************************************************************/
2525

26+
use Doctrine\DBAL\Schema\Column;
27+
use Doctrine\DBAL\Types\Type as ColumnTypes;
2628
use Nimut\TestingFramework\TestCase\FunctionalTestCase;
29+
use TYPO3\CMS\Core\Database\Connection;
30+
use TYPO3\CMS\Core\Database\ConnectionPool;
2731
use TYPO3\CMS\Core\Utility\GeneralUtility;
2832
use TYPO3\CMS\Install\Service\SqlExpectedSchemaService;
2933

@@ -98,10 +102,12 @@ public function doesUpdateStructureActionReportChanges()
98102
$result = $this->controller->updateStructureAction($arguments);
99103

100104
// Assert that nothing has been created, this is just for reporting:
101-
$tables = $GLOBALS['TYPO3_DB']->admin_get_tables();
102-
$pagesFields = $GLOBALS['TYPO3_DB']->admin_get_fields('pages');
105+
$tables = $this->getTables();
106+
$pagesFields = $this->getTableColumns('pages');
107+
103108
$this->assertFalse(isset($tables['tx_testextension_test']));
104-
$this->assertNotEquals('varchar(255)', strtolower($pagesFields['alias']['Type']));
109+
$this->assertEquals(ColumnTypes::STRING, $pagesFields['alias']->getType()->getName());
110+
$this->assertEquals(33, $pagesFields['alias']->getLength());
105111

106112
// Assert that changes are reported:
107113
$this->assertContains('ALTER TABLE pages ADD tx_testextension_field_test', $result);
@@ -122,12 +128,14 @@ public function doesUpdateStructureActionExecuteChanges()
122128
$result = $this->controller->updateStructureAction($arguments);
123129

124130
// Assert that tables have been created:
125-
$tables = $GLOBALS['TYPO3_DB']->admin_get_tables();
126-
$pagesFields = $GLOBALS['TYPO3_DB']->admin_get_fields('pages');
131+
$tables = $this->getTables();
132+
$pagesFields = $this->getTableColumns('pages');
133+
127134
$this->assertTrue(isset($tables['tx_testextension']));
128135
$this->assertTrue(isset($tables['tx_testextension_test']));
129136
$this->assertTrue(isset($pagesFields['tx_testextension_field_test']));
130-
$this->assertEquals('varchar(255)', strtolower($pagesFields['alias']['Type']));
137+
$this->assertEquals(ColumnTypes::STRING, $pagesFields['alias']->getType()->getName());
138+
$this->assertEquals(255, $pagesFields['alias']->getLength());
131139

132140
// Assert that nothing is reported we just want to execute:
133141
$this->assertEmpty($result);
@@ -149,9 +157,10 @@ public function doesUpdateStructureActionReportRemovals()
149157
$result = $this->controller->updateStructureAction($arguments);
150158

151159
// Assert that nothing has been removed, this is just for reporting:
152-
$tables = $GLOBALS['TYPO3_DB']->admin_get_tables();
160+
$tables = $this->getTables();
153161
$this->assertTrue(isset($tables['tx_testextension']));
154-
$pagesFields = $GLOBALS['TYPO3_DB']->admin_get_fields('pages');
162+
163+
$pagesFields = $this->getTableColumns('pages');
155164
$this->assertTrue(isset($pagesFields['tx_testextension_field']));
156165

157166
// Assert that removals are reported:
@@ -175,9 +184,10 @@ public function doesUpdateStructureActionExecuteRemovals()
175184
$result = $this->controller->updateStructureAction($arguments);
176185

177186
// Assert that tables and columns have been removed:
178-
$tables = $GLOBALS['TYPO3_DB']->admin_get_tables();
187+
$tables = $this->getTables();
179188
$this->assertFalse(isset($tables['tx_testextension']));
180-
$pagesFields = $GLOBALS['TYPO3_DB']->admin_get_fields('pages');
189+
190+
$pagesFields = $this->getTableColumns('pages');
181191
$this->assertFalse(isset($pagesFields['tx_testextension_field']));
182192

183193
// Assert that nothing is reported we just want to execute:
@@ -200,9 +210,10 @@ public function doesUpdateStructureActionReportDropKeys()
200210
$result = $this->controller->updateStructureAction($arguments);
201211

202212
// Assert that nothing has been removed, this is just for reporting:
203-
$tables = $GLOBALS['TYPO3_DB']->admin_get_tables();
213+
$tables = $this->getTables();
204214
$this->assertTrue(isset($tables['tx_testextension']));
205-
$pagesFields = $GLOBALS['TYPO3_DB']->admin_get_fields('pages');
215+
216+
$pagesFields = $this->getTableColumns('pages');
206217
$this->assertTrue(isset($pagesFields['tx_testextension_field']));
207218

208219
// Assert that removals are reported:
@@ -233,6 +244,7 @@ public function doesUpdateStructureActionDumpChangesToFile()
233244

234245
$this->assertFileExists($testDumpFile);
235246
$testDumpFileContent = file_get_contents($testDumpFile);
247+
236248
// Assert that changes are dumped:
237249
$this->assertContains('ALTER TABLE pages ADD tx_testextension_field_test', $testDumpFileContent);
238250
$this->assertContains('ALTER TABLE pages CHANGE alias alias varchar(255)', $testDumpFileContent);
@@ -241,4 +253,32 @@ public function doesUpdateStructureActionDumpChangesToFile()
241253
// Assert that dump result is reported:
242254
$this->assertNotEmpty($result);
243255
}
244-
}
256+
257+
/**
258+
* @return string[]
259+
*/
260+
private function getTables()
261+
{
262+
/** @var Connection $connection */
263+
$connection = GeneralUtility::makeInstance(ConnectionPool::class)->getConnectionByName('Default');
264+
$tables = $connection->getSchemaManager()->listTableNames();
265+
266+
return array_flip($tables);
267+
}
268+
269+
/**
270+
* @param string $table
271+
* @return Column[]
272+
*/
273+
private function getTableColumns(string $table)
274+
{
275+
/** @var Connection $connection */
276+
$connection = GeneralUtility::makeInstance(ConnectionPool::class)->getConnectionByName('Default');
277+
$tableColumns = [];
278+
foreach ($connection->getSchemaManager()->listTableColumns($table) as $column) {
279+
$tableColumns[$column->getName()] = $column;
280+
}
281+
282+
return $tableColumns;
283+
}
284+
}

composer.json

+2-2
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@
1515
"helhum/typo3-console": ">=4.0"
1616
},
1717
"require-dev": {
18-
"typo3/cms": "^7.6",
19-
"nimut/testing-framework": "^4.1"
18+
"typo3/cms": "^8.7",
19+
"nimut/testing-framework": "^5.0"
2020
},
2121
"autoload": {
2222
"psr-4": {

0 commit comments

Comments
 (0)