Skip to content

Commit f76e20e

Browse files
authored
Generate table comment (#144)
Generate table comment
1 parent b8a6c0e commit f76e20e

File tree

6 files changed

+60
-0
lines changed

6 files changed

+60
-0
lines changed

src/DBAL/Models/DBALTable.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,11 @@ abstract class DBALTable implements Table
3434
*/
3535
protected $name;
3636

37+
/**
38+
* @var string
39+
*/
40+
protected $comment;
41+
3742
/**
3843
* @var \Illuminate\Support\Collection<\KitLoong\MigrationsGenerator\Schema\Models\Index>
3944
*/
@@ -49,6 +54,7 @@ abstract class DBALTable implements Table
4954
public function __construct(DoctrineDBALTable $table, array $columns, array $indexes)
5055
{
5156
$this->name = $table->getName();
57+
$this->comment = $table->getComment();
5258
$this->collation = $table->getOptions()['collation'] ?? null;
5359

5460
$this->columns = (new Collection($columns))->reduce(function (Collection $columns, DoctrineDBALColumn $column) use ($table) {
@@ -116,6 +122,14 @@ public function getName(): string
116122
return $this->name;
117123
}
118124

125+
/**
126+
* @inheritDoc
127+
*/
128+
public function getComment(): ?string
129+
{
130+
return $this->comment;
131+
}
132+
119133
/**
120134
* @inheritDoc
121135
*/
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?php
2+
3+
namespace KitLoong\MigrationsGenerator\Enum\Migrations\Method;
4+
5+
use MyCLabs\Enum\Enum;
6+
7+
/**
8+
* Preserved table methods of the framework.
9+
*
10+
* @see https://laravel.com/docs/master/migrations#tables
11+
* @method static self COMMENT()
12+
*/
13+
final class TableMethod extends Enum
14+
{
15+
private const COMMENT = 'comment';
16+
}

src/Migration/TableMigration.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,10 @@
77
use Illuminate\Support\Str;
88
use KitLoong\MigrationsGenerator\Enum\Driver;
99
use KitLoong\MigrationsGenerator\Enum\Migrations\Method\SchemaBuilder;
10+
use KitLoong\MigrationsGenerator\Enum\Migrations\Method\TableMethod;
1011
use KitLoong\MigrationsGenerator\Enum\Migrations\Property\TableProperty;
1112
use KitLoong\MigrationsGenerator\Migration\Blueprint\DBStatementBlueprint;
13+
use KitLoong\MigrationsGenerator\Migration\Blueprint\Method;
1214
use KitLoong\MigrationsGenerator\Migration\Blueprint\SchemaBlueprint;
1315
use KitLoong\MigrationsGenerator\Migration\Blueprint\TableBlueprint;
1416
use KitLoong\MigrationsGenerator\Migration\Enum\MigrationFileType;
@@ -18,11 +20,13 @@
1820
use KitLoong\MigrationsGenerator\Migration\Writer\SquashWriter;
1921
use KitLoong\MigrationsGenerator\Schema\Models\Table;
2022
use KitLoong\MigrationsGenerator\Setting;
23+
use KitLoong\MigrationsGenerator\Support\CheckMigrationMethod;
2124
use KitLoong\MigrationsGenerator\Support\MigrationNameHelper;
2225
use KitLoong\MigrationsGenerator\Support\TableName;
2326

2427
class TableMigration
2528
{
29+
use CheckMigrationMethod;
2630
use TableName;
2731

2832
private $columnGenerator;
@@ -117,6 +121,10 @@ private function up(Table $table): SchemaBlueprint
117121
$blueprint->setLineBreak();
118122
}
119123

124+
if ($this->hasTableComment() && $table->getComment() !== null) {
125+
$blueprint->setMethod(new Method(TableMethod::COMMENT(), $table->getComment()));
126+
}
127+
120128
$chainableIndexes = $this->indexGenerator->getChainableIndexes($table->getName(), $table->getIndexes());
121129
$notChainableIndexes = $this->indexGenerator->getNotChainableIndexes($table->getIndexes(), $chainableIndexes);
122130

src/Schema/Models/Table.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,13 @@ interface Table extends Model
1313
*/
1414
public function getName(): string;
1515

16+
/**
17+
* Get the table comment.
18+
*
19+
* @return string|null
20+
*/
21+
public function getComment(): ?string;
22+
1623
/**
1724
* Get a list of columns.
1825
*

src/Support/CheckMigrationMethod.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,4 +50,15 @@ public function hasAnonymousMigration(): bool
5050
{
5151
return method_exists(Migrator::class, 'getMigrationClass');
5252
}
53+
54+
/**
55+
* Check if support add comment to a table.
56+
* This feature is added since Laravel v9.
57+
*
58+
* @return bool
59+
*/
60+
public function hasTableComment(): bool
61+
{
62+
return method_exists(Blueprint::class, 'comment');
63+
}
5364
}

tests/resources/database/migrations/general/2020_03_21_000000_expected_create_all_columns_db_table.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,10 @@ class ExpectedCreateAllColumns_DB_Table extends TestMigration
2323
public function up()
2424
{
2525
Schema::create('all_columns_[db]', function (Blueprint $table) {
26+
if ($this->hasTableComment()) {
27+
$table->comment('A table comment.');
28+
}
29+
2630
$table->bigInteger('bigInteger');
2731
$table->bigInteger('bigInteger_default')->default(1080);
2832
$table->binary('binary');

0 commit comments

Comments
 (0)