Exemplo n.º 1
0
 /**
  * If the schema is not for the Default connection remove all tables from the schema
  * that have no mapping in the TYPO3 configuration. This avoids update suggestions
  * for tables that are in the database but have no direct relation to the TYPO3 instance.
  *
  * @param bool $renameUnused
  * @return \Doctrine\DBAL\Schema\SchemaDiff
  * @throws \Doctrine\DBAL\DBALException
  * @throws \InvalidArgumentException
  */
 protected function buildSchemaDiff(bool $renameUnused = true) : SchemaDiff
 {
     // Build the schema definitions
     $fromSchema = $this->connection->getSchemaManager()->createSchema();
     $toSchema = $this->buildExpectedSchemaDefinitions($this->connectionName);
     // Add current table options to the fromSchema
     $tableOptions = $this->getTableOptions($fromSchema->getTableNames());
     foreach ($fromSchema->getTables() as $table) {
         $tableName = $table->getName();
         if (!array_key_exists($tableName, $tableOptions)) {
             continue;
         }
         foreach ($tableOptions[$tableName] as $optionName => $optionValue) {
             $table->addOption($optionName, $optionValue);
         }
     }
     // Build SchemaDiff and handle renames of tables and colums
     $comparator = GeneralUtility::makeInstance(Comparator::class);
     $schemaDiff = $comparator->compare($fromSchema, $toSchema);
     if ($renameUnused) {
         $schemaDiff = $this->migrateUnprefixedRemovedTablesToRenames($schemaDiff);
         $schemaDiff = $this->migrateUnprefixedRemovedFieldsToRenames($schemaDiff);
     }
     // All tables in the default connection are managed by TYPO3
     if ($this->connectionName === ConnectionPool::DEFAULT_CONNECTION_NAME) {
         return $schemaDiff;
     }
     // If there are no mapped tables return a SchemaDiff without any changes
     // to avoid update suggestions for tables not related to TYPO3.
     if (empty($GLOBALS['TYPO3_CONF_VARS']['DB']['TableMapping']) || !is_array($GLOBALS['TYPO3_CONF_VARS']['DB']['TableMapping'])) {
         /** @var SchemaDiff $schemaDiff */
         $schemaDiff = GeneralUtility::makeInstance(SchemaDiff::class, [], [], [], $fromSchema);
         return $schemaDiff;
     }
     // Collect the table names that have been mapped to this connection.
     $connectionName = $this->connectionName;
     $tablesForConnection = array_keys(array_filter($GLOBALS['TYPO3_CONF_VARS']['DB']['TableMapping'], function ($tableConnectionName) use($connectionName) {
         return $tableConnectionName === $connectionName;
     }));
     // Remove all tables that are not assigned to this connection from the diff
     $schemaDiff->newTables = $this->removeUnrelatedTables($schemaDiff->newTables, $tablesForConnection);
     $schemaDiff->changedTables = $this->removeUnrelatedTables($schemaDiff->changedTables, $tablesForConnection);
     $schemaDiff->removedTables = $this->removeUnrelatedTables($schemaDiff->removedTables, $tablesForConnection);
     return $schemaDiff;
 }