Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

MSSQL Migration fixes #2

Closed
wants to merge 4 commits into from
Closed
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
53 changes: 52 additions & 1 deletion migrations/Migration.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,25 @@ public function init()
}
}

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 = Yii::$app->db->schema->getRawTableName($table);
Expand All @@ -64,12 +83,44 @@ public function dropColumnConstraints($table, $column)
SELECT column_id
FROM sys.columns
WHERE object_id = object_id(:table)
and name = :column
AND name = :column
)', [ ':table' => $table, ':column' => $column ]);

$constraints = $cmd->queryAll();
foreach ($constraints as $c) {
$this->execute('ALTER TABLE '.Yii::$app->db->quoteTableName($table).' 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
*/
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