Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit f8c8051

Browse files
committed
Added renameColumn support to schema builder. Also added dropColumn support to SQLite.
1 parent e5d0ec7 commit f8c8051

11 files changed

Lines changed: 195 additions & 10 deletions

File tree

composer.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
"require": {
1313
"php": ">=5.3.7",
1414
"classpreloader/classpreloader": "1.0.*",
15+
"doctrine/dbal": "2.4.x",
1516
"ircmaxell/password-compat": "1.0.*",
1617
"filp/whoops": "1.0.1",
1718
"monolog/monolog": "1.4.*",

readme.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515
- Added `Redirect::home` method from Laravel 3.
1616
- Added `Crypt::setKey`, `Crypt::setCipher`, and `Crypt::setMode`.
1717
- Allow "lazy" eager loading from an individual model: `$user->load('orders.lines');`.
18+
- Added `renameColumn` support to schema builder.
19+
- Added `dropColumn` support to SQLite schema builder.
1820

1921
## Beta 4
2022

src/Illuminate/Database/Connection.php

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -539,6 +539,42 @@ protected function getElapsedTime($start)
539539
return number_format((microtime(true) - $start) * 1000, 2);
540540
}
541541

542+
/**
543+
* Get a Doctrine Schema Column instance.
544+
*
545+
* @param string $table
546+
* @param string $column
547+
* @return \Doctrine\DBAL\Schema\Column
548+
*/
549+
public function getDoctrineColumn($table, $column)
550+
{
551+
$schema = $this->getDoctrineSchemaManager();
552+
553+
return $schema->listTableDetails($table)->getColumn($column);
554+
}
555+
556+
/**
557+
* Get the Doctrine DBAL schema manager for the connection.
558+
*
559+
* @return \Doctrine\DBAL\Schema\AbstractSchemaManager
560+
*/
561+
public function getDoctrineSchemaManager()
562+
{
563+
return $this->getDoctrineDriver()->getSchemaManager($this->getDoctrineConnection());
564+
}
565+
566+
/**
567+
* Get the Doctrine DBAL database connection instance.
568+
*
569+
* @return \Doctrine\DBAL\Connection
570+
*/
571+
public function getDoctrineConnection()
572+
{
573+
$driver = $this->getDoctrineDriver();
574+
575+
return new \Doctrine\DBAL\Connection(array('pdo' => $this->pdo), $driver);
576+
}
577+
542578
/**
543579
* Get the currently used PDO connection.
544580
*

src/Illuminate/Database/MySqlConnection.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,4 +34,14 @@ protected function getDefaultSchemaGrammar()
3434
return $this->withTablePrefix(new Schema\Grammars\MySqlGrammar);
3535
}
3636

37+
/**
38+
* Get the Doctrine DBAL Driver.
39+
*
40+
* @return \Doctrine\DBAL\Driver
41+
*/
42+
protected function getDoctrineDriver()
43+
{
44+
return new \Doctrine\DBAL\Driver\PDOMySql\Driver;
45+
}
46+
3747
}

src/Illuminate/Database/PostgresConnection.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,4 +32,14 @@ protected function getDefaultPostProcessor()
3232
return new Query\Processors\PostgresProcessor;
3333
}
3434

35+
/**
36+
* Get the Doctrine DBAL Driver.
37+
*
38+
* @return \Doctrine\DBAL\Driver
39+
*/
40+
protected function getDoctrineDriver()
41+
{
42+
return new \Doctrine\DBAL\Driver\PDOPgSql\Driver;
43+
}
44+
3545
}

src/Illuminate/Database/SQLiteConnection.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,4 +22,14 @@ protected function getDefaultSchemaGrammar()
2222
return $this->withTablePrefix(new Schema\Grammars\SQLiteGrammar);
2323
}
2424

25+
/**
26+
* Get the Doctrine DBAL Driver.
27+
*
28+
* @return \Doctrine\DBAL\Driver
29+
*/
30+
protected function getDoctrineDriver()
31+
{
32+
return new \Doctrine\DBAL\Driver\PDOSqlite\Driver;
33+
}
34+
2535
}

src/Illuminate/Database/Schema/Blueprint.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -207,6 +207,18 @@ public function dropColumns()
207207
return $this->dropColumn(func_get_args());
208208
}
209209

