Skip to content

Commit

Permalink
Merge branch 'master' of https://github.com/tsdogs/yii2-user
Browse files Browse the repository at this point in the history
  • Loading branch information
Alessandro Briosi committed May 23, 2017
2 parents c8ec099 + 3e93e26 commit 95a73fa
Showing 1 changed file with 31 additions and 0 deletions.
31 changes: 31 additions & 0 deletions migrations/Migration.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/
Expand Down

0 comments on commit 95a73fa

Please sign in to comment.