From 2c02bc918d759cd17e7655ccc4fa9a4e0475af4e Mon Sep 17 00:00:00 2001 From: sergey Gonimar Date: Mon, 10 Jun 2013 20:30:31 +0600 Subject: [PATCH] migrate command, primary key --- framework/yii/db/Command.php | 4 ++-- framework/yii/db/Migration.php | 29 +++++++++++++++++++++++++++++ framework/yii/db/QueryBuilder.php | 2 +- 3 files changed, 32 insertions(+), 3 deletions(-) diff --git a/framework/yii/db/Command.php b/framework/yii/db/Command.php index e3ea0d2..a32e892 100644 --- a/framework/yii/db/Command.php +++ b/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. * @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); } diff --git a/framework/yii/db/Migration.php b/framework/yii/db/Migration.php index 774ac14..ae25914 100644 --- a/framework/yii/db/Migration.php +++ b/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. * The method will properly quote the table and column names. * @param string $name the name of the foreign key constraint. diff --git a/framework/yii/db/QueryBuilder.php b/framework/yii/db/QueryBuilder.php index 4d7a451..0d221bc 100644 --- a/framework/yii/db/QueryBuilder.php +++ b/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. * @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) . ' DROP CONSTRAINT ' . $this->db->quoteColumnName($name);