Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
70 changes: 61 additions & 9 deletions migrations/Migration.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ class Migration extends \yii\db\Migration
protected $restrict = 'RESTRICT';
protected $cascade = 'CASCADE';
protected $dbType;


/**
* @inheritdoc
Expand Down Expand Up @@ -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']));
}

}

}
2 changes: 1 addition & 1 deletion migrations/m140504_113157_update_tables.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
4 changes: 4 additions & 0 deletions migrations/m140830_171933_fix_ip_field.php
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}

Expand Down
2 changes: 1 addition & 1 deletion migrations/m150623_212711_fix_username_notnull.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
3 changes: 2 additions & 1 deletion models/UserSearch.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion views/admin/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -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('<span class="glyphicon glyphicon-user"></span>', ['/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?'),
Expand Down