public function safeUp()
 {
     $this->addColumnAfter('market_shippingmethods', 'default', ColumnType::Bool, 'enabled');
     MigrationHelper::dropForeignKeyIfExists('market_ordertypes', ['shippingMethodId']);
     $this->dropColumn('market_ordertypes', 'shippingMethodId');
     return true;
 }
 public function safeUp()
 {
     MigrationHelper::dropForeignKeyIfExists('market_lineitems', ['variantId']);
     $this->renameColumn('market_lineitems', 'variantId', 'purchasableId');
     $this->addForeignKey('market_lineitems', 'purchasableId', 'elements', 'id', 'SET NULL', 'CASCADE');
     return true;
 }
 /**
  * Any migration code in here is wrapped inside of a transaction.
  *
  * @return bool
  */
 public function safeUp()
 {
     MigrationHelper::refresh();
     Craft::log('Dropping FK if it exists.', LogLevel::Info, true);
     MigrationHelper::dropForeignKeyIfExists('emailmessages', array('locale'));
     Craft::log('Adding FK to emailmessages table.', LogLevel::Info, true);
     $this->addForeignKey('emailmessages', 'locale', 'locales', 'locale', 'CASCADE', 'CASCADE');
     return true;
 }
 /**
  * Any migration code in here is wrapped inside of a transaction.
  *
  * @return bool
  */
 public function safeUp()
 {
     Craft::log('Dropping FK if it exists.', LogLevel::Info, true);
     MigrationHelper::dropForeignKeyIfExists('charges', array('id', 'userId'));
     // This throws errors on some installs
     // It's not fully requried, so we'll avoid it for now, and update it in a future version
     //Craft::log('Adding FK to charges table.', LogLevel::Info, true);
     //$this->addForeignKey('charges', 'id', 'elements', 'id', 'CASCADE', 'CASCADE');
     //$this->addForeignKey('charges', 'userId', 'users', 'id', 'SET NULL');
     return true;
 }
 /**
  * Any migration code in here is wrapped inside of a transaction.
  *
  * @return bool
  */
 public function safeUp()
 {
     if (craft()->db->columnExists('sections', 'defaultAuthorId')) {
         Craft::log('Dropping foreign key from sections to users table for default author.', LogLevel::Info, true);
         MigrationHelper::dropForeignKeyIfExists('sections', array('defaultAuthorId'));
         Craft::log('Removing defaultAuthorId column from sections table.', LogLevel::Info, true);
         $this->dropColumn('sections', 'defaultAuthorId');
     } else {
         Craft::log('defaultAuthorId does not exist in the sections, table.  All is well.', LogLevel::Info, true);
     }
     return true;
 }
 /**
  * Any migration code in here is wrapped inside of a transaction.
  *
  * @return bool
  */
 public function safeUp()
 {
     /* -- Rename the column in  the retour_static_redirects column */
     MigrationHelper::renameColumn('retour_static_redirects', 'associatedEntryId', 'associatedElementId');
     /* -- Rename the column in  the retour_redirects column */
     MigrationHelper::renameColumn('retour_redirects', 'associatedEntryId', 'associatedElementId');
     /* -- Drop the old fk */
     MigrationHelper::dropForeignKeyIfExists('retour_redirects', array('associatedElementId'));
     /* -- Add the new foreign key */
     $this->addForeignKey('retour_redirects', 'associatedElementId', 'elements', 'id', 'CASCADE', 'CASCADE');
     // return true and let craft know its done
     return true;
 }
 /**
  * Any migration code in here is wrapped inside of a transaction.
  *
  * @return bool
  */
 public function safeUp()
 {
     // Entry Versions
     Craft::log('Dropping foreign key for the `creatorId` column on the `entryversions` table.', LogLevel::Info, true);
     MigrationHelper::dropForeignKeyIfExists('entryversions', array('creatorId'));
     Craft::log('Making `creatorId` nullable on the table `entryversions`.', LogLevel::Info, true);
     $this->alterColumn('entryversions', 'creatorId', array('column' => ColumnType::Int, 'null' => true));
     Craft::log('Adding foreign key for the `creatorId` column on the `entryversions` table with a delete set to nullable.', LogLevel::Info, true);
     $this->addForeignKey('entryversions', 'creatorId', 'users', 'id', 'SET NULL');
     // Entry Drafts
     Craft::log('Dropping foreign key for the `creatorId` column on the `entrydrafts` table.', LogLevel::Info, true);
     MigrationHelper::dropForeignKeyIfExists('entrydrafts', array('creatorId'));
     Craft::log('Adding foreign key for the `creatorId` column on the `entrydrafts` table with a cascade delete set', LogLevel::Info, true);
     $this->addForeignKey('entrydrafts', 'creatorId', 'users', 'id', 'CASCADE');
     return true;
 }
 public function safeUp()
 {
     MigrationHelper::dropForeignKeyIfExists('market_orders', ['typeId']);
     MigrationHelper::dropIndexIfExists('market_orders', ['typeId']);
     $this->dropColumn('market_orders', 'typeId');
     // find everything that is not 'order'
     $ids = craft()->db->createCommand()->select('id')->from('market_ordertypes')->where("handle != 'order'")->queryColumn();
     // delete 'em
     $this->delete('market_ordertypes', array('in', 'id', $ids));
     $table = MigrationHelper::getTable('market_ordertypes');
     MigrationHelper::dropAllForeignKeysOnTable($table);
     $this->renameTable('market_ordertypes', 'market_ordersettings');
     $this->addForeignKey('market_ordersettings', 'fieldLayoutId', 'fieldlayouts', 'id', 'SET NULL');
     $orderSettings = craft()->db->createCommand()->select('*')->from('market_ordersettings')->where("handle = 'order'")->queryScalar();
     if (!$orderSettings) {
         craft()->db->createCommand()->insert('market_ordersettings', ['name' => 'Order', 'handle' => 'order', 'fieldLayoutId' => null]);
     }
     return true;
 }
 /**
  * Any migration code in here is wrapped inside of a transaction.
  *
  * @return bool
  */
 public function safeUp()
 {
     MigrationHelper::refresh();
     $addFkBack = false;
     if (craft()->db->tableExists('tagsets')) {
         // A couple people have had failed updates that resulted in tagsets *and* taggroups tables lying around
         // causing a MySQL error if trying to rename the tagsets table
         // so let's make sure it's gone first.
         if (craft()->db->tableExists('taggroups')) {
             MigrationHelper::dropForeignKeyIfExists('taggroups', array('fieldLayoutId'));
             if (craft()->db->columnExists('tags', 'groupId')) {
                 MigrationHelper::dropForeignKeyIfExists('tags', array('groupId'));
                 MigrationHelper::renameColumn('tags', 'groupId', 'setId');
                 $addFkBack = true;
             }
             $this->dropTable('taggroups');
             // ...and refresh the schema cache
             craft()->db->getSchema()->refresh();
         }
         Craft::log('Renaming the tagsets table to taggroups.', LogLevel::Info, true);
         MigrationHelper::renameTable('tagsets', 'taggroups');
     }
     if (craft()->db->columnExists('tags', 'setId')) {
         Craft::log('Renaming the tags.setId column to groupId.', LogLevel::Info, true);
         MigrationHelper::renameColumn('tags', 'setId', 'groupId');
     }
     if ($addFkBack) {
         $this->addForeignKey('tags', 'groupId', 'taggroups', 'id', null, 'CASCADE');
     }
     Craft::log('Updating the Tags fields\' settings.', LogLevel::Info, true);
     $fields = craft()->db->createCommand()->select('id, settings')->from('fields')->where('type="Tags"')->queryAll();
     foreach ($fields as $field) {
         $settings = JsonHelper::decode($field['settings']);
         if (isset($settings['source']) && strncmp($settings['source'], 'tagset:', 7) === 0) {
             $settings['source'] = 'taggroup:' . substr($settings['source'], 7);
             $this->update('fields', array('settings' => JsonHelper::encode($settings)), array('id' => $field['id']));
         }
     }
     return true;
 }
 /**
  * Any migration code in here is wrapped inside of a transaction.
  *
  * @return bool
  */
 public function safeUp()
 {
     $relationsTable = $this->dbConnection->schema->getTable('{{relations}}');
     if ($relationsTable) {
         Craft::log('Dropping foreign key for the `childId` column on the `relations` table.', LogLevel::Info, true);
         MigrationHelper::dropForeignKeyIfExists('relations', array('childId'));
         Craft::log('Dropping foreign key for the `parentId` column on the `relations` table.', LogLevel::Info, true);
         MigrationHelper::dropForeignKeyIfExists('relations', array('parentId'));
         Craft::log('Dropping foreign key for the `fieldId` column on the `relations` table.', LogLevel::Info, true);
         MigrationHelper::dropForeignKeyIfExists('relations', array('fieldId'));
         // Make sure there are no orphans before we re-add the FK's.
         $this->_murderOrphans();
         Craft::log('Adding foreign key for the `childId` column on the `relations` table.', LogLevel::Info, true);
         $this->addForeignKey('relations', 'childId', 'elements', 'id', 'CASCADE');
         Craft::log('Adding foreign key for the `parentId` column on the `relations` table.', LogLevel::Info, true);
         $this->addForeignKey('relations', 'parentId', 'elements', 'id', 'CASCADE');
         Craft::log('Adding foreign key for the `fieldId` column on the `relations` table.', LogLevel::Info, true);
         $this->addForeignKey('relations', 'fieldId', 'fields', 'id', 'CASCADE');
     } else {
         Craft::log('Could not find an `relations` table. Wut?', LogLevel::Error);
     }
 }
 public function safeUp()
 {
     MigrationHelper::dropForeignKeyIfExists('market_orderstatuses', ['orderTypeId']);
     $this->dropColumn('market_orderstatuses', 'orderTypeId');
     return true;
 }
