diff --git a/migrations/Migration.php b/migrations/Migration.php index b7fa2458e..d8bd512f7 100644 --- a/migrations/Migration.php +++ b/migrations/Migration.php @@ -25,7 +25,7 @@ class Migration extends \yii\db\Migration protected $restrict = 'RESTRICT'; protected $cascade = 'CASCADE'; protected $dbType; - + /** * @inheritdoc @@ -54,22 +54,74 @@ public function init() throw new \RuntimeException('Your database is not supported!'); } } - + + public function dropTable($table) + { + if ($this->dbType == 'sqlsrv') { + $this->dropTableConstraints($table); + } + return parent::dropTable($table); + } + + public function dropColumn($table, $column) + { + if ($this->dbType == 'sqlsrv') { + $this->dropColumnConstraints($table, $column); + } + return parent::dropColumn($table, $column); + } + + /* + * Drops contratints and Indexes referencind a Table Column + */ public function dropColumnConstraints($table, $column) { $table = $this->db->schema->getRawTableName($table); $cmd = $this->db->createCommand('SELECT name FROM sys.default_constraints - WHERE parent_object_id = object_id(:table) + WHERE parent_object_id = object_id(:table_1) AND type = \'D\' AND parent_column_id = ( - SELECT column_id - FROM sys.columns - WHERE object_id = object_id(:table) - and name = :column - )', [ ':table' => $table, ':column' => $column ]); - + SELECT column_id + FROM sys.columns + WHERE object_id = object_id(:table_2) + AND name = :column + )', [ ':table_1' => $table, ':table_2' => $table, ':column' => $column ]); + $constraints = $cmd->queryAll(); foreach ($constraints as $c) { $this->execute('ALTER TABLE '.$this->db->quoteTableName($table).' DROP CONSTRAINT '.$this->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 + */ + 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'])); + } + } + } diff --git a/migrations/m140504_113157_update_tables.php b/migrations/m140504_113157_update_tables.php index 89c05cfa7..eae8d911a 100644 --- a/migrations/m140504_113157_update_tables.php +++ b/migrations/m140504_113157_update_tables.php @@ -53,6 +53,6 @@ public function down() $this->addColumn('{{%user}}', 'confirmation_sent_at', $this->integer()); $this->addColumn('{{%user}}', 'confirmation_token', $this->string(32)); $this->createIndex('{{%user_confirmation}}', '{{%user}}', 'id, confirmation_token', true); - $this->createIndex('{{%user_recovery}', '{{%user}}', 'id, recovery_token', true); + $this->createIndex('{{%user_recovery}}', '{{%user}}', 'id, recovery_token', true); } } diff --git a/migrations/m140830_171933_fix_ip_field.php b/migrations/m140830_171933_fix_ip_field.php index 76d1b6c04..87af8f5db 100644 --- a/migrations/m140830_171933_fix_ip_field.php +++ b/migrations/m140830_171933_fix_ip_field.php @@ -18,6 +18,10 @@ class m140830_171933_fix_ip_field extends Migration { public function up() { + if ($this->dbType == 'sqlsrv') { + // this is needed because we need to drop the constraint SQL created when renaming the column (?) + $this->dropColumnConstraints('{{%user}}','registration_ip'); + } $this->alterColumn('{{%user}}', 'registration_ip', $this->bigInteger()); } diff --git a/migrations/m150623_212711_fix_username_notnull.php b/migrations/m150623_212711_fix_username_notnull.php index 46afa50f1..1e69dc350 100644 --- a/migrations/m150623_212711_fix_username_notnull.php +++ b/migrations/m150623_212711_fix_username_notnull.php @@ -36,7 +36,7 @@ public function down() if ($this->dbType == 'sqlsrv') { $this->dropIndex('{{%user_unique_username}}', '{{%user}}'); } - $this->alterColumn('{{%user}}', 'username', $this->string(255)->null()); + $this->alterColumn('{{%user}}', 'username', $this->string(255). ' NULL'); if ($this->dbType == 'sqlsrv') { $this->createIndex('{{%user_unique_username}}', '{{%user}}', 'username', true); } diff --git a/models/UserSearch.php b/models/UserSearch.php index cf0c18ffb..6a2a75e8a 100644 --- a/models/UserSearch.php +++ b/models/UserSearch.php @@ -103,7 +103,8 @@ public function search($params) $query->andFilterWhere(['item_name' => $this->auth_item]); } - $table_name = $query->modelClass::tableName(); + $model = $query->modelClass; + $table_name = $model::tableName(); if ($this->created_at !== null) { $date = strtotime($this->created_at); diff --git a/views/admin/index.php b/views/admin/index.php index 4ae2e1855..bdc2384c6 100644 --- a/views/admin/index.php +++ b/views/admin/index.php @@ -139,7 +139,7 @@ } }, 'switch' => function ($url, $model) { - if($model->isAdmin && $model->id != Yii::$app->user->id && Yii::$app->getModule('user')->enableImpersonateUser) { + if(!$model->isAdmin && $model->id != Yii::$app->user->id && Yii::$app->getModule('user')->enableImpersonateUser) { return Html::a('', ['/user/admin/switch', 'id' => $model->id], [ 'title' => Yii::t('user', 'Become this user'), 'data-confirm' => Yii::t('user', 'Are you sure you want to switch to this user for the rest of this Session?'),