Skip to content

Commit 6d4014b

Browse files
authored
Respect order for soft deletes (#534)
1 parent d3d33a0 commit 6d4014b

File tree

9 files changed

+84
-18
lines changed

9 files changed

+84
-18
lines changed

src/Generators/MigrationGenerator.php

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,10 @@ protected function buildDefinition(Model $model)
181181
$column_definition .= '$table->id(';
182182
} elseif ($dataType === 'rememberToken') {
183183
$column_definition .= '$table->rememberToken(';
184+
} elseif ($dataType === 'softDeletes') {
185+
$column_definition .= '$table->softDeletes(';
186+
} elseif ($dataType === 'softDeletesTz') {
187+
$column_definition .= '$table->softDeletesTz(';
184188
} else {
185189
$column_definition .= '$table->' . $dataType . "('{$column->name()}'";
186190
}
@@ -193,6 +197,7 @@ protected function buildDefinition(Model $model)
193197
$column_definition .= implode(', ', $column->attributes());
194198
}
195199
}
200+
196201
$column_definition .= ')';
197202

198203
$modifiers = $column->modifiers();
@@ -252,10 +257,6 @@ function ($modifier) use ($column) {
252257
$definition .= $column_definition;
253258
}
254259

255-
if ($model->usesSoftDeletes()) {
256-
$definition .= self::INDENT . '$table->' . $model->softDeletesDataType() . '();' . PHP_EOL;
257-
}
258-
259260
if ($model->morphTo()) {
260261
$definition .= self::INDENT . sprintf('$table->unsignedBigInteger(\'%s\');', Str::lower($model->morphTo() . '_id')) . PHP_EOL;
261262
$definition .= self::INDENT . sprintf('$table->string(\'%s\');', Str::lower($model->morphTo() . '_type')) . PHP_EOL;

src/Generators/ModelGenerator.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -80,17 +80,15 @@ protected function buildClassPhpDoc(Model $model)
8080
$phpDoc .= PHP_EOL;
8181
$phpDoc .= ' * @property string|null $' . $column->name() . '_type';
8282
$phpDoc .= PHP_EOL;
83+
} elseif (in_array($column->dataType(), ['softDeletesTz', 'softDeletes'])) {
84+
$phpDoc .= ' * @property \Carbon\Carbon $deleted_at';
85+
$phpDoc .= PHP_EOL;
8386
} else {
8487
$phpDoc .= sprintf(' * @property %s $%s', $this->phpDataType($column->dataType()), $column->name());
8588
$phpDoc .= PHP_EOL;
8689
}
8790
}
8891

89-
if ($model->usesSoftDeletes()) {
90-
$phpDoc .= ' * @property \Carbon\Carbon $deleted_at';
91-
$phpDoc .= PHP_EOL;
92-
}
93-
9492
if ($model->usesTimestamps()) {
9593
$phpDoc .= ' * @property \Carbon\Carbon $created_at';
9694
$phpDoc .= PHP_EOL;
@@ -247,6 +245,8 @@ private function fillableColumns(array $columns)
247245
'created_at',
248246
'updated_at',
249247
'remember_token',
248+
'softdeletes',
249+
'softdeletestz',
250250
]
251251
);
252252
}