Ejemplo n.º 12
0
        throw new Exception("Table {$tableName} doesn't exist");
    }
    $columns = explode(',', $columns);
    $refColumns = explode(',', $refColumns);
    if (count($columns) > 1 || count($refColumns) > 1) {
        throw new Exception('Foreign keys spanning multiple columns is not supported.');
    }
    $columnName = $columns[0];
    $refColumnName = $refColumns[0];
    $allowNull = $onDelete == 'SET NULL';
    // Find the invalid values for this FK
    $invalidValues = $app->db->createCommand()->selectDistinct("t.{$columnName}")->from("{$tableName} t")->leftJoin("{$refTableName} r", "t.{$columnName} = r.{$refColumnName}")->where(array('and', "t.{$columnName} is not null", "r.{$refColumnName} is null"))->queryColumn();
    if ($run) {
        // Drop the existing FK if it exists
        // Even if it does, we want to recreate it with the proper ON DELETE and ON UPDATE values
        MigrationHelper::dropForeignKeyIfExists($tableName, $columns);
        // Deal with any invalid values
        if ($invalidValues) {
            $condition = array('in', $columnName, $invalidValues);
            if ($allowNull) {
                $app->db->createCommand()->update($tableName, array($columnName => null), $condition, array(), false);
            } else {
                $app->db->createCommand()->delete($tableName, $condition);
            }
        }
        // Add the FK
        $app->db->createCommand()->addForeignKey($tableName, $columnName, $refTableName, $refColumnName, $onDelete, $onUpdate);
    }
    $report[] = array($tableName, $columnName, $refTableName, $refColumnName, $allowNull, $invalidValues);
}
?>