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; }
public function safeUp() { $all = <<<EOT select vv.id as idx, vv.variantId as variantId, v.productId as variantProductId, p.typeId as productTypeId, ot.handle as optionTypeName, ov.id as optionValueId, ov.name as optionValueName, ov.displayName as optionValueDisplayName from craft_market_variant_optionvalues vv \tleft join craft_market_variants v \t\ton vv.variantId = v.id \tleft join craft_market_products p \t\ton v.productId = p.id \tleft join craft_market_optionvalues ov \t\ton vv.optionValueId = ov.id \tleft join craft_market_optiontypes ot \t\ton ov.optionTypeId = ot.id EOT; $allData = craft()->db->createCommand($all)->queryAll(); if (!empty($allData)) { craft()->db->createCommand()->createTable('market_variantoptionvaluesbackup', ['idx' => ['column' => 'varchar', 'maxLength' => 255], 'variantId' => ['column' => 'varchar', 'maxLength' => 255], 'variantProductId' => ['column' => 'varchar', 'maxLength' => 255], 'productTypeId' => ['column' => 'varchar', 'maxLength' => 255], 'optionTypeName' => ['column' => 'varchar', 'maxLength' => 255], 'optionValueId' => ['column' => 'varchar', 'maxLength' => 255], 'optionValueName' => ['column' => 'varchar', 'maxLength' => 255], 'optionValueDisplayName' => ['column' => 'varchar', 'maxLength' => 255]], null, false); foreach ($allData as $row) { $this->insert('market_variantoptionvaluesbackup', $row); } } $market_optionvalues = MigrationHelper::getTable('market_optionvalues'); $market_optiontypes = MigrationHelper::getTable('market_optiontypes'); $market_product_optiontypes = MigrationHelper::getTable('market_product_optiontypes'); $market_variant_optionvalues = MigrationHelper::getTable('market_variant_optionvalues'); MigrationHelper::dropAllForeignKeysOnTable($market_optionvalues); MigrationHelper::dropAllForeignKeysOnTable($market_optiontypes); MigrationHelper::dropAllForeignKeysOnTable($market_product_optiontypes); MigrationHelper::dropAllForeignKeysOnTable($market_variant_optionvalues); $this->dropTable('market_optionvalues'); $this->dropTable('market_optiontypes'); $this->dropTable('market_product_optiontypes'); $this->dropTable('market_variant_optionvalues'); return true; }
/** * Drops the foreign keys from the model's table. * * @return null */ public function dropForeignKeys() { $tableName = $this->getTableName(); // Does the table exist? if (craft()->db->tableExists($tableName, true)) { $table = MigrationHelper::getTable($tableName); MigrationHelper::dropAllForeignKeysOnTable($table); } }
$fks[] = array($tableName, 'locale', 'locales', 'locale', 'CASCADE', 'CASCADE', $app->db->getForeignKeyName($tableName, array('locale'))); } // What are we doing? $run = !empty($_POST['run']); $backupDb = !empty($_POST['backupdb']); if ($backupDb) { $path = $app->db->backup(false); if ($path === false) { throw new Exception('The DB backup failed'); } } $report = array(); foreach ($fks as $fk) { list($tableName, $columns, $refTableName, $refColumns, $onDelete, $onUpdate, $fkName) = $fk; // Make sure the table exists if (MigrationHelper::getTable($tableName) === null) { 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