diff --git a/laravel/database/schema.php b/laravel/database/schema.php index c7e56309..51dc724a 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, $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($new_name); + + return static::execute($table); + } + /** * Drop a database table from the schema. * diff --git a/laravel/database/schema/grammars/mysql.php b/laravel/database/schema/grammars/mysql.php index 85fe4b31..0c2941bb 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 d694b2b3..9ea74d17 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 a1f2399a..6594662c 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 4c3fa99c..ffbdea60 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 ba187df5..a513d794 100644 --- a/laravel/database/schema/table.php +++ b/laravel/database/schema/table.php @@ -144,6 +144,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. *