diff --git a/.phpmd.xml b/.phpmd.xml index c1b641b..73cf3f8 100644 --- a/.phpmd.xml +++ b/.phpmd.xml @@ -25,7 +25,7 @@ - + diff --git a/src/Database/Models/MySQL/MySQLColumn.php b/src/Database/Models/MySQL/MySQLColumn.php index 64fa13f..0791aa9 100644 --- a/src/Database/Models/MySQL/MySQLColumn.php +++ b/src/Database/Models/MySQL/MySQLColumn.php @@ -76,9 +76,13 @@ public function __construct(string $table, array $column) case ColumnType::SOFT_DELETES: case ColumnType::SOFT_DELETES_TZ: + case ColumnType::DATETIME: + case ColumnType::DATETIME_TZ: case ColumnType::TIMESTAMP: case ColumnType::TIMESTAMP_TZ: $this->onUpdateCurrentTimestamp = $this->hasOnUpdateCurrentTimestamp(); + $this->flattenCurrentTimestamp(); + break; case ColumnType::GEOGRAPHY: @@ -343,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'; + } } diff --git a/src/Repositories/MySQLRepository.php b/src/Repositories/MySQLRepository.php index 32796c2..cbdc9d6 100644 --- a/src/Repositories/MySQLRepository.php +++ b/src/Repositories/MySQLRepository.php @@ -36,7 +36,7 @@ public function isOnUpdateCurrentTimestamp(string $table, string $column): bool $result = DB::selectOne( "SHOW COLUMNS FROM `$table` WHERE Field = '$column' - AND Type = 'timestamp' + AND (Type LIKE 'timestamp%' OR Type LIKE 'datetime%') AND Extra LIKE '%on update CURRENT_TIMESTAMP%'", ); return !($result === null); diff --git a/src/Schema/Models/Column.php b/src/Schema/Models/Column.php index 7139a3a..c0a9b09 100644 --- a/src/Schema/Models/Column.php +++ b/src/Schema/Models/Column.php @@ -95,7 +95,7 @@ public function getSpatialSrID(): ?int; /** * Check if the column uses "on update CURRENT_TIMESTAMP". - * This is usually used for MySQL `timestamp` and `timestampTz`. + * This is usually used for MySQL `timestamp` and `datetime`. */ public function isOnUpdateCurrentTimestamp(): bool; diff --git a/tests/resources/database/migrations/general/2020_03_21_000000_expected_create_timestamps_table.php b/tests/resources/database/migrations/general/2020_03_21_000000_expected_create_timestamps_table.php index c8b31ae..a250361 100644 --- a/tests/resources/database/migrations/general/2020_03_21_000000_expected_create_timestamps_table.php +++ b/tests/resources/database/migrations/general/2020_03_21_000000_expected_create_timestamps_table.php @@ -82,6 +82,24 @@ public function up() $table->timestampTz('created_at')->nullable(); $table->timestampTz('update_at')->nullable()->useCurrent()->useCurrentOnUpdate(); }); + + 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(); + $table->timestamp('timestamp_useCurrent')->useCurrent(); + $table->timestamp('timestamp_useCurrentOnUpdate')->useCurrentOnUpdate()->default('2024-10-08 00:00:00'); + }); } /** @@ -99,5 +117,6 @@ public function down() Schema::dropIfExists('not_timestamps2'); Schema::dropIfExists('not_timestamps3'); Schema::dropIfExists('not_timestamps4'); + Schema::dropIfExists('use_current_on_update'); } };