src/Generators/TestGenerator.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -226,7 +226,7 @@ protected function buildTestCases(Controller $controller)
226226
$request_data[$data] = '$' . $variable_name;
227227
} elseif (!is_null($local_model)) {
228228
foreach ($local_model->columns() as $local_column) {
229-
if ($local_column->name() === 'id') {
229+
if (in_array($local_column->name(), ['id', 'softdeletes', 'softdeletestz'])) {
230230
continue;
231231
}
232232

src/Lexers/ModelLexer.php

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -149,10 +149,8 @@ private function buildModel(string $name, array $columns)
149149

150150
if (isset($columns['softdeletes'])) {
151151
$model->enableSoftDeletes();
152-
unset($columns['softdeletes']);
153152
} elseif (isset($columns['softdeletestz'])) {
154153
$model->enableSoftDeletes(true);
155-
unset($columns['softdeletestz']);
156154
}
157155

158156
if (isset($columns['relationships'])) {

tests/Feature/Generators/MigrationGeneratorTest.php

Lines changed: 30 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
use Blueprint\Generators\MigrationGenerator;
77
use Blueprint\Tree;
88
use Carbon\Carbon;
9-
use Illuminate\Support\Facades\App;
109
use Symfony\Component\Finder\SplFileInfo;
1110
use Tests\TestCase;
1211

@@ -53,7 +52,7 @@ public function output_writes_nothing_for_empty_tree()
5352
*/
5453
public function output_writes_migration_for_model_tree($definition, $path, $migration)
5554
{
56-
if ($migration === 'migrations/return-type-declarations.php') {
55+
if ($migration === 'migrations/return-type-declarations.php') {
5756
$this->app['config']->set('blueprint.use_return_types', true);
5857
}
5958

@@ -85,7 +84,7 @@ public function output_writes_migration_for_model_tree($definition, $path, $migr
8584
*/
8685
public function output_updates_migration_for_model_tree($definition, $path, $migration)
8786
{
88-
if ($migration === 'migrations/return-type-declarations.php') {
87+
if ($migration === 'migrations/return-type-declarations.php') {
8988
$this->app['config']->set('blueprint.use_return_types', true);
9089
}
9190

@@ -608,6 +607,34 @@ public function output_generates_constraint_for_uuid()
608607
$this->assertEquals(['created' => [$timestamp_path]], $this->subject->output($tree));
609608
}
610609

610+
/**
611+
* @test
612+
*/
613+
public function output_respects_softdelete_order()
614+
{
615+
$this->app->config->set('blueprint.use_constraints', true);
616+
617+
$this->filesystem->expects('stub')
618+
->with('migration.stub')
619+
->andReturn($this->stub('migration.stub'));
620+
621+
$now = Carbon::now();
622+
Carbon::setTestNow($now);
623+
624+
$timestamp_path = 'database/migrations/' . $now->format('Y_m_d_His') . '_create_comments_table.php';
625+
626+
$this->filesystem->expects('exists')
627+
->with($timestamp_path)
628+
->andReturn(false);
629+
630+
$this->filesystem->expects('put')
631+
->with($timestamp_path, $this->fixture('migrations/soft-deletes-respect-order.php'));
632+
633+
$tokens = $this->blueprint->parse($this->fixture('drafts/soft-deletes-respect-order.yaml'));
634+
$tree = $this->blueprint->analyze($tokens);
635+
$this->assertEquals(['created' => [$timestamp_path]], $this->subject->output($tree));
636+
}
637+
611638
public function modelTreeDataProvider()
612639
{
613640
return [

tests/Feature/Generators/TestGeneratorTest.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -172,7 +172,6 @@ public function output_generates_tests_with_models_with_custom_namespace_correct
172172

173173
$tokens = $this->blueprint->parse($this->fixture($definition));
174174
$tree = $this->blueprint->analyze($tokens);
175-
176175
$this->assertEquals(['created' => [$path]], $this->subject->output($tree));
177176
}
178177

@@ -226,7 +225,7 @@ public function controllerTreeDataProvider()
226225
'tests/Feature/Http/Controllers/TelegramControllerTest.php',
227226
'tests/Feature/Http/Controllers/PaymentControllerTest.php',
228227
'tests/Feature/Http/Controllers/Api/PaymentControllerTest.php'
229-
],[
228+
], [
230229
'tests/call-to-a-member-function-columns-on-null-SubscriptionControllerTest.php',
231230
'tests/call-to-a-member-function-columns-on-null-TelegramControllerTest.php',
232231
'tests/call-to-a-member-function-columns-on-null-PaymentControllerTest.php',

tests/Feature/Lexers/ModelLexerTest.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -377,9 +377,11 @@ public function it_enables_soft_deletes()
377377
$this->assertTrue($model->usesSoftDeletes());
378378

379379
$columns = $model->columns();
380-
$this->assertCount(1, $columns);
380+
$this->assertCount(2, $columns);
381381
$this->assertEquals('id', $columns['id']->name());
382382
$this->assertEquals('id', $columns['id']->dataType());
383+
$this->assertEquals('softdeletes', $columns['softdeletes']->name());
384+
$this->assertEquals('softDeletes', $columns['softdeletes']->dataType());
383385
$this->assertEquals([], $columns['id']->modifiers());
384386
}
385387

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
models:
2+
Comment:
3+
title: string
4+
softdeletes
5+
name: string
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
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->id();
18+
$table->string('title');
19+
$table->softDeletes();
20+
$table->string('name');
21+
$table->timestamps();
22+
});
23+
}
24+
25+
/**
26+
* Reverse the migrations.
27+
*
28+
* @return void
29+
*/
30+
public function down()
31+
{
32+
Schema::dropIfExists('comments');
33+
}
34+
}

0 commit comments

Comments
 (0)