/** * Compare current and expected database schemas and return the database differences * * @return array database differences */ protected function getDatabaseDifferences() { $expectedSchema = $this->expectedSchemaService->getExpectedDatabaseSchema(); $currentSchema = $this->schemaMigrationService->getFieldDefinitions_database(); // Difference from expected to current return $this->schemaMigrationService->getDatabaseExtra($expectedSchema, $currentSchema); }
/** * Determine all create table statements which create the sys_file* tables * * @return array */ protected function getRequiredUpdates() { $requiredUpdates = array(); $fileContent = \TYPO3\CMS\Core\Utility\GeneralUtility::getUrl(\TYPO3\CMS\Core\Utility\ExtensionManagementUtility::extPath('core') . 'ext_tables.sql'); $FDfile = $this->installerSql->getFieldDefinitions_fileContent($fileContent); $FDdb = $this->installerSql->getFieldDefinitions_database(TYPO3_db); $diff = $this->installerSql->getDatabaseExtra($FDfile, $FDdb); $update_statements = $this->installerSql->getUpdateSuggestions($diff); foreach ((array) $update_statements['create_table'] as $string) { if (preg_match('/^CREATE TABLE sys_file($|_)?/', $string)) { $requiredUpdates[] = $string; } } return $requiredUpdates; }
/** * Perform necessary database schema migrations * * @param SchemaUpdateType[] $schemaUpdateTypes List of permitted schema update types * @return SchemaUpdateResult Result of the schema update */ public function updateSchema(array $schemaUpdateTypes) { $expectedSchema = $this->expectedSchemaService->getExpectedDatabaseSchema(); $currentSchema = $this->schemaMigrationService->getFieldDefinitions_database(); $addCreateChange = $this->schemaMigrationService->getDatabaseExtra($expectedSchema, $currentSchema); $dropRename = $this->schemaMigrationService->getDatabaseExtra($currentSchema, $expectedSchema); $updateStatements = array(); ArrayUtility::mergeRecursiveWithOverrule($updateStatements, $this->schemaMigrationService->getUpdateSuggestions($addCreateChange)); ArrayUtility::mergeRecursiveWithOverrule($updateStatements, $this->schemaMigrationService->getUpdateSuggestions($dropRename, 'remove')); $updateResult = new SchemaUpdateResult(); foreach ($schemaUpdateTypes as $schemaUpdateType) { $statementTypes = $this->getStatementTypes($schemaUpdateType); foreach ($statementTypes as $statementType) { if (isset($updateStatements[$statementType])) { $statements = $updateStatements[$statementType]; $result = $this->schemaMigrationService->performUpdateQueries($statements, array_combine(array_keys($statements), array_fill(0, count($statements), TRUE))); if ($result === TRUE) { $updateResult->addPerformedUpdates($schemaUpdateType, count($statements)); } elseif (is_array($result)) { $updateResult->addErrors($schemaUpdateType, $result); } } } } return $updateResult; }
/** * Gets the sql data dump for a specific sql file (for example ext_tables.sql) * * @param string $sqlFile * @return string */ protected function getSqlDataDumpForFile($sqlFile) { $sqlData = ''; if (file_exists($sqlFile)) { $sqlContent = \TYPO3\CMS\Core\Utility\GeneralUtility::getUrl($sqlFile); $fieldDefinitions = $this->installToolSqlParser->getFieldDefinitions_fileContent($sqlContent); $sqlData = $this->databaseUtility->dumpStaticTables($fieldDefinitions); } return $sqlData; }
/** * Gets the sql data dump for a specific sql file (for example ext_tables.sql) * * @param string $sqlFile * @return string */ protected function getSqlDataDumpForFile($sqlFile) { $sqlData = ''; if (file_exists($sqlFile)) { $sqlContent = file_get_contents($sqlFile); $fieldDefinitions = $this->installToolSqlParser->getFieldDefinitions_fileContent($sqlContent); $sqlData = $this->databaseUtility->dumpStaticTables($fieldDefinitions); } return $sqlData; }
/** * Gets all create, add and change queries from core/ext_tables.sql * * @return array */ protected function getUpdateStatements() { $updateStatements = array(); // Get all necessary statements for ext_tables.sql file $rawDefinitions = \TYPO3\CMS\Core\Utility\GeneralUtility::getUrl(\TYPO3\CMS\Core\Utility\ExtensionManagementUtility::extPath('core') . '/ext_tables.sql'); $fieldDefinitionsFromFile = $this->installToolSqlParser->getFieldDefinitions_fileContent($rawDefinitions); if (count($fieldDefinitionsFromFile)) { $fieldDefinitionsFromCurrentDatabase = $this->installToolSqlParser->getFieldDefinitions_database(); $diff = $this->installToolSqlParser->getDatabaseExtra($fieldDefinitionsFromFile, $fieldDefinitionsFromCurrentDatabase); $updateStatements = $this->installToolSqlParser->getUpdateSuggestions($diff); } return $updateStatements; }
/** * Gets the defined field definitions from the ext_tables.sql files. * * @return array The accordant definitions */ protected function getDefinedFieldDefinitions() { $content = ''; $cacheTables = ''; if (class_exists('t3lib_cache') && method_exists(t3lib_cache, 'getDatabaseTableDefinitions')) { $cacheTables = \TYPO3\CMS\Core\Cache\Cache::getDatabaseTableDefinitions(); } if (method_exists($this->install, 'getFieldDefinitions_fileContent')) { $content = $this->install->getFieldDefinitions_fileContent(implode(chr(10), $this->getAllRawStructureDefinitions()) . $cacheTables); } else { $content = $this->install->getFieldDefinitions_sqlContent(implode(chr(10), $this->getAllRawStructureDefinitions()) . $cacheTables); } return $content; }
/** * @test */ public function columnAndKeyDeletionDoesNotReturnAnError() { // Get the current database fields. $currentDatabaseSchema = $this->sqlSchemaMigrationService->getFieldDefinitions_database(); // Limit our scope to the be_users table: $currentDatabaseSchemaForBeUsers = array(); $currentDatabaseSchemaForBeUsers['be_users'] = $currentDatabaseSchema['be_users']; unset($currentDatabaseSchema); // Create a key and a field that belongs to that key: $expectedDatabaseSchemaForBeUsers = $currentDatabaseSchemaForBeUsers; $expectedDatabaseSchemaForBeUsers['be_users']['fields']['functional_test_field_1'] = "tinyint(1) unsigned NOT NULL default '0'"; $expectedDatabaseSchemaForBeUsers['be_users']['keys']['functional_test_key_1'] = 'KEY functional_test_key_1 (functional_test_field_1)'; $createFieldDiff = $this->sqlSchemaMigrationService->getDatabaseExtra($expectedDatabaseSchemaForBeUsers, $currentDatabaseSchemaForBeUsers); $createFieldDiff = $this->sqlSchemaMigrationService->getUpdateSuggestions($createFieldDiff); $this->sqlSchemaMigrationService->performUpdateQueries($createFieldDiff['add'], $createFieldDiff['add']); // Now remove the fields again (without the renaming step). unset($currentDatabaseSchemaForBeUsers['be_users']['fields']['functional_test_field_1']); unset($currentDatabaseSchemaForBeUsers['be_users']['keys']['functional_test_key_1']); $this->sqlSchemaMigrationService->setDeletedPrefixKey(''); $removeFieldDiff = $this->sqlSchemaMigrationService->getDatabaseExtra($expectedDatabaseSchemaForBeUsers, $currentDatabaseSchemaForBeUsers); $removeFieldDiff = $this->sqlSchemaMigrationService->getUpdateSuggestions($removeFieldDiff, 'remove'); $result = $this->sqlSchemaMigrationService->performUpdateQueries($removeFieldDiff['drop'], $removeFieldDiff['drop']); $this->assertTrue($result, 'performUpdateQueries() did not return TRUE, this means an error occurred: ' . (is_array($result) ? array_pop($result) : '')); }
/** * Parse and analyze given SQL string * * @param $sql * @return void */ protected function parseAndAnalyzeSql($sql) { $parsedSql = $this->installerSql->getFieldDefinitions_fileContent($sql); $this->analyzeFields($parsedSql); }
/** * @test */ public function getDatabaseExtraDoesNotLowercaseReservedWordsForTheComparison() { $subject = new SqlSchemaMigrationService(); $differenceArray = $subject->getDatabaseExtra(array('tx_foo' => array('fields' => array('PRIMARY KEY (md5hash)'))), array('tx_foo' => array('fields' => array('PRIMARY KEY (md5hash)')))); $this->assertEquals($differenceArray, array('extra' => array(), 'diff' => array(), 'diff_currentValues' => NULL)); }
/** * @test */ public function checkColumnDefinitionIfNoCommentIsSupplied() { $subject = new SqlSchemaMigrationService(); $fieldDefinition = $subject->assembleFieldDefinition(array('Field' => 'uid', 'Type' => 'int(11)', 'Null' => 'NO', 'Key' => 'PRI', 'Default' => NULL, 'Extra' => 'auto_increment')); $this->assertSame('int(11) NOT NULL auto_increment', $fieldDefinition); }
/** * @test */ public function getDatabaseExtraDoesNotLowercaseReservedWordsForTheComparison() { $differenceArray = $this->fixture->getDatabaseExtra(array('tx_foo' => array('fields' => array('PRIMARY KEY (md5hash)'))), array('tx_foo' => array('fields' => array('PRIMARY KEY (md5hash)')))); $this->assertEquals($differenceArray, array('extra' => array(), 'diff' => array(), 'diff_currentValues' => NULL)); }
/** * Stub function for the extension manager * * @return boolean true to allow access */ protected function init() { $this->objectManager = GeneralUtility::makeInstance('TYPO3\\CMS\\Extbase\\Object\\ObjectManager'); $this->schemaMigrationService = $this->objectManager->get('TYPO3\\CMS\\Install\\Service\\SqlSchemaMigrationService'); $this->currentSchema = $this->schemaMigrationService->getFieldDefinitions_database(); }