Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[5.6] Postgres add comment (#18782) #21855

Merged
merged 2 commits into from
Oct 31, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 23 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,25 @@ 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
23 changes: 23 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,22 @@ 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