/**
  * @param Schema   $schema
  * @param QueryBag $queries
  * @param string   $tableName
  * @param string   $columnName
  * @param array    $options
  */
 protected function processColumn(Schema $schema, QueryBag $queries, $tableName, $columnName, $options)
 {
     $className = $this->entityMetadataHelper->getEntityClassByTableName($tableName);
     $table = $schema->getTable($tableName);
     if (!isset($options[ExtendOptionsManager::NEW_NAME_OPTION])) {
         if (isset($options[ExtendOptionsManager::TYPE_OPTION])) {
             $columnType = $options[ExtendOptionsManager::TYPE_OPTION];
             if (!in_array($columnType, ['oneToMane', 'manyToMany', 'manyToOne', 'optionSet'])) {
                 $indexName = $this->nameGenerator->generateIndexNameForExtendFieldVisibleInGrid($className, $columnName);
                 $enabled = !isset($options['datagrid']['is_visible']) || $options['datagrid']['is_visible'];
                 if ($enabled && !$table->hasIndex($indexName)) {
                     $table->addIndex([$columnName], $indexName);
                 } elseif (!$enabled && $table->hasIndex($indexName)) {
                     $table->dropIndex($indexName);
                 }
             }
         }
     } else {
         // in case of renaming column name we should rename existing index
         $newColumnName = $options[ExtendOptionsManager::NEW_NAME_OPTION];
         $indexName = $this->nameGenerator->generateIndexNameForExtendFieldVisibleInGrid($className, $columnName);
         if ($table->hasIndex($indexName)) {
             $table->dropIndex($indexName);
             $newIndexName = $this->nameGenerator->generateIndexNameForExtendFieldVisibleInGrid($className, $newColumnName);
             $this->renameExtension->addIndex($schema, $queries, $tableName, [$newColumnName], $newIndexName);
         }
     }
 }
 /**
  * @param Schema $schema
  * @param QueryBag $queries
  * @param string $tableName
  * @param string $columnName
  * @param array $options
  * @param string $className
  * @param Table $table
  */
 protected function renameIndex(Schema $schema, QueryBag $queries, $tableName, $columnName, $options, $className, $table)
 {
     $newColumnName = $options[ExtendOptionsManager::NEW_NAME_OPTION];
     $indexName = $this->nameGenerator->generateIndexNameForExtendFieldVisibleInGrid($className, $columnName);
     if ($table->hasIndex($indexName)) {
         $table->dropIndex($indexName);
         $newIndexName = $this->nameGenerator->generateIndexNameForExtendFieldVisibleInGrid($className, $newColumnName);
         $this->renameExtension->addIndex($schema, $queries, $tableName, [$newColumnName], $newIndexName);
     }
 }
Ejemplo n.º 3
0
 /**
  * @dataProvider addIndexProvider
  */
 public function testAddIndex($platform, $expectedSql)
 {
     $extension = new RenameExtension();
     $extension->setDatabasePlatform($platform);
     $extension->setNameGenerator(new DbIdentifierNameGenerator());
     $schema = new Schema();
     $queries = new QueryBag();
     $extension->addIndex($schema, $queries, 'test_table', ['new_column']);
     /** @var MigrationQuery $query */
     $query = $queries->getPostQueries()[0];
     $this->assertEquals($expectedSql, $query->getDescription());
 }
 protected function renameOneToManyExtendField(Schema $schema, QueryBag $queries, Table $table, $associationName, $targetEntityClassName, EntityMetadataHelper $entityMetadataHelper)
 {
     $entityClassName = $entityMetadataHelper->getEntityClassByTableName($table->getName());
     $targetTableName = $entityMetadataHelper->getTableNameByEntityClass($targetEntityClassName);
     if ($schema->hasTable($targetTableName)) {
         $targetTable = $schema->getTable($targetTableName);
         $oldTargetColumnName = sprintf('field_%s_%s_id', strtolower(ExtendHelper::getShortClassName($entityClassName)), $associationName);
         if ($targetTable->hasColumn($oldTargetColumnName)) {
             $newTargetColumnName = $this->nameGenerator->generateOneToManyRelationColumnName($entityClassName, $associationName);
             $oldIndexName = $this->nameGenerator->generateIndexName($targetTableName, [$oldTargetColumnName], false, true);
             if ($targetTable->hasIndex($oldIndexName)) {
                 $targetTable->dropIndex($oldIndexName);
             }
             $oldForeignKeyName = $this->nameGenerator->generateForeignKeyConstraintName($targetTableName, [$oldTargetColumnName], true);
             if ($targetTable->hasForeignKey($oldForeignKeyName)) {
                 $targetTable->removeForeignKey($oldForeignKeyName);
             }
             $this->renameExtension->renameColumn($schema, $queries, $targetTable, $oldTargetColumnName, $newTargetColumnName);
             $this->renameExtension->addIndex($schema, $queries, $targetTable->getName(), [$newTargetColumnName]);
             $this->renameExtension->addForeignKeyConstraint($schema, $queries, $targetTable->getName(), $table->getName(), [$newTargetColumnName], $table->getPrimaryKeyColumns(), ['onDelete' => 'SET NULL']);
         }
     }
 }