Browse Source

Merge pull request #521 from sergon/migrate_command_primary_key

Migrate command primary key
tags/2.0.0-beta
Qiang Xue 12 years ago
parent
commit
3fc1900877
  1. 4
      framework/yii/db/Command.php
  2. 29
      framework/yii/db/Migration.php
  3. 2
      framework/yii/db/QueryBuilder.php

4
framework/yii/db/Command.php

@ -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);
} }

29
framework/yii/db/Migration.php

@ -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.

2
framework/yii/db/QueryBuilder.php

@ -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);

Loading…
Cancel
Save