From 7d4a346f84536f0a7cbb70a38660fbc439a74070 Mon Sep 17 00:00:00 2001 From: Colin Viebrock Date: Mon, 2 Apr 2012 11:30:53 -0500 Subject: [PATCH 1/3] Adding Schema::rename('oldtable','newtable') support Signed-off-by: Colin Viebrock --- laravel/database/schema/grammars/mysql.php | 12 ++++++++++++ laravel/database/schema/grammars/postgres.php | 14 +++++++++++++- laravel/database/schema/grammars/sqlite.php | 12 ++++++++++++ laravel/database/schema/grammars/sqlserver.php | 14 +++++++++++++- laravel/database/schema/table.php | 11 +++++++++++ 5 files changed, 61 insertions(+), 2 deletions(-) diff --git a/laravel/database/schema/grammars/mysql.php b/laravel/database/schema/grammars/mysql.php index c2ae7455..bcc8e94a 100644 --- a/laravel/database/schema/grammars/mysql.php +++ b/laravel/database/schema/grammars/mysql.php @@ -212,6 +212,18 @@ protected function key(Table $table, Fluent $command, $type) return 'ALTER TABLE '.$this->wrap($table)." ADD {$type} {$name}({$keys})"; } + /** + * Generate the SQL statement for a rename table command. + * + * @param Table $table + * @param Fluent $command + * @return string + */ + public function rename(Table $table, Fluent $command) + { + return 'RENAME TABLE '.$this->wrap($table).' TO '.$this->wrap($command->name); + } + /** * Generate the SQL statement for a drop table command. * diff --git a/laravel/database/schema/grammars/postgres.php b/laravel/database/schema/grammars/postgres.php index 760af1a8..3c1b62e2 100644 --- a/laravel/database/schema/grammars/postgres.php +++ b/laravel/database/schema/grammars/postgres.php @@ -198,6 +198,18 @@ protected function key(Table $table, Fluent $command, $unique = false) return $create." INDEX {$command->name} ON ".$this->wrap($table)." ({$columns})"; } + /** + * Generate the SQL statement for a rename table command. + * + * @param Table $table + * @param Fluent $command + * @return string + */ + public function rename(Table $table, Fluent $command) + { + return 'ALTER TABLE '.$this->wrap($table).' RENAME TO '.$this->wrap($command->name); + } + /** * Generate the SQL statement for a drop table command. * @@ -302,7 +314,7 @@ protected function drop_key(Table $table, Fluent $command) */ public function drop_foreign(Table $table, Fluent $command) { - return $this->drop_constraint($table, $command); + return $this->drop_constraint($table, $command); } /** diff --git a/laravel/database/schema/grammars/sqlite.php b/laravel/database/schema/grammars/sqlite.php index 09102f52..00c69604 100644 --- a/laravel/database/schema/grammars/sqlite.php +++ b/laravel/database/schema/grammars/sqlite.php @@ -201,6 +201,18 @@ protected function key(Table $table, Fluent $command, $unique = false) return $create." INDEX {$command->name} ON ".$this->wrap($table)." ({$columns})"; } + /** + * Generate the SQL statement for a rename table command. + * + * @param Table $table + * @param Fluent $command + * @return string + */ + public function rename(Table $table, Fluent $command) + { + return 'ALTER TABLE '.$this->wrap($table).' RENAME TO '.$this->wrap($command->name); + } + /** * Generate the SQL statement for a drop table command. * diff --git a/laravel/database/schema/grammars/sqlserver.php b/laravel/database/schema/grammars/sqlserver.php index 0fb80f6a..513ed0f4 100644 --- a/laravel/database/schema/grammars/sqlserver.php +++ b/laravel/database/schema/grammars/sqlserver.php @@ -212,6 +212,18 @@ protected function key(Table $table, Fluent $command, $unique = false) return $create." INDEX {$command->name} ON ".$this->wrap($table)." ({$columns})"; } + /** + * Generate the SQL statement for a rename table command. + * + * @param Table $table + * @param Fluent $command + * @return string + */ + public function rename(Table $table, Fluent $command) + { + return 'ALTER TABLE '.$this->wrap($table).' RENAME TO '.$this->wrap($command->name); + } + /** * Generate the SQL statement for a drop table command. * @@ -320,7 +332,7 @@ protected function drop_key(Table $table, Fluent $command) */ public function drop_foreign(Table $table, Fluent $command) { - return $this->drop_constraint($table, $command); + return $this->drop_constraint($table, $command); } /** diff --git a/laravel/database/schema/table.php b/laravel/database/schema/table.php index 9518d6e7..f9561f07 100644 --- a/laravel/database/schema/table.php +++ b/laravel/database/schema/table.php @@ -142,6 +142,17 @@ public function key($type, $columns, $name) return $this->command($type, compact('name', 'columns')); } + /** + * Rename the database table. + * + * @param string $name + * @return Fluent + */ + public function rename($name) + { + return $this->command(__FUNCTION__, compact('name')); + } + /** * Drop the database table. * From 7293c0448d6df8c5ff74e006d1a4352531957369 Mon Sep 17 00:00:00 2001 From: Colin Viebrock Date: Mon, 2 Apr 2012 11:38:12 -0500 Subject: [PATCH 2/3] One more change (comments may need to be altered) Signed-off-by: Colin Viebrock --- laravel/database/schema.php | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/laravel/database/schema.php b/laravel/database/schema.php index 3787e33b..6290fa4e 100644 --- a/laravel/database/schema.php +++ b/laravel/database/schema.php @@ -40,6 +40,25 @@ public static function create($table, $callback) return static::execute($table); } + /** + * Rename a database table in the schema. + * + * @param string $table + * @param string $name + * @return void + */ + public static function rename($table, $rename) + { + $table = new Schema\Table($table); + + // To indicate that the table needs to be renamed, we will run the + // "rename" command on the table instance and pass the instance to + // the execute method as calling a Closure isn't needed. + $table->rename($name); + + return static::execute($table); + } + /** * Drop a database table from the schema. * From ef5ab30ca29f6a9349d921e1b3b6d6a7dd5d2edf Mon Sep 17 00:00:00 2001 From: Colin Viebrock Date: Sat, 26 May 2012 20:51:35 -0500 Subject: [PATCH 3/3] Fix ... thanks Vespakoen! --- laravel/database/schema.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/laravel/database/schema.php b/laravel/database/schema.php index 6290fa4e..a81d5bc3 100644 --- a/laravel/database/schema.php +++ b/laravel/database/schema.php @@ -47,14 +47,14 @@ public static function create($table, $callback) * @param string $name * @return void */ - public static function rename($table, $rename) + public static function rename($table, $new_name) { $table = new Schema\Table($table); // To indicate that the table needs to be renamed, we will run the // "rename" command on the table instance and pass the instance to // the execute method as calling a Closure isn't needed. - $table->rename($name); + $table->rename($new_name); return static::execute($table); }