Skip to content

Commit 997a271

Browse files
committed
better management of default parameter values in column definitions
1 parent fe9bd05 commit 997a271

12 files changed

+281
-227
lines changed

.wiki

Submodule .wiki updated from 0da7d85 to 60058e1

Readme.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
- [`Column`](#column)
2727
- [`Binary`](#binary)
2828
- [`Char`](#char)
29-
- [`CString`](#cstring)
29+
- [`CString`](#cstring "`String` is a reserved word in PHP")
3030
- [`Integer`](#integer)
3131
- [`TinyInteger`](#tinyinteger)
3232
- [`SmallInteger`](#smallinteger)
@@ -36,7 +36,7 @@
3636
- [`TinyIncrements`](#tinyincrements)
3737
- [`SmallIncrements`](#smallincrements)
3838
- [`MedumIncrements`](#mediumincrements)
39-
- [`CFloat`](#cfloat)
39+
- [`CFloat`](#cfloat "`Float` is a reserved word in PHP")
4040
- [`Decimal`](#decimal)
4141
- [`DateTime`](#datetime)
4242
- [`DateTimeTz`](#datetimetz)
@@ -345,7 +345,7 @@ Alias for `Column('binary', $name, $nullable, $default, fixed: $fixed, comment:
345345
Alias for `Column('char', $name, $nullable, $default, $length, collation: $collation, comment: $comment, virtualAs: $virtualAs, storedAs: $storedAs, after: $after)`
346346

347347

348-
## `CString`
348+
## [`CString`](## "`String` is a reserved wordin PHP")
349349

350350
##### Target: *`class`*, *`property`*
351351

@@ -489,7 +489,7 @@ Alias for `Column('mediumIncrements', $name, $nullable, $default, unsigned: true
489489
Alias for `Column('bigIncrements', $name, $nullable, $default, unsigned: true, autoIncrement: true, comment: $comment, virtualAs: $virtualAs, storedAs: $storedAs, after: $after)`
490490

491491

492-
## `CFloat`
492+
## [`CFloat`](## "`Float` is a reserved wordin PHP")
493493

494494
##### Target: *`class`*, *`property`*
495495

src/Attributes/Column.php

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,6 @@ class Column extends MigrationAttribute
9090
'collation',
9191
];
9292

93-
9493
protected bool $inferred = false;
9594

9695
public function __construct(
@@ -165,6 +164,20 @@ protected function validate(Blueprint $table)
165164
}
166165
}
167166

167+
public static function getParameters($type, $attributes): array
168+
{
169+
$parameters = [];
170+
foreach (static::PARAMETER_MAP[$type] ?? [] as $parameterName) {
171+
if (!array_key_exists($parameterName, $attributes)) {
172+
continue;
173+
}
174+
175+
$parameters[$parameterName] = $attributes[$parameterName];
176+
}
177+
178+
return $parameters;
179+
}
180+
168181
protected function process(Blueprint $table): Blueprint
169182
{
170183
try {
@@ -194,14 +207,7 @@ protected function process(Blueprint $table): Blueprint
194207
$attributes[$attributeName] = $this->{$attributeName};
195208
}
196209

197-
$parameters = [];
198-
foreach (static::PARAMETER_MAP[$this->type] ?? [] as $parameterName) {
199-
if (!isset($attributes[$parameterName])) {
200-
continue;
201-
}
202-
203-
$parameters[$parameterName] = $attributes[$parameterName];
204-
}
210+
$parameters = static::getParameters($this->type, $attributes);
205211

206212
if (
207213
!empty(array_diff(

src/Blueprint/BlueprintDiff.php

Lines changed: 153 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace Toramanlis\ImplicitMigrations\Blueprint;
44

55
use Exception;
6+
use Illuminate\Database\Schema\Blueprint;
67
use Illuminate\Database\Schema\ColumnDefinition;
78
use Illuminate\Support\Facades\App;
89
use Illuminate\Support\Fluent;
@@ -11,25 +12,45 @@
1112
class BlueprintDiff implements Migratable
1213
{
1314
/**
14-
* @param SimplifyingBlueprint $from
15-
* @param SimplifyingBlueprint $to
16-
* @param array<string> $modifiedColumns
15+
* @param array<string>
16+
*/
17+
public array $modifiedColumns;
18+
19+
/**
1720
* @param array<ColumnDefinition> $droppedColumns
21+
*/
22+
public array $droppedColumns;
23+
24+
/**
1825
* @param array<ColumnDefinition> $addedColumns
26+
*/
27+
public array $addedColumns;
28+
29+
/**
1930
* @param array<Fluent> $droppedIndexes
31+
*/
32+
public array $droppedIndexes;
33+
34+
/**
2035
* @param array<string, string> $renamedIndexes
36+
*/
37+
public array $renamedIndexes;
38+
39+
/**
2140
* @param array<Fluent> $addedIndexes
2241
*/
42+
public array $addedIndexes;
43+
44+
/**
45+
* @param SimplifyingBlueprint $from
46+
* @param SimplifyingBlueprint $to
47+
*/
2348
public function __construct(
2449
readonly public SimplifyingBlueprint $from,
25-
readonly public SimplifyingBlueprint $to,
26-
public array $modifiedColumns,
27-
public array $droppedColumns,
28-
public array $addedColumns,
29-
public array $droppedIndexes,
30-
public array $renamedIndexes,
31-
public array $addedIndexes
50+
readonly public SimplifyingBlueprint $to
3251
) {
52+
[$this->modifiedColumns, $this->droppedColumns, $this->addedColumns] = static::getColumnDiffs($from, $to);
53+
[$this->droppedIndexes, $this->renamedIndexes, $this->addedIndexes] = static::getIndexDiffs($from, $to);
3354
}
3455

3556
public function applyColumnIndexes(bool $reverse = false)
@@ -246,4 +267,126 @@ public function defaultIndexName(Fluent $index, bool $reverse = false)
246267
{
247268
return ($reverse ? $this->from : $this->to)->defaultIndexName($index);
248269
}
270+
271+
protected static function attributesEqual(Fluent $left, Fluent $right, array $exceptions = [])
272+
{
273+
$leftClone = clone $left;
274+
$rightClone = clone $right;
275+
276+
$leftAttributes = array_filter($leftClone->getAttributes(), fn ($i) => null !== $i);
277+
$rightAttributes = array_filter($rightClone->getAttributes(), fn ($i) => null !== $i);
278+
279+
ksort($leftAttributes);
280+
ksort($rightAttributes);
281+
282+
foreach ($exceptions as $exception) {
283+
unset($leftAttributes[$exception], $rightAttributes[$exception]);
284+
}
285+
286+
return $leftAttributes === $rightAttributes;
287+
}
288+
289+
/**
290+
* @param Blueprint $from
291+
* @param Blueprint $to
292+
* @return array
293+
*/
294+
protected static function getColumnDiffs(Blueprint $from, Blueprint $to): array
295+
{
296+
$unchangedColumns = [];
297+
$modifiedColumns = [];
298+
$droppedColumns = [];
299+
$addedColumns = [];
300+
301+
foreach ($from->getColumns() as $fromColumn) {
302+
foreach ($to->getColumns() as $toColumn) {
303+
if (in_array($toColumn->name, $unchangedColumns)) {
304+
continue;
305+
}
306+
307+
if ($fromColumn->name === $toColumn->name) {
308+
if (static::attributesEqual($fromColumn, $toColumn)) {
309+
$unchangedColumns[] = $fromColumn->name;
310+
continue 2;
311+
}
312+
313+
$modifiedColumns[] = $fromColumn->name;
314+
continue 2;
315+
}
316+
}
317+
318+
$droppedColumns[] = $fromColumn;
319+
}
320+
321+
foreach ($to->getColumns() as $toColumn) {
322+
if (
323+
in_array($toColumn->name, $unchangedColumns) ||
324+
in_array($toColumn->name, $modifiedColumns)
325+
) {
326+
continue;
327+
}
328+
329+
$addedColumns[] = $toColumn;
330+
}
331+
332+
return [$modifiedColumns, $droppedColumns, $addedColumns];
333+
}
334+
335+
/**
336+
* @param Blueprint $from
337+
* @param Blueprint $to
338+
* @return array
339+
*/
340+
protected static function getIndexDiffs(Blueprint $from, Blueprint $to): array
341+
{
342+
$unchangedIndexes = [];
343+
$droppedIndexes = [];
344+
$renamedIndexes = [];
345+
$addedIndexes = [];
346+
347+
foreach ($from->getCommands() as $fromCommand) {
348+
if (null === IndexType::tryFrom($fromCommand->name)) {
349+
continue;
350+
}
351+
352+
foreach ($to->getCommands() as $toCommand) {
353+
if (
354+
null === IndexType::tryFrom($toCommand->name) ||
355+
in_array($toCommand->index, $unchangedIndexes)
356+
) {
357+
continue;
358+
}
359+
360+
if (static::attributesEqual($fromCommand, $toCommand, ['index'])) {
361+
if ($fromCommand->index === $toCommand->index) {
362+
unset($renamedIndexes[$fromCommand->index]);
363+
$unchangedIndexes[] = $fromCommand->index;
364+
continue 2;
365+
}
366+
367+
$renamedIndexes[$fromCommand->index] = $toCommand->index;
368+
continue 2;
369+
}
370+
}
371+
372+
$droppedIndexes[] = $fromCommand;
373+
}
374+
375+
foreach ($to->getCommands() as $toCommand) {
376+
if (null === IndexType::tryFrom($toCommand->name)) {
377+
continue;
378+
}
379+
380+
if (
381+
in_array($toCommand->index, $unchangedIndexes) ||
382+
in_array($toCommand->index, $renamedIndexes)
383+
) {
384+
continue;
385+
}
386+
387+
$addedIndexes[] = $toCommand;
388+
}
389+
390+
return [$droppedIndexes, $renamedIndexes, $addedIndexes];
391+
}
249392
}

0 commit comments

Comments
 (0)