Skip to content

Commit

Permalink
Fix #237 Generate CURRENT_TIMESTAMP with precision
Browse files Browse the repository at this point in the history
  • Loading branch information
kitloong committed Nov 3, 2024
1 parent 29128c5 commit 3320d14
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 2 deletions.
20 changes: 19 additions & 1 deletion src/Database/Models/MySQL/MySQLColumn.php
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,8 @@ public function __construct(string $table, array $column)
case ColumnType::TIMESTAMP:
case ColumnType::TIMESTAMP_TZ:
$this->onUpdateCurrentTimestamp = $this->hasOnUpdateCurrentTimestamp();
$this->flattenCurrentTimestamp();

break;

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

if (Str::startsWith($columnDefault, 'current_timestamp')) {
return 'CURRENT_TIMESTAMP';
}

return match ($columnDefault) {
'current_timestamp()' => 'CURRENT_TIMESTAMP',
'curdate()' => 'CURRENT_DATE',
'curtime()' => 'CURRENT_TIME',
default => $columnDefault,
};
}

private function flattenCurrentTimestamp(): void
{
if ($this->default === null) {
return;
}

if (!Str::startsWith($this->default, 'CURRENT_TIMESTAMP')) {
return;
}

$this->default = 'CURRENT_TIMESTAMP';
}
}
2 changes: 1 addition & 1 deletion src/Repositories/MySQLRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public function isOnUpdateCurrentTimestamp(string $table, string $column): bool
$result = DB::selectOne(
"SHOW COLUMNS FROM `$table`
WHERE Field = '$column'
AND Type IN ('timestamp', 'datetime')
AND (Type LIKE 'timestamp%' OR Type LIKE 'datetime%')
AND Extra LIKE '%on update CURRENT_TIMESTAMP%'",
);
return !($result === null);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,10 +85,15 @@ public function up()

Schema::create('use_current_on_update', function (Blueprint $table) {
$table->increments('id');

$table->dateTime('datetime_precision_useCurrent', 2)->nullable()->useCurrent()->useCurrentOnUpdate();
$table->dateTime('timestamp_precision_useCurrent', 2)->nullable()->useCurrent()->useCurrentOnUpdate();

$table->dateTime('datetime_useCurrentOnUpdate_nullable_useCurrent')->useCurrentOnUpdate()->nullable()->useCurrent();
$table->dateTime('datetime_useCurrentOnUpdate_useCurrent')->useCurrentOnUpdate()->useCurrent();
$table->dateTime('datetime_nullable')->useCurrentOnUpdate()->nullable();
$table->dateTime('datetime_useCurrent')->useCurrent();

$table->timestamp('timestamp_useCurrentOnUpdate_nullable_useCurrent')->useCurrentOnUpdate()->nullable()->useCurrent();
$table->timestamp('timestamp_useCurrentOnUpdate_useCurrent')->useCurrentOnUpdate()->useCurrent();
$table->timestamp('timestamp_nullable')->useCurrentOnUpdate()->nullable();
Expand Down

0 comments on commit 3320d14

Please sign in to comment.