Skip to content

Commit ee0c41a

Browse files
committed
Generate now() as useCurrent() #39
1 parent e13747f commit ee0c41a

File tree

2 files changed

+36
-2
lines changed

2 files changed

+36
-2
lines changed

src/KitLoong/MigrationsGenerator/Generators/DatetimeField.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,8 @@ public function makeDefault(Column $column): string
8888
{
8989
if (in_array($column->getDefault(), ['CURRENT_TIMESTAMP'], true)) {
9090
return ColumnModifier::USE_CURRENT;
91+
} elseif ($column->getDefault() === 'now()') { // For PgSQL
92+
return ColumnModifier::USE_CURRENT;
9193
} else {
9294
$default = $this->decorator->columnDefaultToString($column->getDefault());
9395
return $this->decorator->decorate(ColumnModifier::DEFAULT, [$default]);

tests/KitLoong/Feature/PgSQL/CommandTest.php

Lines changed: 34 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
namespace Tests\KitLoong\Feature\PgSQL;
99

10+
use Illuminate\Support\Facades\DB;
1011
use Illuminate\Support\Facades\File;
1112
use Illuminate\Support\Str;
1213

@@ -16,13 +17,28 @@ public function testRun()
1617
{
1718
$migrateTemplates = function () {
1819
$this->migrateGeneral('pgsql');
20+
21+
// Test timestamp default now()
22+
DB::statement("ALTER TABLE all_columns_pgsql ADD COLUMN timestamp_defaultnow timestamp(0) without time zone DEFAULT now() NOT NULL");
1923
};
2024

2125
$generateMigrations = function () {
2226
$this->generateMigrations();
2327
};
2428

25-
$this->verify($migrateTemplates, $generateMigrations);
29+
$beforeVerify = function () {
30+
$this->assertLineExistsThenReplace(
31+
$this->storageSql('actual.sql'),
32+
'timestamp_defaultnow timestamp(0) without time zone DEFAULT CURRENT_TIMESTAMP NOT NULL'
33+
);
34+
35+
$this->assertLineExistsThenReplace(
36+
$this->storageSql('expected.sql'),
37+
'timestamp_defaultnow timestamp(0) without time zone DEFAULT now() NOT NULL'
38+
);
39+
};
40+
41+
$this->verify($migrateTemplates, $generateMigrations, $beforeVerify);
2642
}
2743

2844
public function testCollation()
@@ -38,7 +54,7 @@ public function testCollation()
3854
$this->verify($migrateTemplates, $generateMigrations);
3955
}
4056

41-
public function verify(callable $migrateTemplates, callable $generateMigrations)
57+
public function verify(callable $migrateTemplates, callable $generateMigrations, callable $beforeVerify = null)
4258
{
4359
$migrateTemplates();
4460

@@ -64,9 +80,25 @@ public function verify(callable $migrateTemplates, callable $generateMigrations)
6480
$this->truncateMigration();
6581
$this->dumpSchemaAs($this->storageSql('actual.sql'));
6682

83+
$beforeVerify === null ?: $beforeVerify();
84+
6785
$this->assertFileEqualsIgnoringOrder(
6886
$this->storageSql('expected.sql'),
6987
$this->storageSql('actual.sql')
7088
);
7189
}
90+
91+
private function assertLineExistsThenReplace(string $file, string $line)
92+
{
93+
$this->assertTrue(str_contains(
94+
file_get_contents($file),
95+
$line
96+
));
97+
98+
File::put($file, str_replace(
99+
$line,
100+
'replaced',
101+
file_get_contents($file)
102+
));
103+
}
72104
}

0 commit comments

Comments
 (0)