diff --git a/src/Illuminate/Database/Schema/Blueprint.php b/src/Illuminate/Database/Schema/Blueprint.php index 1a8f60c4145b..6788eabffb8f 100755 --- a/src/Illuminate/Database/Schema/Blueprint.php +++ b/src/Illuminate/Database/Schema/Blueprint.php @@ -96,7 +96,7 @@ public function build(Connection $connection, Grammar $grammar) */ public function toSql(Connection $connection, Grammar $grammar) { - $this->addImpliedCommands(); + $this->addImpliedCommands($grammar); $statements = []; @@ -121,7 +121,7 @@ public function toSql(Connection $connection, Grammar $grammar) * * @return void */ - protected function addImpliedCommands() + protected function addImpliedCommands(Grammar $grammar) { if (count($this->getAddedColumns()) > 0 && ! $this->creating()) { array_unshift($this->commands, $this->createCommand('add')); @@ -132,6 +132,8 @@ protected function addImpliedCommands() } $this->addFluentIndexes(); + + $this->addFluentCommands($grammar); } /** @@ -164,6 +166,24 @@ protected function addFluentIndexes() } } + public function addFluentCommands(Grammar $grammar) + { + foreach ($this->columns as $column) { + foreach ($grammar->getFluentCommands() as $commandName) { + $attributeName = lcfirst($commandName); + + if (!isset($column->{$attributeName})) + continue; + + $value = $column->{$attributeName}; + + $this->addCommand( + $commandName, compact('value', 'column') + ); + } + } + } + /** * Determine if the blueprint has a create command. * diff --git a/src/Illuminate/Database/Schema/Grammars/Grammar.php b/src/Illuminate/Database/Schema/Grammars/Grammar.php index ddd38385710d..2fea9662b293 100755 --- a/src/Illuminate/Database/Schema/Grammars/Grammar.php +++ b/src/Illuminate/Database/Schema/Grammars/Grammar.php @@ -19,6 +19,13 @@ abstract class Grammar extends BaseGrammar */ protected $transactions = false; + /** + * Enable other commands to be executed outside of create or alter command (like indexes) + * + * @var array + */ + protected $fluentCommands = []; + /** * Compile a rename column command. * @@ -120,6 +127,14 @@ protected function getType(Fluent $column) return $this->{'type'.ucfirst($column->type)}($column); } + /** + * @return array + */ + public function getFluentCommands() + { + return $this->fluentCommands; + } + /** * Add the column modifiers to the definition. * diff --git a/src/Illuminate/Database/Schema/Grammars/PostgresGrammar.php b/src/Illuminate/Database/Schema/Grammars/PostgresGrammar.php index 795d4b8f625e..4420c4863116 100755 --- a/src/Illuminate/Database/Schema/Grammars/PostgresGrammar.php +++ b/src/Illuminate/Database/Schema/Grammars/PostgresGrammar.php @@ -22,6 +22,13 @@ class PostgresGrammar extends Grammar */ protected $modifiers = ['Increment', 'Nullable', 'Default']; + /** + * Enable other commands to be executed outside of create or alter command (like indexes) + * + * @var array + */ + protected $fluentCommands = ['Comment']; + /** * The columns available as serials. * @@ -297,6 +304,23 @@ public function compileDisableForeignKeyConstraints() return 'SET CONSTRAINTS ALL DEFERRED;'; } + /** + * Compile a plain index key command. + * + * @param \Illuminate\Database\Schema\Blueprint $blueprint + * @param \Illuminate\Support\Fluent $command + * @return string + */ + public function compileComment(Blueprint $blueprint, Fluent $command) + { + return sprintf('comment on column %s.%s is %s', + $this->wrapTable($blueprint), + $this->wrap($command->column->name), + "'".addslashes($command->value)."'" + ); + } + + /** * Create the column definition for a char type. * diff --git a/tests/Database/DatabasePostgresSchemaGrammarTest.php b/tests/Database/DatabasePostgresSchemaGrammarTest.php index 13ab3b782a30..e3dbd19c7203 100755 --- a/tests/Database/DatabasePostgresSchemaGrammarTest.php +++ b/tests/Database/DatabasePostgresSchemaGrammarTest.php @@ -33,6 +33,19 @@ public function testBasicCreateTable() $this->assertEquals('alter table "users" add column "id" serial primary key not null, add column "email" varchar(255) not null', $statements[0]); } + public function testCreateTableAndCommentColumn() + { + $blueprint = new Blueprint('users'); + $blueprint->create(); + $blueprint->increments('id'); + $blueprint->string('email')->comment("my first comment"); + $statements = $blueprint->toSql($this->getConnection(), $this->getGrammar()); + + $this->assertCount(2, $statements); + $this->assertEquals('create table "users" ("id" serial primary key not null, "email" varchar(255) not null)', $statements[0]); + $this->assertEquals('comment on column "users"."email" is \'my first comment\'', $statements[1]); + } + public function testDropTable() { $blueprint = new Blueprint('users');