Skip to content

Commit 38234c6

Browse files
committed
Merge pull request #480 from cviebrock/schema-rename
Schema::rename('oldtable','newtable') support
2 parents c937f98 + ef5ab30 commit 38234c6

File tree

6 files changed

+80
-2
lines changed

6 files changed

+80
-2
lines changed

laravel/database/schema.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,25 @@ public static function create($table, $callback)
4040
return static::execute($table);
4141
}
4242

43+
/**
44+
* Rename a database table in the schema.
45+
*
46+
* @param string $table
47+
* @param string $name
48+
* @return void
49+
*/
50+
public static function rename($table, $new_name)
51+
{
52+
$table = new Schema\Table($table);
53+
54+
// To indicate that the table needs to be renamed, we will run the
55+
// "rename" command on the table instance and pass the instance to
56+
// the execute method as calling a Closure isn't needed.
57+
$table->rename($new_name);
58+
59+
return static::execute($table);
60+
}
61+
4362
/**
4463
* Drop a database table from the schema.
4564
*

laravel/database/schema/grammars/mysql.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -212,6 +212,18 @@ protected function key(Table $table, Fluent $command, $type)
212212
return 'ALTER TABLE '.$this->wrap($table)." ADD {$type} {$name}({$keys})";
213213
}
214214

215+
/**
216+
* Generate the SQL statement for a rename table command.
217+
*
218+
* @param Table $table
219+
* @param Fluent $command
220+
* @return string
221+
*/
222+
public function rename(Table $table, Fluent $command)
223+
{
224+
return 'RENAME TABLE '.$this->wrap($table).' TO '.$this->wrap($command->name);
225+
}
226+
215227
/**
216228
* Generate the SQL statement for a drop table command.
217229
*

laravel/database/schema/grammars/postgres.php

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -198,6 +198,18 @@ protected function key(Table $table, Fluent $command, $unique = false)
198198
return $create." INDEX {$command->name} ON ".$this->wrap($table)." ({$columns})";
199199
}
200200

201+
/**
202+
* Generate the SQL statement for a rename table command.
203+
*
204+
* @param Table $table
205+
* @param Fluent $command
206+
* @return string
207+
*/
208+
public function rename(Table $table, Fluent $command)
209+
{
210+
return 'ALTER TABLE '.$this->wrap($table).' RENAME TO '.$this->wrap($command->name);
211+
}
212+
201213
/**
202214
* Generate the SQL statement for a drop table command.
203215
*
@@ -302,7 +314,7 @@ protected function drop_key(Table $table, Fluent $command)
302314
*/
303315
public function drop_foreign(Table $table, Fluent $command)
304316
{
305-
return $this->drop_constraint($table, $command);
317+
return $this->drop_constraint($table, $command);
306318
}
307319

308320
/**

laravel/database/schema/grammars/sqlite.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -201,6 +201,18 @@ protected function key(Table $table, Fluent $command, $unique = false)
201201
return $create." INDEX {$command->name} ON ".$this->wrap($table)." ({$columns})";
202202
}
203203

204+
/**
205+
* Generate the SQL statement for a rename table command.
206+
*
207+
* @param Table $table
208+
* @param Fluent $command
209+
* @return string
210+
*/
211+
public function rename(Table $table, Fluent $command)
212+
{
213+
return 'ALTER TABLE '.$this->wrap($table).' RENAME TO '.$this->wrap($command->name);
214+
}
215+
204216
/**
205217
* Generate the SQL statement for a drop table command.
206218
*

laravel/database/schema/grammars/sqlserver.php

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -212,6 +212,18 @@ protected function key(Table $table, Fluent $command, $unique = false)
212212
return $create." INDEX {$command->name} ON ".$this->wrap($table)." ({$columns})";
213213
}
214214

215+
/**
216+
* Generate the SQL statement for a rename table command.
217+
*
218+
* @param Table $table
219+
* @param Fluent $command
220+
* @return string
221+
*/
222+
public function rename(Table $table, Fluent $command)
223+
{
224+
return 'ALTER TABLE '.$this->wrap($table).' RENAME TO '.$this->wrap($command->name);
225+
}
226+
215227
/**
216228
* Generate the SQL statement for a drop table command.
217229
*
@@ -320,7 +332,7 @@ protected function drop_key(Table $table, Fluent $command)
320332
*/
321333
public function drop_foreign(Table $table, Fluent $command)
322334
{
323-
return $this->drop_constraint($table, $command);
335+
return $this->drop_constraint($table, $command);
324336
}
325337

326338
/**

laravel/database/schema/table.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,17 @@ public function key($type, $columns, $name)
144144
return $this->command($type, compact('name', 'columns'));
145145
}
146146

147+
/**
148+
* Rename the database table.
149+
*
150+
* @param string $name
151+
* @return Fluent
152+
*/
153+
public function rename($name)
154+
{
155+
return $this->command(__FUNCTION__, compact('name'));
156+
}
157+
147158
/**
148159
* Drop the database table.
149160
*

0 commit comments

Comments
 (0)