Skip to content

Commit 8b35752

Browse files
authored
Merge pull request #228 from kitloong/feature/index
Generate the index with user-defined type column after the column is generated
2 parents 6c2c786 + 7a16b2c commit 8b35752

File tree

13 files changed

+128
-20
lines changed

13 files changed

+128
-20
lines changed

src/Database/Models/DatabaseIndex.php

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,17 +21,23 @@ abstract class DatabaseIndex implements Index
2121

2222
protected IndexType $type;
2323

24+
/**
25+
* @var string[]
26+
*/
27+
protected array $udtColumnSqls;
28+
2429
/**
2530
* Create an index instance.
2631
*
2732
* @param SchemaIndex $index
2833
*/
2934
public function __construct(string $table, array $index)
3035
{
31-
$this->tableName = $table;
32-
$this->name = $index['name'];
33-
$this->columns = $index['columns'];
34-
$this->type = $this->getIndexType($index);
36+
$this->tableName = $table;
37+
$this->name = $index['name'];
38+
$this->columns = $index['columns'];
39+
$this->type = $this->getIndexType($index);
40+
$this->udtColumnSqls = [];
3541
}
3642

3743
/**
@@ -66,6 +72,22 @@ public function getType(): IndexType
6672
return $this->type;
6773
}
6874

75+
/**
76+
* @inheritDoc
77+
*/
78+
public function getUDTColumnSqls(): array
79+
{
80+
return $this->udtColumnSqls;
81+
}
82+
83+
/**
84+
* @inheritDoc
85+
*/
86+
public function hasUDTColumn(): bool
87+
{
88+
return count($this->udtColumnSqls) > 0;
89+
}
90+
6991
/**
7092
* Get the index type.
7193
*

src/Database/Models/DatabaseTable.php

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -55,14 +55,14 @@ abstract protected function makeUDTColumn(string $table, array $column): UDTColu
5555
*
5656
* @param SchemaIndex $index
5757
*/
58-
abstract protected function makeIndex(string $table, array $index): Index;
58+
abstract protected function makeIndex(string $table, array $index, bool $hasUDTColumn): Index;
5959

6060
/**
6161
* Create a new instance.
6262
*
6363
* @param SchemaTable $table
64-
* @param \Illuminate\Support\Collection<int, SchemaColumn> $columns Key is quoted name.
65-
* @param \Illuminate\Support\Collection<int, SchemaIndex> $indexes Key is name.
64+
* @param \Illuminate\Support\Collection<int, SchemaColumn> $columns
65+
* @param \Illuminate\Support\Collection<int, SchemaIndex> $indexes
6666
* @param \Illuminate\Support\Collection<int, string> $userDefinedTypes
6767
*/
6868
public function __construct(array $table, Collection $columns, Collection $indexes, Collection $userDefinedTypes)
@@ -87,7 +87,14 @@ public function __construct(array $table, Collection $columns, Collection $index
8787
return $columns;
8888
}, new Collection())->values();
8989

90-
$this->indexes = $indexes->map(fn (array $index) => $this->makeIndex($this->name, $index))->values();
90+
$this->indexes = $indexes->map(function (array $index) {
91+
$hasUdtColumn = $this->udtColumns
92+
->map(static fn ($column) => $column->getName())
93+
->intersect($index['columns'])
94+
->isNotEmpty();
95+
96+
return $this->makeIndex($this->name, $index, $hasUdtColumn);
97+
})->values();
9198
}
9299

93100
/**

src/Database/Models/MySQL/MySQLTable.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ protected function makeUDTColumn(string $table, array $column): UDTColumn
2828
/**
2929
* @inheritDoc
3030
*/
31-
protected function makeIndex(string $table, array $index): Index
31+
protected function makeIndex(string $table, array $index, bool $hasUDTColumn): Index
3232
{
3333
return new MySQLIndex($table, $index);
3434
}

src/Database/Models/PgSQL/PgSQLIndex.php

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,20 @@
22

33
namespace KitLoong\MigrationsGenerator\Database\Models\PgSQL;
44

5+
use Illuminate\Database\Schema\Blueprint;
6+
use Illuminate\Support\Facades\Schema;
57
use KitLoong\MigrationsGenerator\Database\Models\DatabaseIndex;
68
use KitLoong\MigrationsGenerator\Enum\Migrations\Method\IndexType;
9+
use KitLoong\MigrationsGenerator\Support\TableName;
710

811
class PgSQLIndex extends DatabaseIndex
912
{
13+
use TableName;
14+
1015
/**
1116
* @inheritDoc
1217
*/
13-
public function __construct(string $table, array $index)
18+
public function __construct(string $table, array $index, bool $hasUDTColumn)
1419
{
1520
parent::__construct($table, $index);
1621

@@ -22,5 +27,16 @@ public function __construct(string $table, array $index)
2227

2328
default:
2429
}
30+
31+
if (!$hasUDTColumn) {
32+
return;
33+
}
34+
35+
$blueprint = new Blueprint($this->stripTablePrefix($table));
36+
37+
// Generate the alter index statement.
38+
$blueprint->{$this->type->value}($this->columns, $this->name);
39+
40+
$this->udtColumnSqls = $blueprint->toSql(Schema::getConnection(), Schema::getConnection()->getSchemaGrammar());
2541
}
2642
}

