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

[9.x] Add ability to add table comments for MySQL and Postgres #42401

Merged
merged 6 commits into from
May 17, 2022
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
11 changes: 11 additions & 0 deletions src/Illuminate/Database/Schema/Blueprint.php
Original file line number Diff line number Diff line change
Expand Up @@ -1507,6 +1507,17 @@ public function rememberToken()
return $this->string('remember_token', 100)->nullable();
}

/**
* Add a comment to the table.
*
* @param string $comment
* @return \Illuminate\Support\Fluent
*/
public function comment($comment)
{
return $this->addCommand('tableComment', compact('comment'));
}

/**
* Add a new index command to the blueprint.
*
Expand Down
15 changes: 15 additions & 0 deletions src/Illuminate/Database/Schema/Grammars/MySqlGrammar.php
Original file line number Diff line number Diff line change
Expand Up @@ -492,6 +492,21 @@ public function compileDisableForeignKeyConstraints()
return 'SET FOREIGN_KEY_CHECKS=0;';
}

/**
* Compile a table comment command.
*
* @param \Illuminate\Database\Schema\Blueprint $blueprint
* @param \Illuminate\Support\Fluent $command
* @return string
*/
public function compileTableComment(Blueprint $blueprint, Fluent $command)
{
return sprintf('alter table %s comment = %s',
$this->wrapTable($blueprint),
"'".str_replace("'", "''", $command->comment)."'"
);
}

/**
* Create the column definition for a char type.
*
Expand Down
15 changes: 15 additions & 0 deletions src/Illuminate/Database/Schema/Grammars/PostgresGrammar.php
Original file line number Diff line number Diff line change
Expand Up @@ -486,6 +486,21 @@ public function compileComment(Blueprint $blueprint, Fluent $command)
);
}

/**
* Compile a table comment command.
*
* @param \Illuminate\Database\Schema\Blueprint $blueprint
* @param \Illuminate\Support\Fluent $command
* @return string
*/
public function compileTableComment(Blueprint $blueprint, Fluent $command)
{
return sprintf('comment on table %s is %s',
$this->wrapTable($blueprint),
"'".str_replace("'", "''", $command->comment)."'"
);
}

/**
* Quote-escape the given tables, views, or types.
*
Expand Down
19 changes: 19 additions & 0 deletions tests/Database/DatabaseSchemaBlueprintTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -415,4 +415,23 @@ public function testTinyTextNullableColumn()
'alter table "posts" add "note" nvarchar(255) null',
], $blueprint->toSql($connection, new SqlServerGrammar));
}

public function testTableComment()
{
$base = new Blueprint('posts', function (Blueprint $table) {
$table->comment('Look at my comment, it is amazing');
});

$connection = m::mock(Connection::class);

$blueprint = clone $base;
$this->assertEquals([
'alter table `posts` comment = \'Look at my comment, it is amazing\'',
], $blueprint->toSql($connection, new MySqlGrammar));

$blueprint = clone $base;
$this->assertEquals([
'comment on table "posts" is \'Look at my comment, it is amazing\'',
], $blueprint->toSql($connection, new PostgresGrammar));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php

namespace Illuminate\Tests\Integration\Database\MySql;

use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Schema;

/**
* @requires extension pdo_mysql
* @requires OS Linux|Darwin
*/
class DatabaseMySqlSchemaBuilderTest extends MySqlTestCase
{
public function testAddCommentToTable()
{
Schema::create('users', function (Blueprint $table) {
$table->id();
$table->comment('This is a comment');
});

$tableInfo = DB::table('information_schema.tables')
->where('table_schema', $this->app['config']->get('database.connections.mysql.database'))
->where('table_name', 'users')
->select('table_comment as table_comment')
->first();

$this->assertEquals('This is a comment', $tableInfo->table_comment);

Schema::drop('users');
}
}
23 changes: 23 additions & 0 deletions tests/Integration/Database/Postgres/PostgresSchemaBuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,29 @@ public function testDropAllViewsOnAllSchemas()
$this->assertFalse($this->hasView('private', 'foo'));
}

public function testAddTableCommentOnNewTable()
{
Schema::create('public.posts', function (Blueprint $table) {
$table->comment('This is a comment');
});

$this->assertEquals('This is a comment', DB::selectOne("select obj_description('public.posts'::regclass, 'pg_class')")->obj_description);
}

public function testAddTableCommentOnExistingTable()
{
Schema::create('public.posts', function (Blueprint $table) {
$table->id();
$table->comment('This is a comment');
});

Schema::table('public.posts', function (Blueprint $table) {
$table->comment('This is a new comment');
});

$this->assertEquals('This is a new comment', DB::selectOne("select obj_description('public.posts'::regclass, 'pg_class')")->obj_description);
}

protected function hasView($schema, $table)
{
return DB::table('information_schema.views')
Expand Down