Skip to content

Commit

Permalink
Merge pull request #239 from kitloong/feature/fix
Browse files Browse the repository at this point in the history
Add `useCurrentOnUpdate` support to datetime column type
  • Loading branch information
kitloong authored Nov 3, 2024
2 parents 50a48f1 + cecd591 commit 3d036e3
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 4 deletions.
2 changes: 1 addition & 1 deletion .phpmd.xml
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
</rule>
<rule ref="rulesets/codesize.xml/CyclomaticComplexity">
<properties>
<property name="reportLevel" value="25"/>
<property name="reportLevel" value="30"/>
</properties>
</rule>
<rule ref="rulesets/codesize.xml/ExcessiveClassComplexity">
Expand Down
22 changes: 21 additions & 1 deletion src/Database/Models/MySQL/MySQLColumn.php
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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';
}
}
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 = 'timestamp'
AND (Type LIKE 'timestamp%' OR Type LIKE 'datetime%')
AND Extra LIKE '%on update CURRENT_TIMESTAMP%'",
);
return !($result === null);
Expand Down
2 changes: 1 addition & 1 deletion src/Schema/Models/Column.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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');
});
}

/**
Expand All @@ -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');
}
};

0 comments on commit 3d036e3

Please sign in to comment.