Skip to content

Commit

Permalink
Postgres add comment (#18782)
Browse files Browse the repository at this point in the history
  • Loading branch information
andersondanilo committed Oct 28, 2017
1 parent c30d094 commit d2858a8
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 2 deletions.
24 changes: 22 additions & 2 deletions src/Illuminate/Database/Schema/Blueprint.php
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ public function build(Connection $connection, Grammar $grammar)
*/
public function toSql(Connection $connection, Grammar $grammar)
{
$this->addImpliedCommands();
$this->addImpliedCommands($grammar);

$statements = [];

Expand All @@ -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'));
Expand All @@ -132,6 +132,8 @@ protected function addImpliedCommands()
}

$this->addFluentIndexes();

$this->addFluentCommands($grammar);
}

/**
Expand Down Expand Up @@ -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.
*
Expand Down
15 changes: 15 additions & 0 deletions src/Illuminate/Database/Schema/Grammars/Grammar.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand Down Expand Up @@ -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.
*
Expand Down
24 changes: 24 additions & 0 deletions src/Illuminate/Database/Schema/Grammars/PostgresGrammar.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand Down Expand Up @@ -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.
*
Expand Down
13 changes: 13 additions & 0 deletions tests/Database/DatabasePostgresSchemaGrammarTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down

0 comments on commit d2858a8

Please sign in to comment.