Skip to content
This repository was archived by the owner on Aug 15, 2023. It is now read-only.

Commit b4ce845

Browse files
author
Stefano Kowalke
committed
[BUGFIX] Avoid dropping the 'categories' fields.
Resolves: #92
1 parent d460a6b commit b4ce845

File tree

5 files changed

+122
-210
lines changed

5 files changed

+122
-210
lines changed

Classes/Service/DatabaseApiService.php

Lines changed: 4 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@
2525
* This copyright notice MUST APPEAR in all copies of the script!
2626
***************************************************************/
2727
use InvalidArgumentException;
28-
use TYPO3\CMS\Core\Cache\Cache;
2928
use TYPO3\CMS\Core\Utility\GeneralUtility;
3029

3130
/**
@@ -38,19 +37,14 @@
3837
class DatabaseApiService {
3938

4039
/**
41-
* @var \Etobi\CoreAPI\Service\DatabaseComparator $comparator
42-
*/
43-
protected $comparator = NULL;
44-
45-
/**
46-
* @var \TYPO3\CMS\Install\Service\SqlSchemaMigrationService $schemaMigrationService Instance of SQL handler
40+
* @var \TYPO3\CMS\Extbase\Object\ObjectManager $objectManager
4741
*/
48-
protected $schemaMigrationService;
42+
protected $objectManager;
4943

5044
/**
51-
* @var \TYPO3\CMS\Extbase\Object\ObjectManager $objectManager
45+
* @var \Etobi\CoreAPI\Service\DatabaseComparator $comparator
5246
*/
53-
protected $objectManager;
47+
protected $comparator = NULL;
5448

5549
/**
5650
* Inject the ObjectManager
@@ -61,15 +55,6 @@ public function injectObjectManager(\TYPO3\CMS\Extbase\Object\ObjectManager $obj
6155
$this->objectManager = $objectManager;
6256
}
6357

64-
/**
65-
* Inject the SchemaMigrationService
66-
*
67-
* @param \TYPO3\CMS\Install\Service\SqlSchemaMigrationService $schemaMigrationService
68-
*/
69-
public function injectSchemaMigrationService(\TYPO3\CMS\Install\Service\SqlSchemaMigrationService $schemaMigrationService) {
70-
$this->schemaMigrationService = $schemaMigrationService;
71-
}
72-
7358
/**
7459
* Database compare.
7560
*

Classes/Service/DatabaseComparator.php

Lines changed: 46 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
use InvalidArgumentException;
1818
use TYPO3\CMS\Core\Cache\Cache;
1919
use TYPO3\CMS\Core\Utility\GeneralUtility;
20+
use TYPO3\CMS\Install\Service\SqlExpectedSchemaService;
2021

2122
/**
2223
* Class DatabaseCompareAbstract
@@ -39,6 +40,16 @@ abstract class DatabaseComparator {
3940
*/
4041
protected $objectManager;
4142

