Skip to content

Commit 95a73fa

Browse files
author
Alessandro Briosi
committed
Merge branch 'master' of https://github.com/tsdogs/yii2-user
2 parents c8ec099 + 3e93e26 commit 95a73fa

File tree

1 file changed

+31
-0
lines changed

1 file changed

+31
-0
lines changed

migrations/Migration.php

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,37 @@ public function dropColumnConstraints($table, $column)
107107
}
108108
}
109109

110+
/*
111+
* Drops contratints referencing the Table
112+
*/
113+
public function dropTableConstraints($table)
114+
{
115+
$table = Yii::$app->db->schema->getRawTableName($table);
116+
$cmd = Yii::$app->db->createCommand('SELECT name, OBJECT_NAME(parent_object_id) as tbl FROM sys.foreign_keys
117+
WHERE referenced_object_id = object_id(:table)',
118+
[ ':table' => $table ]);
119+
$constraints = $cmd->queryAll();
120+
foreach ($constraints as $c) {
121+
echo 'Dropping constrain: '.$c['name']."\n";
122+
$this->execute('ALTER TABLE '.Yii::$app->db->quoteTableName($c['tbl']).' DROP CONSTRAINT '.Yii::$app->db->quoteColumnName($c['name']));
123+
}
124+
125+
// checking for indexes
126+
$cmd = Yii::$app->db->createCommand('SELECT ind.name FROM sys.indexes ind
127+
INNER JOIN sys.index_columns ic
128+
ON ind.object_id = ic.object_id and ind.index_id = ic.index_id
129+
INNER JOIN sys.columns col
130+
ON ic.object_id = col.object_id and ic.column_id = col.column_id
131+
WHERE ind.object_id = object_id(:table)
132+
AND col.name = :column',
133+
[ ':table' => $table, ':column' => $column ]);
134+
135+
$indexes = $cmd->queryAll();
136+
foreach ($indexes as $i) {
137+
$this->dropIndex($i['name'],$table);
138+
}
139+
}
140+
110141
/*
111142
* Drops contratints referencing the Table
112143
*/

0 commit comments

Comments
 (0)