Skip to content

Commit cecd591

Browse files
authored
Merge pull request #240 from kitloong/feature/current-precision
Fix #237 Generate `CURRENT_TIMESTAMP` with precision
2 parents 29128c5 + 3320d14 commit cecd591

File tree

3 files changed

+25
-2
lines changed

3 files changed

+25
-2
lines changed

src/Database/Models/MySQL/MySQLColumn.php

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,8 @@ public function __construct(string $table, array $column)
8181
case ColumnType::TIMESTAMP:
8282
case ColumnType::TIMESTAMP_TZ:
8383
$this->onUpdateCurrentTimestamp = $this->hasOnUpdateCurrentTimestamp();
84+
$this->flattenCurrentTimestamp();
85+
8486
break;
8587

8688
case ColumnType::GEOGRAPHY:
@@ -345,11 +347,27 @@ private function getMariaDBColumnDefault(?string $columnDefault): ?string
345347
return strtr($matches[1], self::MARIADB_ESCAPE_SEQUENCES);
346348
}
347349

350+
if (Str::startsWith($columnDefault, 'current_timestamp')) {
351+
return 'CURRENT_TIMESTAMP';
352+
}
353+
348354
return match ($columnDefault) {
349-
'current_timestamp()' => 'CURRENT_TIMESTAMP',
350355
'curdate()' => 'CURRENT_DATE',
351356
'curtime()' => 'CURRENT_TIME',
352357
default => $columnDefault,
353358
};
354359
}
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+
}
355373
}

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 IN ('timestamp', 'datetime')
39+
AND (Type LIKE 'timestamp%' OR Type LIKE 'datetime%')
4040
AND Extra LIKE '%on update CURRENT_TIMESTAMP%'",
4141
);
4242
return !($result === null);

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,10 +85,15 @@ public function up()
8585

8686
Schema::create('use_current_on_update', function (Blueprint $table) {
8787
$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+
8892
$table->dateTime('datetime_useCurrentOnUpdate_nullable_useCurrent')->useCurrentOnUpdate()->nullable()->useCurrent();
8993
$table->dateTime('datetime_useCurrentOnUpdate_useCurrent')->useCurrentOnUpdate()->useCurrent();
9094
$table->dateTime('datetime_nullable')->useCurrentOnUpdate()->nullable();
9195
$table->dateTime('datetime_useCurrent')->useCurrent();
96+
9297
$table->timestamp('timestamp_useCurrentOnUpdate_nullable_useCurrent')->useCurrentOnUpdate()->nullable()->useCurrent();
9398
$table->timestamp('timestamp_useCurrentOnUpdate_useCurrent')->useCurrentOnUpdate()->useCurrent();
9499
$table->timestamp('timestamp_nullable')->useCurrentOnUpdate()->nullable();

0 commit comments

Comments
 (0)