diff --git a/.github/workflows/tests.yml b/.github/workflows/tests.yml index 2550067..95e648a 100644 --- a/.github/workflows/tests.yml +++ b/.github/workflows/tests.yml @@ -77,7 +77,7 @@ jobs: strategy: matrix: - php: [ 8.2, 8.3 ] + php: [ 8.2, 8.3, 8.4 ] laravel: [ 11.* ] sqlsrv_extension: [ pdo_sqlsrv ] include: 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/Database/Models/PgSQL/PgSQLColumn.php b/src/Database/Models/PgSQL/PgSQLColumn.php index f34a71d..5696f86 100644 --- a/src/Database/Models/PgSQL/PgSQLColumn.php +++ b/src/Database/Models/PgSQL/PgSQLColumn.php @@ -164,7 +164,7 @@ private function setRealSpatialColumn(string $fullDefinitionType): void return; } - if (!preg_match('/(\w+)(?:\((\w+)(?:,\s*(\w+))?\))?/', $dataType, $matches)) { + if (!preg_match('/(\w+)(?:\((\w+)(?:,\s*(\w+))?\))?/', $dataType, $matches) || !isset($matches[2])) { return; } diff --git a/src/Migration/Generator/ForeignKeyGenerator.php b/src/Migration/Generator/ForeignKeyGenerator.php index 91d5be1..9a0c319 100644 --- a/src/Migration/Generator/ForeignKeyGenerator.php +++ b/src/Migration/Generator/ForeignKeyGenerator.php @@ -58,10 +58,6 @@ public function makeMethod(ForeignKey $foreignKey): Method return new Method(Foreign::FOREIGN, $foreignKey->getLocalColumns()); } - if ($foreignKey->getName() === null) { - return new Method(Foreign::FOREIGN, $foreignKey->getLocalColumns()); - } - return new Method(Foreign::FOREIGN, $foreignKey->getLocalColumns(), $foreignKey->getName()); } 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/Feature/SQLite/SQLiteTestCase.php b/tests/Feature/SQLite/SQLiteTestCase.php index 599dee5..2be8458 100644 --- a/tests/Feature/SQLite/SQLiteTestCase.php +++ b/tests/Feature/SQLite/SQLiteTestCase.php @@ -14,7 +14,7 @@ protected function getEnvironmentSetUp($app): void { parent::getEnvironmentSetUp($app); - touch(env('SQLITE_DATABASE')); + touch((string) env('SQLITE_DATABASE')); $app['config']->set('database.default', 'sqlite'); $app['config']->set('database.connections.sqlite', [ diff --git a/tests/Unit/Migration/Generator/ForeignKeyGeneratorTest.php b/tests/Unit/Migration/Generator/ForeignKeyGeneratorTest.php new file mode 100644 index 0000000..7fe7927 --- /dev/null +++ b/tests/Unit/Migration/Generator/ForeignKeyGeneratorTest.php @@ -0,0 +1,33 @@ +setIgnoreForeignKeyNames(false); + + $foreignKeyGenerator = app(ForeignKeyGenerator::class); + + $method = $foreignKeyGenerator->generateDrop(new SQLiteForeignKey('table', [ + 'name' => null, + 'columns' => ['column'], + 'foreign_schema' => null, + 'foreign_table' => 'foreign_table', + 'foreign_columns' => ['foreign_column'], + 'on_update' => 'on_update', + 'on_delete' => 'on_delete', + ])); + + $this->assertSame($method->getName(), Foreign::DROP_FOREIGN); + $this->assertEmpty($method->getValues()); + } +} 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'); } };