Skip to content

Commit 3d036e3

Browse files
authored
Merge pull request #239 from kitloong/feature/fix
Add `useCurrentOnUpdate` support to datetime column type
2 parents 50a48f1 + cecd591 commit 3d036e3

File tree

5 files changed

+43
-4
lines changed

5 files changed

+43
-4
lines changed

.phpmd.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
</rule>
2626
<rule ref="rulesets/codesize.xml/CyclomaticComplexity">
2727
<properties>
28-
<property name="reportLevel" value="25"/>
28+
<property name="reportLevel" value="30"/>
2929
</properties>
3030
</rule>
3131
<rule ref="rulesets/codesize.xml/ExcessiveClassComplexity">

src/Database/Models/MySQL/MySQLColumn.php

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,9 +76,13 @@ public function __construct(string $table, array $column)
7676

7777
case ColumnType::SOFT_DELETES:
7878
case ColumnType::SOFT_DELETES_TZ:
79+
case ColumnType::DATETIME:
80+
case ColumnType::DATETIME_TZ:
7981
case ColumnType::TIMESTAMP:
8082
case ColumnType::TIMESTAMP_TZ:
8183
$this->onUpdateCurrentTimestamp = $this->hasOnUpdateCurrentTimestamp();
84+
$this->flattenCurrentTimestamp();
85+
8286
break;
8387

8488
case ColumnType::GEOGRAPHY:
@@ -343,11 +347,27 @@ private function getMariaDBColumnDefault(?string $columnDefault): ?string
343347
return strtr($matches[1], self::MARIADB_ESCAPE_SEQUENCES);
344348
}
345349

350+
if (Str::startsWith($columnDefault, 'current_timestamp')) {
351+
return 'CURRENT_TIMESTAMP';
352+
}
353+
346354
return match ($columnDefault) {
347-
'current_timestamp()' => 'CURRENT_TIMESTAMP',
348355
'curdate()' => 'CURRENT_DATE',
349356
'curtime()' => 'CURRENT_TIME',
350357
default => $columnDefault,
351358
};
352359
}
360+
361+
private function flattenCurrentTimestamp(): void
362+
{
363+
if ($this->default === null) {
364+
return;
365+
}
366+
367+
if (!Str::startsWith($this->default, 'CURRENT_TIMESTAMP')) {
368+
return;
369+
}
370+
371+
$this->default = 'CURRENT_TIMESTAMP';
372+
}
353373
}

src/Repositories/MySQLRepository.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ public function isOnUpdateCurrentTimestamp(string $table, string $column): bool
3636
$result = DB::selectOne(
3737
"SHOW COLUMNS FROM `$table`
3838
WHERE Field = '$column'
39-
AND Type = 'timestamp'
39+
AND (Type LIKE 'timestamp%' OR Type LIKE 'datetime%')
4040
AND Extra LIKE '%on update CURRENT_TIMESTAMP%'",
4141
);
4242
return !($result === null);

src/Schema/Models/Column.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ public function getSpatialSrID(): ?int;
9595

9696
/**
9797
* Check if the column uses "on update CURRENT_TIMESTAMP".
98-
* This is usually used for MySQL `timestamp` and `timestampTz`.
98+
* This is usually used for MySQL `timestamp` and `datetime`.
9999
*/
100100
public function isOnUpdateCurrentTimestamp(): bool;
101101

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

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,24 @@ public function up()
8282
$table->timestampTz('created_at')->nullable();
8383
$table->timestampTz('update_at')->nullable()->useCurrent()->useCurrentOnUpdate();
8484
});
85+
86+
Schema::create('use_current_on_update', function (Blueprint $table) {
87+
$table->increments('id');
88+
89+
$table->dateTime('datetime_precision_useCurrent', 2)->nullable()->useCurrent()->useCurrentOnUpdate();
90+
$table->dateTime('timestamp_precision_useCurrent', 2)->nullable()->useCurrent()->useCurrentOnUpdate();
91+
92+
$table->dateTime('datetime_useCurrentOnUpdate_nullable_useCurrent')->useCurrentOnUpdate()->nullable()->useCurrent();
93+
$table->dateTime('datetime_useCurrentOnUpdate_useCurrent')->useCurrentOnUpdate()->useCurrent();
94+
$table->dateTime('datetime_nullable')->useCurrentOnUpdate()->nullable();
95+
$table->dateTime('datetime_useCurrent')->useCurrent();
96+
97+
$table->timestamp('timestamp_useCurrentOnUpdate_nullable_useCurrent')->useCurrentOnUpdate()->nullable()->useCurrent();
98+
$table->timestamp('timestamp_useCurrentOnUpdate_useCurrent')->useCurrentOnUpdate()->useCurrent();
99+
$table->timestamp('timestamp_nullable')->useCurrentOnUpdate()->nullable();
100+
$table->timestamp('timestamp_useCurrent')->useCurrent();
101+
$table->timestamp('timestamp_useCurrentOnUpdate')->useCurrentOnUpdate()->default('2024-10-08 00:00:00');
102+
});
85103
}
86104

87105
/**
@@ -99,5 +117,6 @@ public function down()
99117
Schema::dropIfExists('not_timestamps2');
100118
Schema::dropIfExists('not_timestamps3');
101119
Schema::dropIfExists('not_timestamps4');
120+
Schema::dropIfExists('use_current_on_update');
102121
}
103122
};

0 commit comments

Comments
 (0)