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

Migration & MySql Query addCheck() and dropCheck() #19881

Merged
merged 18 commits into from
May 22, 2024
Merged
Show file tree
Hide file tree
Changes from 4 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
33 changes: 31 additions & 2 deletions framework/db/Migration.php
Original file line number Diff line number Diff line change
Expand Up @@ -311,15 +311,15 @@
*
* If a column is specified with definition only (e.g. 'PRIMARY KEY (name, type)'), it will be directly
* put into the generated SQL.
*
*
* Example usage:
* ```php
* class m200000_000000_create_table_fruits extends \yii\db\Migration
* {
* public function safeUp()
* {
* $this->createTable('{{%fruits}}', [
* // ...
* // ...
* 'column_name double precision null default null',
* ```

Expand Down Expand Up @@ -519,6 +519,35 @@
$this->endCommand($time);
}

/**
* Creates a SQL command for adding a check constraint to an existing table.
* @param string $name the name of the check constraint.
* The name will be properly quoted by the method.
* @param string $table the table that the check constraint will be added to.
* The name will be properly quoted by the method.
* @param string $expression the SQL of the `CHECK` constraint.
*/
public function addCheck($name, $table, $expression)
{
$time = $this->beginCommand("add check $name in table $table");
$this->db->createCommand()->addCheck($name, $table, $expression)->execute();
$this->endCommand($time);
}

Check warning on line 535 in framework/db/Migration.php

View check run for this annotation

Codecov / codecov/patch

framework/db/Migration.php#L530-L535

Added lines #L530 - L535 were not covered by tests

/**
* Creates a SQL command for dropping a check constraint.
* @param string $name the name of the check constraint to be dropped.
* The name will be properly quoted by the method.
* @param string $table the table whose check constraint is to be dropped.
* The name will be properly quoted by the method.
*/
public function dropCheck($name, $table)
{
$time = $this->beginCommand("add check $name in table $table");
samdark marked this conversation as resolved.
Show resolved Hide resolved
$this->db->createCommand()->dropCheck($name, $table)->execute();
$this->endCommand($time);
}

Check warning on line 549 in framework/db/Migration.php

View check run for this annotation

Codecov / codecov/patch

framework/db/Migration.php#L544-L549

Added lines #L544 - L549 were not covered by tests

/**
* Builds and execute a SQL statement for adding comment to column.
*
Expand Down
20 changes: 1 addition & 19 deletions framework/db/mysql/QueryBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -150,24 +150,6 @@ public function dropUnique($name, $table)
return $this->dropIndex($name, $table);
}

/**
* {@inheritdoc}
* @throws NotSupportedException this is not supported by MySQL.
*/
public function addCheck($name, $table, $expression)
{
throw new NotSupportedException(__METHOD__ . ' is not supported by MySQL.');
}

/**
* {@inheritdoc}
* @throws NotSupportedException this is not supported by MySQL.
*/
public function dropCheck($name, $table)
{
throw new NotSupportedException(__METHOD__ . ' is not supported by MySQL.');
}

/**
* Creates a SQL statement for resetting the sequence value of a table's primary key.
* The sequence will be reset such that the primary key of the next new row inserted
Expand Down Expand Up @@ -266,7 +248,7 @@ protected function prepareInsertValues($table, $columns, $params = [])
$columns = [reset($tableSchema->columns)->name];
$defaultValue = 'DEFAULT';
}

foreach ($columns as $name) {
$names[] = $this->db->quoteColumnName($name);
$placeholders[] = $defaultValue;
Expand Down