Skip to content

Commit

Permalink
Merge pull request #241 from kitloong/7.x
Browse files Browse the repository at this point in the history
Merge 7.x
  • Loading branch information
kitloong authored Nov 3, 2024
2 parents 2577c4e + 3d036e3 commit b9bc2bd
Show file tree
Hide file tree
Showing 10 changed files with 79 additions and 11 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
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/Database/Models/PgSQL/PgSQLColumn.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down
4 changes: 0 additions & 4 deletions src/Migration/Generator/ForeignKeyGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}

Expand Down
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
2 changes: 1 addition & 1 deletion tests/Feature/SQLite/SQLiteTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -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', [
Expand Down
33 changes: 33 additions & 0 deletions tests/Unit/Migration/Generator/ForeignKeyGeneratorTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php

namespace KitLoong\MigrationsGenerator\Tests\Unit\Migration\Generator;

use KitLoong\MigrationsGenerator\Database\Models\SQLite\SQLiteForeignKey;
use KitLoong\MigrationsGenerator\Enum\Migrations\Method\Foreign;
use KitLoong\MigrationsGenerator\Migration\Generator\ForeignKeyGenerator;
use KitLoong\MigrationsGenerator\Setting;
use KitLoong\MigrationsGenerator\Tests\TestCase;

class ForeignKeyGeneratorTest extends TestCase
{
public function testGenerateDropWithNullName(): void
{
$setting = app(Setting::class);
$setting->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());
}
}
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 b9bc2bd

Please sign in to comment.