Skip to content

Commit 0aaea29

Browse files
committed
Support timezones
1 parent 20f537d commit 0aaea29

File tree

11 files changed

+131
-13
lines changed

11 files changed

+131
-13
lines changed

src/Blueprint.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ class Blueprint
1414

1515
public function parse($content)
1616
{
17-
$content = preg_replace_callback('/^(\s+)(id|timestamps|soft[dD]eletes)$/m', function ($matches) {
17+
$content = preg_replace_callback('/^(\s+)(id|timestamps(Tz)?|softDeletes(Tz)?)$/mi', function ($matches) {
1818
return $matches[1] . strtolower($matches[2]) . ': ' . $matches[2];
1919
}, $content);
2020

src/Generators/MigrationGenerator.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,8 +83,12 @@ protected function buildDefinition(Model $model)
8383
$definition .= ';' . PHP_EOL;
8484
}
8585

86+
if ($model->usesSoftDeletes()) {
87+
$definition .= self::INDENT . '$table->' . $model->softDeletesDataType() . '();' . PHP_EOL;
88+
}
89+
8690
if ($model->usesTimestamps()) {
87-
$definition .= self::INDENT . '$table->timestamps();' . PHP_EOL;
91+
$definition .= self::INDENT . '$table->' . $model->timestampsDataType() . '();' . PHP_EOL;
8892
}
8993

9094
return trim($definition);

src/Lexers/ModelLexer.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,11 +109,17 @@ private function buildModel(string $name, array $columns)
109109
}
110110

111111
unset($columns['timestamps']);
112+
} elseif (isset($columns['timestampstz'])) {
113+
$model->enableTimestamps(true);
114+
unset($columns['timestampstz']);
112115
}
113116

114117
if (isset($columns['softdeletes'])) {
115118
$model->enableSoftDeletes();
116119
unset($columns['softdeletes']);
120+
} elseif (isset($columns['softdeletestz'])) {
121+
$model->enableSoftDeletes(true);
122+
unset($columns['softdeletestz']);
117123
}
118124

119125
if (!isset($columns['id'])) {

src/Models/Model.php

Lines changed: 24 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
class Model
88
{
99
private $name;
10-
private $timestamps = true;
10+
private $timestamps = 'timestamps';
1111
private $softDeletes = false;
1212
private $columns = [];
1313

@@ -34,34 +34,49 @@ public function columns(): array
3434
return $this->columns;
3535
}
3636

37-
public function usesTimestamps()
37+
public function primaryKey()
38+
{
39+
return 'id';
40+
}
41+
42+
public function tableName()
43+
{
44+
return Str::snake(Str::pluralStudly($this->name));
45+
}
46+
47+
public function timestampsDataType(): string
3848
{
3949
return $this->timestamps;
4050
}
4151

52+
public function usesTimestamps(): bool
53+
{
54+
return $this->timestamps !== false;
55+
}
56+
4257
public function disableTimestamps()
4358
{
4459
$this->timestamps = false;
4560
}
4661

47-
public function primaryKey()
62+
public function enableTimestamps(bool $withTimezone = false)
4863
{
49-
64+
$this->timestamps = $withTimezone ? 'timestampsTz' : 'timestamps';
5065
}
5166

52-
public function tableName()
67+
public function softDeletesDataType(): string
5368
{
54-
return Str::snake(Str::pluralStudly($this->name));
69+
return $this->softDeletes;
5570
}
5671

5772
public function usesSoftDeletes(): bool
5873
{
59-
return $this->softDeletes;
74+
return $this->softDeletes !== false;
6075
}
6176

62-
public function enableSoftDeletes()
77+
public function enableSoftDeletes(bool $withTimezone = false)
6378
{
64-
$this->softDeletes = true;
79+
$this->softDeletes = $withTimezone ? 'softDeletesTz' : 'softDeletes';
6580
}
6681

6782
public function hasColumn(string $name)

tests/Feature/BlueprintTest.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@
88
use Symfony\Component\Yaml\Exception\ParseException;
99
use Tests\TestCase;
1010

11+
/**
12+
* @see Blueprint
13+
*/
1114
class BlueprintTest extends TestCase
1215
{
1316
/**
@@ -87,6 +90,23 @@ public function it_parses_shorthands()
8790
], $this->subject->parse($blueprint));
8891
}
8992

93+
/**
94+
* @test
95+
*/
96+
public function it_parses_shorthands_with_timezones()
97+
{
98+
$blueprint = $this->fixture('definitions/with-timezones.bp');
99+
100+
$this->assertEquals([
101+
'models' => [
102+
'Comment' => [
103+
'softdeletestz' => 'softDeletesTz',
104+
'timestampstz' => 'timestampstz',
105+
],
106+
],
107+
], $this->subject->parse($blueprint));
108+
}
109+
90110
/**
91111
* @test
92112
*/

tests/Feature/Generator/MigrationGeneratorTest.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@
77
use Carbon\Carbon;
88
use Tests\TestCase;
99

10+
/**
11+
* @see MigrationGenerator
12+
*/
1013
class MigrationGeneratorTest extends TestCase
1114
{
1215
private $blueprint;
@@ -66,13 +69,14 @@ public function output_writes_migration_for_model_tree($definition, $path, $migr
6669
$this->assertEquals(['created' => [$timestamp_path]], $this->subject->output($tree));
6770
}
6871

69-
7072
public function modelTreeDataProvider()
7173
{
7274
return [
7375
['definitions/readme-example.bp', 'database/migrations/timestamp_create_posts_table.php', 'migrations/readme-example.php'],
7476
['definitions/model-identities.bp', 'database/migrations/timestamp_create_relationships_table.php', 'migrations/identity-columns.php'],
7577
['definitions/model-modifiers.bp', 'database/migrations/timestamp_create_modifiers_table.php', 'migrations/modifiers.php'],
78+
['definitions/soft-deletes.bp', 'database/migrations/timestamp_create_comments_table.php', 'migrations/soft-deletes.php'],
79+
['definitions/with-timezones.bp', 'database/migrations/timestamp_create_comments_table.php', 'migrations/with-timezones.php'],
7680
];
7781
}
7882
}

tests/Feature/Generator/ModelGeneratorTest.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,7 @@ public function modelTreeDataProvider()
8383
return [
8484
['definitions/readme-example.bp', 'app/Post.php', 'models/readme-example.php'],
8585
['definitions/soft-deletes.bp', 'app/Comment.php', 'models/soft-deletes.php'],
86+
['definitions/with-timezones.bp', 'app/Comment.php', 'models/soft-deletes.php'],
8687
];
8788
}
8889
}

tests/fixtures/definitions/shorthands.bp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,4 @@ models:
22
Name:
33
softDeletes
44
id
5-
timestamps
5+
timestamps
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
models:
2+
Comment:
3+
softDeletesTz
4+
timestampstz
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<?php
2+
3+
use Illuminate\Database\Migrations\Migration;
4+
use Illuminate\Database\Schema\Blueprint;
5+
use Illuminate\Support\Facades\Schema;
6+
7+
class CreateCommentsTable extends Migration
8+
{
9+
/**
10+
* Run the migrations.
11+
*
12+
* @return void
13+
*/
14+
public function up()
15+
{
16+
Schema::create('comments', function (Blueprint $table) {
17+
$table->increments('id');
18+
$table->softDeletes();
19+
$table->timestamps();
20+
});
21+
}
22+
23+
/**
24+
* Reverse the migrations.
25+
*
26+
* @return void
27+
*/
28+
public function down()
29+
{
30+
Schema::dropIfExists('comments');
31+
}
32+
}

0 commit comments

Comments
 (0)