43+
/**
44+
* @var \TYPO3\CMS\Install\Service\SqlSchemaMigrationService $schemaMigrationService Instance of SQL handler
45+
*/
46+
protected $schemaMigrationService;
47+
48+
/**
49+
* @var \TYPO3\CMS\Install\Service\SqlExpectedSchemaService
50+
*/
51+
protected $sqlExpectedSchemaService;
52+
4253
/**
4354
* Inject the ObjectManager
4455
*
@@ -48,6 +59,22 @@ public function injectObjectManager(\TYPO3\CMS\Extbase\Object\ObjectManager $obj
4859
$this->objectManager = $objectManager;
4960
}
5061

62+
/**
63+
* @param \TYPO3\CMS\Install\Service\SqlExpectedSchemaService $sqlExpectedSchemaService
64+
*/
65+
public function injectSqlExpectedSchemaService(SqlExpectedSchemaService $sqlExpectedSchemaService) {
66+
$this->sqlExpectedSchemaService = $sqlExpectedSchemaService;
67+
}
68+
69+
/**
70+
* Inject the SchemaMigrationService
71+
*
72+
* @param \TYPO3\CMS\Install\Service\SqlSchemaMigrationService $schemaMigrationService
73+
*/
74+
public function injectSchemaMigrationService(\TYPO3\CMS\Install\Service\SqlSchemaMigrationService $schemaMigrationService) {
75+
$this->schemaMigrationService = $schemaMigrationService;
76+
}
77+
5178
/**
5279
* @param $actions
5380
*
@@ -81,46 +108,33 @@ protected function getUrl($url) {
81108
}
82109

83110
/**
84-
* Wrapper around Cache::getDatabaseTableDefinitions()
111+
* Reflect, checks and return the allowed actions
85112
*
86-
* @return string
87-
*/
88-
protected function getDatabaseTableDefinitionsFromCache() {
89-
return Cache::getDatabaseTableDefinitions();
90-
}
91-
92-
/**
93-
* Wrapper around \TYPO3\CMS\Core\Category\CategoryRegistry::getInstance()
94-
*
95-
* @return \TYPO3\CMS\Core\Category\CategoryRegistry
96-
*/
97-
protected function getCategoryRegistry() {
98-
return \TYPO3\CMS\Core\Category\CategoryRegistry::getInstance();
99-
}
100-
101-
/**
102-
* Checks the given actions against the defined and allowed actions
103-
*
104-
* @param $actions
105-
* @param array $allowedActions
106-
*
107-
* @throws InvalidArgumentException
113+
* @param string $actions comma separated list of IDs
114+
* @return array
108115
*/
109-
protected function checkAvailableActions($actions, array &$allowedActions = array()) {
110-
$classReflection = $this->objectManager->get('TYPO3\\CMS\\Extbase\\Reflection\\ClassReflection', 'Etobi\\CoreAPI\\Service\\DatabaseComparator');
111-
$availableActions = array_flip($classReflection->getConstants());
112-
116+
protected function getAllowedActions($actions) {
113117
if (empty($actions)) {
114118
throw new InvalidArgumentException('No compare modes defined');
115119
}
116120

117-
$actionSplit = $this->trimExplode($actions);
118-
foreach ($actionSplit as $split) {
119-
if (!isset($availableActions[$split])) {
120-
throw new InvalidArgumentException(sprintf('Action "%s" is not available!', $split));
121+
$allowedActions = array();
122+
$availableActions = array_flip(
123+
$this->objectManager->get(
124+
'TYPO3\\CMS\\Extbase\\Reflection\\ClassReflection',
125+
'Etobi\\CoreAPI\\Service\\DatabaseComparator'
126+
)->getConstants()
127+
);
128+
129+
$actions = $this->trimExplode($actions);
130+
foreach ($actions as $action) {
131+
if (!isset($availableActions[$action])) {
132+
throw new InvalidArgumentException(sprintf('Action "%s" is not available!', $action));
121133
}
122-
$allowedActions[$split] = 1;
134+
$allowedActions[$action] = 1;
123135
}
136+
137+
return $allowedActions;
124138
}
125139
}
126140

Classes/Service/DatabaseCompareDry.php

Lines changed: 34 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -25,99 +25,63 @@
2525
*/
2626
class DatabaseCompareDry extends DatabaseComparator {
2727

28-
/**
29-
* @var \TYPO3\CMS\Install\Service\SqlSchemaMigrationService $schemaMigrationService Instance of SQL handler
30-
*/
31-
protected $schemaMigrationService;
32-
33-
/**
34-
* Inject the SchemaMigrationService
35-
*
36-
* @param \TYPO3\CMS\Install\Service\SqlSchemaMigrationService $schemaMigrationService
37-
*/
38-
public function injectSchemaMigrationService(\TYPO3\CMS\Install\Service\SqlSchemaMigrationService $schemaMigrationService) {
39-
$this->schemaMigrationService = $schemaMigrationService;
40-
}
41-
4228
/**
4329
* Database compare.
4430
*
4531
* @param string $actions comma separated list of IDs
46-
*
4732
* @throws InvalidArgumentException
48-
*
4933
* @return array
5034
*/
5135
public function compare($actions) {
5236
$errors = array();
53-
$allowedActions = array();
37+
$results = array();
5438

55-
$this->checkAvailableActions($actions, $allowedActions);
39+
$allowedActions = $this->getAllowedActions($actions);
5640

57-
$tblFileContent = '';
41+
$expectedSchema = $this->sqlExpectedSchemaService->getExpectedDatabaseSchema();
42+
$currentSchema = $this->schemaMigrationService->getFieldDefinitions_database();
5843

59-
foreach ($GLOBALS['TYPO3_LOADED_EXT'] as $loadedExtConf) {
60-
if (is_array($loadedExtConf) && $loadedExtConf['ext_tables.sql']) {
61-
$extensionSqlContent = $this->getUrl($loadedExtConf['ext_tables.sql']);
62-
$tblFileContent .= LF . LF . LF . LF . $extensionSqlContent;
63-
}
64-
}
44+
$addCreateChange = $this->schemaMigrationService->getDatabaseExtra($expectedSchema, $currentSchema);
45+
$addCreateChange = $this->schemaMigrationService->getUpdateSuggestions($addCreateChange);
6546

66-
if (is_callable('TYPO3\\CMS\\Core\\Cache\\Cache::getDatabaseTableDefinitions')) {
67-
$tblFileContent .= $this->getDatabaseTableDefinitionsFromCache();
68-
}
47+
$dropRemove = $this->schemaMigrationService->getDatabaseExtra($currentSchema, $expectedSchema);
48+
$dropRemove = $this->schemaMigrationService->getUpdateSuggestions($dropRemove, 'remove');
6949

70-
if (class_exists('TYPO3\\CMS\\Core\\Category\\CategoryRegistry')) {
71-
$tblFileContent .= $this->getCategoryRegistry()->getDatabaseTableDefinitions();
50+
if ($allowedActions[self::ACTION_UPDATE_CLEAR_TABLE] == 1) {
51+
$results['update_clear_table'] = $addCreateChange['clear_table'];
7252
}
7353

74-
if ($tblFileContent) {
75-
$fileContent = implode(LF, $this->schemaMigrationService->getStatementArray($tblFileContent, 1, '^CREATE TABLE '));
76-
$fieldDefinitionsFromFile = $this->schemaMigrationService->getFieldDefinitions_fileContent($fileContent);
77-
78-
$fieldDefinitionsFromDb = $this->schemaMigrationService->getFieldDefinitions_database();
79-
80-
$diff = $this->schemaMigrationService->getDatabaseExtra($fieldDefinitionsFromFile, $fieldDefinitionsFromDb);
81-
$updateStatements = $this->schemaMigrationService->getUpdateSuggestions($diff);
82-
83-
$results = array();
84-
85-
if ($allowedActions[self::ACTION_UPDATE_CLEAR_TABLE] == 1) {
86-
$results['clear_table'] = $updateStatements['clear_table'];
87-
}
88-
89-
if ($allowedActions[self::ACTION_UPDATE_ADD] == 1) {
90-
$results['add'] = $updateStatements['add'];
91-
}
54+
if ($allowedActions[self::ACTION_UPDATE_ADD] == 1) {
55+
$results['update_add'] = $addCreateChange['add'];
56+
}
9257

93-
if ($allowedActions[self::ACTION_UPDATE_CHANGE] == 1) {
94-
$results['update_change'] = $updateStatements['change'];
95-
}
58+
if ($allowedActions[self::ACTION_UPDATE_CHANGE] == 1) {
59+
$results['update_change'] = $addCreateChange['change'];
60+
}
9661

97-
if ($allowedActions[self::ACTION_REMOVE_CHANGE] == 1) {
98-
$results['remove_change'] = $updateStatements['change'];
99-
}
62+
if ($allowedActions[self::ACTION_UPDATE_CREATE_TABLE] == 1) {
63+
$results['update_create_table'] = $addCreateChange['create_table'];
64+
}
10065

101-
if ($allowedActions[self::ACTION_REMOVE_DROP] == 1) {
102-
$results['drop'] = $updateStatements['drop'];
103-
}
66+
if ($allowedActions[self::ACTION_REMOVE_CHANGE] == 1) {
67+
$results['remove_change'] = $dropRemove['change'];
68+
}
10469

105-
if ($allowedActions[self::ACTION_UPDATE_CREATE_TABLE] == 1) {
106-
$results['create_table'] = $updateStatements['create_table'];
107-
}
70+
if ($allowedActions[self::ACTION_REMOVE_DROP] == 1) {
71+
$results['remove_drop'] = $dropRemove['drop'];
72+
}
10873

109-
if ($allowedActions[self::ACTION_REMOVE_CHANGE_TABLE] == 1) {
110-
$results['change_table'] = $updateStatements['change_table'];
111-
}
74+
if ($allowedActions[self::ACTION_REMOVE_CHANGE_TABLE] == 1) {
75+
$results['remove_change_table'] = $dropRemove['change_table'];
76+
}
11277

113-
if ($allowedActions[self::ACTION_REMOVE_DROP_TABLE] == 1) {
114-
$results['change_table'] = $updateStatements['change_table'];
115-
}
78+
if ($allowedActions[self::ACTION_REMOVE_DROP_TABLE] == 1) {
79+
$results['remove_drop_table'] = $dropRemove['drop_table'];
80+
}
11681

117-
foreach ($results as $key => $resultSet) {
118-
if (!empty($resultSet)) {
119-
$errors[$key] = $resultSet;
120-
}
82+
foreach ($results as $key => $resultSet) {
83+
if (!empty($resultSet)) {
84+
$errors[$key] = $resultSet;
12185
}
12286
}
12387

0 commit comments

Comments
 (0)