src/Database/Models/PgSQL/PgSQLTable.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,9 +48,9 @@ protected function makeUDTColumn(string $table, array $column): UDTColumn
4848
/**
4949
* @inheritDoc
5050
*/
51-
protected function makeIndex(string $table, array $index): Index
51+
protected function makeIndex(string $table, array $index, bool $hasUDTColumn): Index
5252
{
53-
return new PgSQLIndex($table, $index);
53+
return new PgSQLIndex($table, $index, $hasUDTColumn);
5454
}
5555

5656
/**
@@ -87,6 +87,7 @@ private function updateFulltextIndexes(): void
8787
'unique' => false,
8888
'primary' => false,
8989
],
90+
false,
9091
);
9192
});
9293
}

src/Database/Models/PgSQL/PgSQLUDTColumn.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@ public function __construct(string $table, array $column)
2323
parent::__construct($table, $column);
2424

2525
$blueprint = new Blueprint($this->stripTablePrefix($table));
26+
27+
// Generate the add column statement with string column type.
2628
$blueprint->addColumn('string', $column['name'], [
2729
'autoIncrement' => $column['auto_increment'],
2830
'collation' => $column['collation'],
@@ -31,7 +33,9 @@ public function __construct(string $table, array $column)
3133
'nullable' => $column['nullable'],
3234
]);
3335

34-
$sqls = $blueprint->toSql(Schema::getConnection(), Schema::getConnection()->getSchemaGrammar());
36+
$sqls = $blueprint->toSql(Schema::getConnection(), Schema::getConnection()->getSchemaGrammar());
37+
38+
// Replace the string column type with the user-defined type.
3539
$sqls[0] = Str::replaceFirst(' varchar ', ' ' . $column['type'] . ' ', $sqls[0]);
3640

3741
$this->sqls = $sqls;

src/Database/Models/SQLSrv/SQLSrvIndex.php

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,21 @@
22

33
namespace KitLoong\MigrationsGenerator\Database\Models\SQLSrv;
44

5+
use Illuminate\Database\Schema\Blueprint;
6+
use Illuminate\Support\Facades\Schema;
57
use Illuminate\Support\Str;
68
use KitLoong\MigrationsGenerator\Database\Models\DatabaseIndex;
79
use KitLoong\MigrationsGenerator\Enum\Migrations\Method\IndexType;
10+
use KitLoong\MigrationsGenerator\Support\TableName;
811

912
class SQLSrvIndex extends DatabaseIndex
1013
{
14+
use TableName;
15+
1116
/**
1217
* @inheritDoc
1318
*/
14-
public function __construct(string $table, array $index)
19+
public function __construct(string $table, array $index, bool $hasUDTColumn)
1520
{
1621
parent::__construct($table, $index);
1722

@@ -22,6 +27,17 @@ public function __construct(string $table, array $index)
2227

2328
default:
2429
}
30+
31+
if (!$hasUDTColumn) {
32+
return;
33+
}
34+
35+
$blueprint = new Blueprint($this->stripTablePrefix($table));
36+
37+
// Generate the alter index statement.
38+
$blueprint->{$this->type->value}($this->columns, $this->name);
39+
40+
$this->udtColumnSqls = $blueprint->toSql(Schema::getConnection(), Schema::getConnection()->getSchemaGrammar());
2541
}
2642

2743
/**

src/Database/Models/SQLSrv/SQLSrvTable.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,8 @@ protected function makeUDTColumn(string $table, array $column): UDTColumn
2828
/**
2929
* @inheritDoc
3030
*/
31-
protected function makeIndex(string $table, array $index): Index
31+
protected function makeIndex(string $table, array $index, bool $hasUDTColumn): Index
3232
{
33-
return new SQLSrvIndex($table, $index);
33+
return new SQLSrvIndex($table, $index, $hasUDTColumn);
3434
}
3535
}

src/Database/Models/SQLSrv/SQLSrvUDTColumn.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,13 +22,17 @@ public function __construct(string $table, array $column)
2222
parent::__construct($table, $column);
2323

2424
$blueprint = new Blueprint($this->stripTablePrefix($table));
25+
26+
// Generate the add column statement with string column type.
2527
$blueprint->addColumn('string', $column['name'], [
2628
'autoIncrement' => $column['auto_increment'],
2729
'default' => $this->parseDefault($column['default']),
2830
'nullable' => $column['nullable'],
2931
]);
3032

31-
$sqls = $blueprint->toSql(Schema::getConnection(), Schema::getConnection()->getSchemaGrammar());
33+
$sqls = $blueprint->toSql(Schema::getConnection(), Schema::getConnection()->getSchemaGrammar());
34+
35+
// Replace the string column type with the user-defined type.
3236
$sqls[0] = Str::replaceFirst(' nvarchar() ', ' ' . $column['type'] . ' ', $sqls[0]);
3337

