/**
  * Drop the index from the given table
  *
  * @param Index|string $index  The name of the index
  * @param string|Table $table The name of the table
  */
 public function dropIndex($index, $table)
 {
     if ($index instanceof Index) {
         $index = $index->getName();
     }
     $this->_execSql($this->_platform->getDropIndexSQL($index, $table));
 }
Exemple #2
0
 /**
  * @param $tables
  * @param $backupPrefix
  *
  * @return array
  *
  * @throws \Doctrine\DBAL\DBALException
  */
 protected function backupExistingSchema($tables, $mauticTables, $backupPrefix)
 {
     $sql = [];
     $sm = $this->db->getSchemaManager();
     //backup existing tables
     $backupRestraints = $backupSequences = $backupIndexes = $backupTables = $dropSequences = $dropTables = [];
     //cycle through the first time to drop all the foreign keys
     foreach ($tables as $t) {
         if (!isset($mauticTables[$t]) && !in_array($t, $mauticTables)) {
             // Not an applicable table
             continue;
         }
         $restraints = $sm->listTableForeignKeys($t);
         if (isset($mauticTables[$t])) {
             //to be backed up
             $backupRestraints[$mauticTables[$t]] = $restraints;
             $backupTables[$t] = $mauticTables[$t];
             $backupIndexes[$t] = $sm->listTableIndexes($t);
         } else {
             //existing backup to be dropped
             $dropTables[] = $t;
         }
         foreach ($restraints as $restraint) {
             $sql[] = $this->platform->getDropForeignKeySQL($restraint, $t);
         }
     }
     //now drop all the backup tables
     foreach ($dropTables as $t) {
         $sql[] = $this->platform->getDropTableSQL($t);
     }
     //now backup tables
     foreach ($backupTables as $t => $backup) {
         //drop old indexes
         /** @var \Doctrine\DBAL\Schema\Index $oldIndex */
         foreach ($backupIndexes[$t] as $indexName => $oldIndex) {
             if ($indexName == 'primary') {
                 continue;
             }
             $oldName = $oldIndex->getName();
             $newName = $this->generateBackupName($this->dbParams['table_prefix'], $backupPrefix, $oldName);
             $newIndex = new Index($newName, $oldIndex->getColumns(), $oldIndex->isUnique(), $oldIndex->isPrimary(), $oldIndex->getFlags());
             $newIndexes[] = $newIndex;
             $sql[] = $this->platform->getDropIndexSQL($oldIndex, $t);
         }
         //rename table
         $tableDiff = new TableDiff($t);
         $tableDiff->newName = $backup;
         $queries = $this->platform->getAlterTableSQL($tableDiff);
         $sql = array_merge($sql, $queries);
         //create new index
         if (!empty($newIndexes)) {
             foreach ($newIndexes as $newIndex) {
                 $sql[] = $this->platform->getCreateIndexSQL($newIndex, $backup);
             }
             unset($newIndexes);
         }
     }
     //apply foreign keys to backup tables
     foreach ($backupRestraints as $table => $oldRestraints) {
         foreach ($oldRestraints as $or) {
             $foreignTable = $or->getForeignTableName();
             $foreignTableName = $this->generateBackupName($this->dbParams['table_prefix'], $backupPrefix, $foreignTable);
             $r = new ForeignKeyConstraint($or->getLocalColumns(), $foreignTableName, $or->getForeignColumns(), $backupPrefix . $or->getName(), $or->getOptions());
             $sql[] = $this->platform->getCreateForeignKeySQL($r, $table);
         }
     }
     return $sql;
 }