210+
/**
211+
* Indicate that the given columns should be renamed.
212+
*
213+
* @param string $from
214+
* @param string $to
215+
* @return \Illuminate\Support\Fluent
216+
*/
217+
public function renameColumn($from, $to)
218+
{
219+
return $this->addCommand('renameColumn', compact('from', 'to'));
220+
}
221+
210222
/**
211223
* Indicate that the given primary key should be dropped.
212224
*

src/Illuminate/Database/Schema/Grammars/Grammar.php

Lines changed: 75 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,68 @@
11
<?php namespace Illuminate\Database\Schema\Grammars;
22

33
use Illuminate\Support\Fluent;
4+
use Doctrine\DBAL\Schema\Column;
5+
use Doctrine\DBAL\Schema\TableDiff;
6+
use Illuminate\Database\Connection;
47
use Illuminate\Database\Query\Expression;
58
use Illuminate\Database\Schema\Blueprint;
69
use Illuminate\Database\Grammar as BaseGrammar;
10+
use Doctrine\DBAL\Schema\AbstractSchemaManager as SchemaManager;
711

812
abstract class Grammar extends BaseGrammar {
913

14+
/**
15+
* Compile a rename column command.
16+
*
17+
* @param \Illuminate\Database\Schema\Blueprint $blueprint
18+
* @param \Illuminate\Support\Fluent $command
19+
* @param \Illuminate\Database\Connection $connection
20+
* @return array
21+
*/
22+
public function compileRenameColumn(Blueprint $blueprint, Fluent $command, Connection $connection)
23+
{
24+
$schema = $connection->getDoctrineSchemaManager();
25+
26+
$column = $connection->getDoctrineColumn($blueprint->getTabel(), $command->from);
27+
28+
$tableDiff = $this->getRenamedDiff($blueprint, $command, $column, $schema);
29+
30+
return (array) $schema->getDatabasePlatform()->getAlterTableSQL($tableDiff);
31+
}
32+
33+
/**
34+
* Get a new column instance with the new column name.
35+
*
36+
* @param \Illuminate\Database\Schema\Blueprint $blueprint
37+
* @param \Illuminate\Support\Fluent $command
38+
* @param \Doctrine\DBAL\Schema\Column $column
39+
* @param \Doctrine\DBAL\Schema\AbstractSchemaManager $schema
40+
* @return \Doctrine\DBAL\Schema\TableDiff
41+
*/
42+
protected function getRenamedDiff(Blueprint $blueprint, Fluent $command, Column $column, SchemaManager $schema)
43+
{
44+
$tableDiff = $this->getDoctrineTableDiff($blueprint, $schema);
45+
46+
return $this->setRenamedColumns($tableDiff, $command, $column);
47+
}
48+
49+
/**
50+
* Set the renamed columns on the table diff.
51+
*
52+
* @param \Doctrine\DBAL\Schema\TableDiff $tableDiff
53+
* @param \Illuminate\Support\Fluent $command
54+
* @param \Doctrine\DBAL\Schema\Column $column
55+
* @return \Doctrine\DBAL\Schema\TableDiff
56+
*/
57+
protected function setRenamedColumns(TableDiff $tableDiff, Command $command, Column $column)
58+
{
59+
$newColumn = new Column($command->to, $column->getType(), $column->toArray());
60+
61+
$tableDiff->renamedColumns = array($command->from => $newColumn);
62+
63+
return $tableDiff;
64+
}
65+
1066
/**
1167
* Compile a foreign key command.
1268
*
@@ -59,9 +115,9 @@ protected function getColumns(Blueprint $blueprint)
59115

60116
foreach ($blueprint->getColumns() as $column)
61117
{
62-
// Each of the column types have their own compiler functions which are
63-
// responsible for turning the column definition into its SQL format
64-
// for the platform. Then column modifiers are compiled and added.
118+
// Each of the column types have their own compiler functions which are tasked
119+
// with turning the column definition into its SQL format for this platform
120+
// used by the connection. The column's modifiers are compiled and added.
65121
$sql = $this->wrap($column).' '.$this->getType($column);
66122

67123
$columns[] = $this->addModifiers($sql, $blueprint, $column);
@@ -190,4 +246,20 @@ protected function getDefaultValue($value)
190246
return "'".strval($value)."'";
191247
}
192248

249+
/**
250+
* Create an empty Doctrine DBAL TableDiff from the Blueprint.
251+
*
252+
* @param \Illuminate\Database\Schema\Blueprint $blueprint
253+
* @param \Doctrine\DBAL\Schema\AbstractSchemaManager $schema
254+
* @return \Doctrine\DBAL\Schema\TableDiff
255+
*/
256+
protected function getDoctrineTableDiff(Blueprint $blueprint, SchemaManager $schema)
257+
{
258+
$tableDiff = new TableDiff($blueprint->getTable());
259+
260+
$tableDiff->fromTable = $schema->listTableDetails($blueprint->getTable());
261+
262+
return $tableDiff;
263+
}
264+
193265
}

src/Illuminate/Database/Schema/Grammars/SQLiteGrammar.php

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
<?php namespace Illuminate\Database\Schema\Grammars;
22

33
use Illuminate\Support\Fluent;
4+
use Illuminate\Database\Connection;
45
use Illuminate\Database\Schema\Blueprint;
56

67
class SQLiteGrammar extends Grammar {
@@ -193,11 +194,23 @@ public function compileDropIfExists(Blueprint $blueprint, Fluent $command)
193194
*
194195
* @param \Illuminate\Database\Schema\Blueprint $blueprint
195196
* @param \Illuminate\Support\Fluent $command
196-
* @return string
197+
* @param \Illuminate\Database\Connection $connection
198+
* @return array
197199
*/
198-
public function compileDropColumn(Blueprint $blueprint, Fluent $command)
200+
public function compileDropColumn(Blueprint $blueprint, Fluent $command, Connection $connection)
199201
{
200-
throw new \BadMethodCallException("Drop column not supported for SQLite.");
202+
$schema = $connection->getDoctrineSchemaManager();
203+
204+
$tableDiff = $this->getDoctrineTableDiff($blueprint, $schema);
205+
206+
foreach ($command->columns as $name)
207+
{
208+
$column = $connection->getDoctrineColumn($blueprint->getTable(), $name);
209+
210+
$tableDiff->removedColumns[$name] = $column;
211+
}
212+
213+
return (array) $schema->getDatabasePlatform()->getAlterTableSQL($tableDiff);
201214
}
202215

203216
/**

src/Illuminate/Database/SqlServerConnection.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,4 +55,14 @@ protected function getDefaultSchemaGrammar()
5555
return $this->withTablePrefix(new Schema\Grammars\SqlServerGrammar);
5656
}
5757

58+
/**
59+
* Get the Doctrine DBAL Driver.
60+
*
61+
* @return \Doctrine\DBAL\Driver
62+
*/
63+
protected function getDoctrineDriver()
64+
{
65+
return new \Doctrine\DBAL\Driver\PDOSqlsrv\Driver;
66+
}
67+
5868
}

0 commit comments

Comments
 (0)