3438
$this->sqls = $sqls;

src/Database/Models/SQLite/SQLiteTable.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ protected function makeUDTColumn(string $table, array $column): UDTColumn
2828
/**
2929
* @inheritDoc
3030
*/
31-
protected function makeIndex(string $table, array $index): Index
31+
protected function makeIndex(string $table, array $index, bool $hasUDTColumn): Index
3232
{
3333
return new SQLiteIndex($table, $index);
3434
}

src/Migration/TableMigration.php

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
use KitLoong\MigrationsGenerator\Migration\Generator\IndexGenerator;
1919
use KitLoong\MigrationsGenerator\Migration\Writer\MigrationWriter;
2020
use KitLoong\MigrationsGenerator\Migration\Writer\SquashWriter;
21+
use KitLoong\MigrationsGenerator\Schema\Models\Index;
2122
use KitLoong\MigrationsGenerator\Schema\Models\Table;
2223
use KitLoong\MigrationsGenerator\Setting;
2324
use KitLoong\MigrationsGenerator\Support\MigrationNameHelper;
@@ -104,8 +105,10 @@ private function up(Table $table): SchemaBlueprint
104105
$blueprint->setMethod(new Method(TableMethod::COMMENT, $table->getComment()));
105106
}
106107

107-
$chainableIndexes = $this->indexGenerator->getChainableIndexes($table->getName(), $table->getIndexes());
108-
$notChainableIndexes = $this->indexGenerator->getNotChainableIndexes($table->getIndexes(), $chainableIndexes);
108+
$indexes = $table->getIndexes()->filter(static fn (Index $index) => !$index->hasUdtColumn());
109+
110+
$chainableIndexes = $this->indexGenerator->getChainableIndexes($table->getName(), $indexes);
111+
$notChainableIndexes = $this->indexGenerator->getNotChainableIndexes($indexes, $chainableIndexes);
109112

110113
foreach ($table->getColumns() as $column) {
111114
$method = $this->columnGenerator->generate($table, $column, $chainableIndexes);
@@ -143,6 +146,14 @@ private function upAdditionalStatements(Table $table): array
143146
}
144147
}
145148

149+
$indexes = $table->getIndexes()->filter(static fn (Index $index) => $index->hasUdtColumn());
150+
151+
foreach ($indexes as $index) {
152+
foreach ($index->getUDTColumnSqls() as $sql) {
153+
$statements[] = new DBStatementBlueprint($sql);
154+
}
155+
}
156+
146157
return $statements;
147158
}
148159

src/Schema/Models/Index.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,4 +28,18 @@ public function getColumns(): array;
2828
* Get the index type.
2929
*/
3030
public function getType(): IndexType;
31+
32+
/**
33+
* Get the raw add index SQL queries.
34+
* This method is used when the index has a user-defined type column.
35+
* If the index does not have a user-defined type column, this method will return an empty array.
36+
*
37+
* @return string[]
38+
*/
39+
public function getUDTColumnSqls(): array;
40+
41+
/**
42+
* Indicates if this index column(s) is a user-defined type.
43+
*/
44+
public function hasUDTColumn(): bool;
3145
}

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

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -212,23 +212,36 @@ public function up()
212212
"ALTER TABLE ".DB::getTablePrefix()."all_columns ADD COLUMN timestamp_defaultnow timestamp(0) without time zone DEFAULT now() NOT NULL",
213213
);
214214

215+
// Test user defined type column.
215216
DB::statement(
216217
"ALTER TABLE ".DB::getTablePrefix()."all_columns ADD COLUMN status my_status NOT NULL DEFAULT 'PENDING'",
217218
);
218219

220+
// Add a comment so the alter statement will return 2 statements.
219221
DB::statement(
220222
"COMMENT ON column ".DB::getTablePrefix()."all_columns.status IS 'comment a'",
221223
);
222224

225+
// Test having index on user defined type column.
226+
DB::statement(
227+
"CREATE INDEX idx_status ON ".DB::getTablePrefix()."all_columns (status)",
228+
);
229+
223230
DB::statement(
224231
"ALTER TABLE ".DB::getTablePrefix()."all_columns ADD COLUMN timestamp_default_timezone_now timestamp(0) without time zone DEFAULT timezone('Europe/Rome'::text, now()) NOT NULL",
225232
);
226233
break;
227234

228235
case Driver::SQLSRV->value:
236+
// Test user defined type column.
229237
DB::statement(
230238
"ALTER TABLE ".DB::getTablePrefix()."all_columns ADD accountnumber accountnumber NOT NULL DEFAULT '1008'",
231239
);
240+
241+
// Test having index on user defined type column.
242+
DB::statement(
243+
"CREATE INDEX idx_accountnumber ON " . DB::getTablePrefix() . "all_columns (accountnumber)",
244+
);
232245
break;
233246
}
234247
}

0 commit comments

Comments
 (0)