diff --git a/migrations/Migration.php b/migrations/Migration.php index 159adde11..7b155049a 100644 --- a/migrations/Migration.php +++ b/migrations/Migration.php @@ -107,6 +107,37 @@ public function dropColumnConstraints($table, $column) } } + /* + * Drops contratints referencing the Table + */ + public function dropTableConstraints($table) + { + $table = Yii::$app->db->schema->getRawTableName($table); + $cmd = Yii::$app->db->createCommand('SELECT name, OBJECT_NAME(parent_object_id) as tbl FROM sys.foreign_keys + WHERE referenced_object_id = object_id(:table)', + [ ':table' => $table ]); + $constraints = $cmd->queryAll(); + foreach ($constraints as $c) { + echo 'Dropping constrain: '.$c['name']."\n"; + $this->execute('ALTER TABLE '.Yii::$app->db->quoteTableName($c['tbl']).' DROP CONSTRAINT '.Yii::$app->db->quoteColumnName($c['name'])); + } + + // checking for indexes + $cmd = Yii::$app->db->createCommand('SELECT ind.name FROM sys.indexes ind + INNER JOIN sys.index_columns ic + ON ind.object_id = ic.object_id and ind.index_id = ic.index_id + INNER JOIN sys.columns col + ON ic.object_id = col.object_id and ic.column_id = col.column_id + WHERE ind.object_id = object_id(:table) + AND col.name = :column', + [ ':table' => $table, ':column' => $column ]); + + $indexes = $cmd->queryAll(); + foreach ($indexes as $i) { + $this->dropIndex($i['name'],$table); + } + } + /* * Drops contratints referencing the Table */