Commit 3fc19008 by Qiang Xue

Merge pull request #521 from sergon/migrate_command_primary_key

Migrate command primary key
parents 22053cdc 9268e7d3
...@@ -673,9 +673,9 @@ class Command extends \yii\base\Component ...@@ -673,9 +673,9 @@ class Command extends \yii\base\Component
* @param string $table the table that the primary key constraint will be removed from. * @param string $table the table that the primary key constraint will be removed from.
* @return Command the command object itself * @return Command the command object itself
*/ */
public function dropPrimarykey($name, $table) public function dropPrimaryKey($name, $table)
{ {
$sql = $this->db->getQueryBuilder()->dropPrimarykey($name, $table); $sql = $this->db->getQueryBuilder()->dropPrimaryKey($name, $table);
return $this->setSql($sql); return $this->setSql($sql);
} }
......
...@@ -310,6 +310,35 @@ class Migration extends \yii\base\Component ...@@ -310,6 +310,35 @@ class Migration extends \yii\base\Component
} }
/** /**
* Builds and executes a SQL statement for creating a primary key.
* The method will properly quote the table and column names.
* @param string $name the name of the primary key constraint.
* @param string $table the table that the primary key constraint will be added to.
* @param string|array $columns comma separated string or array of columns that the primary key will consist of.
*/
public function addPrimaryKey($name, $table, $columns)
{
echo " > add primary key $name on $table (".(is_array($columns) ? implode(',',$columns) : $columns).") ...";
$time = microtime(true);
$this->db->createCommand()->addPrimaryKey($name, $table, $columns)->execute();
echo " done (time: " . sprintf('%.3f', microtime(true) - $time) . "s)\n";
}
/**
* Builds and executes a SQL statement for dropping a primary key.
* @param string $name the name of the primary key constraint to be removed.
* @param string $table the table that the primary key constraint will be removed from.
* @return Command the command object itself
*/
public function dropPrimaryKey($name, $table)
{
echo " > drop primary key $name ...";
$time = microtime(true);
$this->db->createCommand()->dropPrimaryKey($name, $table)->execute();
echo " done (time: " . sprintf('%.3f', microtime(true) - $time) . "s)\n";
}
/**
* Builds a SQL statement for adding a foreign key constraint to an existing table. * Builds a SQL statement for adding a foreign key constraint to an existing table.
* The method will properly quote the table and column names. * The method will properly quote the table and column names.
* @param string $name the name of the foreign key constraint. * @param string $name the name of the foreign key constraint.
......
...@@ -297,7 +297,7 @@ class QueryBuilder extends \yii\base\Object ...@@ -297,7 +297,7 @@ class QueryBuilder extends \yii\base\Object
* @param string $table the table that the primary key constraint will be removed from. * @param string $table the table that the primary key constraint will be removed from.
* @return string the SQL statement for removing a primary key constraint from an existing table. * * @return string the SQL statement for removing a primary key constraint from an existing table. *
*/ */
public function dropPrimarykey($name, $table) public function dropPrimaryKey($name, $table)
{ {
return 'ALTER TABLE ' . $this->db->quoteTableName($table) return 'ALTER TABLE ' . $this->db->quoteTableName($table)
. ' DROP CONSTRAINT ' . $this->db->quoteColumnName($name); . ' DROP CONSTRAINT ' . $this->db->quoteColumnName($name);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment