Skip to content

Commit 0942543

Browse files
author
Nathan Esayeas
authored
Fix multi-word models + refactors (#376)
1 parent f98f7ec commit 0942543

File tree

6 files changed

+179
-35
lines changed

6 files changed

+179
-35
lines changed

src/Generators/MigrationGenerator.php

Lines changed: 33 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -49,44 +49,57 @@ public function __construct($files)
4949

5050
public function output(Tree $tree, $overwrite = false): array
5151
{
52-
$created_pivot_tables = [];
52+
$tables = ['tableNames' => [], 'pivotTableNames' => []];
5353

5454
$stub = $this->files->stub('migration.stub');
5555

56-
$sequential_timestamp = \Carbon\Carbon::now()->subSeconds(count($tree->models()));
57-
5856
/** @var \Blueprint\Models\Model $model */
5957
foreach ($tree->models() as $model) {
60-
$path = $this->getPath($model, $sequential_timestamp->addSecond(), $overwrite);
61-
$action = $this->files->exists($path) ? 'updated' : 'created';
62-
$this->files->put($path, $this->populateStub($stub, $model));
63-
64-
$this->output[$action][] = $path;
58+
$tables['tableNames'][$model->tableName()] = $this->populateStub($stub, $model);
6559

6660
if (! empty($model->pivotTables())) {
6761
foreach ($model->pivotTables() as $pivotSegments) {
68-
$pivotTable = $this->getPivotTableName($pivotSegments);
69-
$created_pivot_tables[$pivotTable] = $pivotSegments;
62+
$pivotTableName = $this->getPivotTableName($pivotSegments);
63+
$tables['pivotTableNames'][$pivotTableName] = $this->populatePivotStub($stub, $pivotSegments);
7064
}
7165
}
7266
}
7367

74-
foreach ($created_pivot_tables as $pivotTable => $pivotSegments) {
75-
$path = $this->getPivotTablePath($pivotTable, $sequential_timestamp, $overwrite);
76-
$action = $this->files->exists($path) ? 'updated' : 'created';
77-
$this->files->put($path, $this->populatePivotStub($stub, $pivotSegments));
78-
$created_pivot_tables[] = $pivotTable;
79-
$this->output[$action][] = $path;
80-
}
81-
82-
return $this->output;
68+
return $this->createMigrations($tables, $overwrite);
8369
}
8470

8571
public function types(): array
8672
{
8773
return ['migrations'];
8874
}
8975

76+
protected function createMigrations(array $tables, $overwrite = false): array
77+
{
78+
$output = [];
79+
80+
$sequential_timestamp = \Carbon\Carbon::now()->copy()->subSeconds(
81+
collect($tables['tableNames'])->merge($tables['pivotTableNames'])->count()
82+
);
83+
84+
foreach ($tables['tableNames'] as $tableName => $data) {
85+
$path = $this->getTablePath($tableName, $sequential_timestamp->addSecond(), $overwrite);
86+
$action = $this->files->exists($path) ? 'updated' : 'created';
87+
$this->files->put($path, $data);
88+
89+
$output[$action][] = $path;
90+
}
91+
92+
foreach ($tables['pivotTableNames'] as $tableName => $data) {
93+
$path = $this->getTablePath($tableName, $sequential_timestamp->addSecond(), $overwrite);
94+
$action = $this->files->exists($path) ? 'updated' : 'created';
95+
$this->files->put($path, $data);
96+
97+
$output[$action][] = $path;
98+
}
99+
100+
return $output;
101+
}
102+
90103
protected function populateStub(string $stub, Model $model)
91104
{
92105
$stub = str_replace('{{ class }}', $this->getClassName($model), $stub);
@@ -234,7 +247,7 @@ protected function buildPivotTableDefinition(array $segments)
234247
$definition = '';
235248

236249
foreach ($segments as $segment) {
237-
$column = Str::before(Str::lower($segment), ':');
250+
$column = Str::before(Str::snake($segment), ':');
238251
$references = 'id';
239252
$on = Str::plural($column);
240253
$foreign = Str::singular($column).'_'.$references;
@@ -315,11 +328,6 @@ protected function getPath(Model $model, Carbon $timestamp, $overwrite = false)
315328
return $this->getTablePath($model->tableName(), $timestamp, $overwrite);
316329
}
317330

318-
protected function getPivotTablePath($tableName, Carbon $timestamp, $overwrite = false)
319-
{
320-
return $this->getTablePath($tableName, $timestamp, $overwrite);
321-
}
322-
323331
protected function getTablePath($tableName, Carbon $timestamp, $overwrite = false)
324332
{
325333
$dir = 'database/migrations/';

tests/Feature/Generators/MigrationGeneratorTest.php

Lines changed: 41 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,37 @@ public function output_uses_past_timestamp_for_multiple_migrations()
162162
$this->assertEquals(['created' => [$post_path, $comment_path]], $this->subject->output($tree));
163163
}
164164

165+
/**
166+
* @test
167+
*/
168+
public function output_proper_pascal_case_model_names()
169+
{
170+
$this->files->expects('stub')
171+
->with('migration.stub')
172+
->andReturn($this->stub('migration.stub'));
173+
174+
$now = Carbon::now();
175+
Carbon::setTestNow($now);
176+
177+
$broker_path = str_replace('timestamp', $now->copy()->subSeconds(2)->format('Y_m_d_His'), 'database/migrations/timestamp_create_brokers_table.php');
178+
$broker_type_path = str_replace('timestamp', $now->copy()->subSecond()->format('Y_m_d_His'), 'database/migrations/timestamp_create_broker_types_table.php');
179+
$broker_broker_type_path = str_replace('timestamp', $now->format('Y_m_d_His'), 'database/migrations/timestamp_create_broker_broker_type_table.php');
180+
181+
$this->files->expects('exists')->times(3)->andReturn(false);
182+
183+
$this->files->expects('put')
184+
->with($broker_path, $this->fixture('migrations/pascal-case-model-names-broker.php'));
185+
$this->files->expects('put')
186+
->with($broker_type_path, $this->fixture('migrations/pascal-case-model-names-broker-type.php'));
187+
$this->files->expects('put')
188+
->with($broker_broker_type_path, $this->fixture('migrations/pascal-case-model-names-broker-broker-type.php'));
189+
190+
$tokens = $this->blueprint->parse($this->fixture('drafts/pascal-case-model-names.yaml'));
191+
$tree = $this->blueprint->analyze($tokens);
192+
193+
$this->assertEquals(['created' => [$broker_path, $broker_type_path, $broker_broker_type_path]], $this->subject->output($tree));
194+
}
195+
165196
/**
166197
* @test
167198
* @environment-setup useLaravel6
@@ -255,7 +286,7 @@ public function output_also_creates_pivot_table_migration()
255286
$now = Carbon::now();
256287
Carbon::setTestNow($now);
257288

258-
$model_migration = str_replace('timestamp', $now->format('Y_m_d_His'), 'database/migrations/timestamp_create_journeys_table.php');
289+
$model_migration = str_replace('timestamp', $now->copy()->subSecond()->format('Y_m_d_His'), 'database/migrations/timestamp_create_journeys_table.php');
259290
$pivot_migration = str_replace('timestamp', $now->format('Y_m_d_His'), 'database/migrations/timestamp_create_diary_journey_table.php');
260291

261292
$this->files->expects('exists')->twice()->andReturn(false);
@@ -320,7 +351,7 @@ public function output_also_creates_pivot_table_migration_laravel6()
320351
$now = Carbon::now();
321352
Carbon::setTestNow($now);
322353

323-
$model_migration = str_replace('timestamp', $now->format('Y_m_d_His'), 'database/migrations/timestamp_create_journeys_table.php');
354+
$model_migration = str_replace('timestamp', $now->copy()->subSecond()->format('Y_m_d_His'), 'database/migrations/timestamp_create_journeys_table.php');
324355
$pivot_migration = str_replace('timestamp', $now->format('Y_m_d_His'), 'database/migrations/timestamp_create_diary_journey_table.php');
325356

326357
$this->files->expects('exists')->twice()->andReturn(false);
@@ -351,7 +382,7 @@ public function output_also_creates_constraints_for_pivot_table_migration()
351382
$now = Carbon::now();
352383
Carbon::setTestNow($now);
353384

354-
$model_migration = str_replace('timestamp', $now->format('Y_m_d_His'), 'database/migrations/timestamp_create_journeys_table.php');
385+
$model_migration = str_replace('timestamp', $now->copy()->subSecond()->format('Y_m_d_His'), 'database/migrations/timestamp_create_journeys_table.php');
355386
$pivot_migration = str_replace('timestamp', $now->format('Y_m_d_His'), 'database/migrations/timestamp_create_diary_journey_table.php');
356387

357388
$this->files->expects('exists')->twice()->andReturn(false);
@@ -383,7 +414,7 @@ public function output_also_creates_constraints_for_pivot_table_migration_larave
383414
$now = Carbon::now();
384415
Carbon::setTestNow($now);
385416

386-
$model_migration = str_replace('timestamp', $now->format('Y_m_d_His'), 'database/migrations/timestamp_create_journeys_table.php');
417+
$model_migration = str_replace('timestamp', $now->copy()->subSecond()->format('Y_m_d_His'), 'database/migrations/timestamp_create_journeys_table.php');
387418
$pivot_migration = str_replace('timestamp', $now->format('Y_m_d_His'), 'database/migrations/timestamp_create_diary_journey_table.php');
388419

389420
$this->files->expects('exists')->twice()->andReturn(false);
@@ -411,8 +442,8 @@ public function output_does_not_duplicate_pivot_table_migration()
411442
$now = Carbon::now();
412443
Carbon::setTestNow($now);
413444

414-
$company_migration = str_replace('timestamp', $now->copy()->subSecond()->format('Y_m_d_His'), 'database/migrations/timestamp_create_companies_table.php');
415-
$people_migration = str_replace('timestamp', $now->format('Y_m_d_His'), 'database/migrations/timestamp_create_people_table.php');
445+
$company_migration = str_replace('timestamp', $now->copy()->subSeconds(2)->format('Y_m_d_His'), 'database/migrations/timestamp_create_companies_table.php');
446+
$people_migration = str_replace('timestamp', $now->copy()->subSecond()->format('Y_m_d_His'), 'database/migrations/timestamp_create_people_table.php');
416447
$pivot_migration = str_replace('timestamp', $now->format('Y_m_d_His'), 'database/migrations/timestamp_create_company_person_table.php');
417448

418449
$this->files->expects('exists')->times(3)->andReturn(false);
@@ -443,8 +474,8 @@ public function output_does_not_duplicate_pivot_table_migration_laravel6()
443474
$now = Carbon::now();
444475
Carbon::setTestNow($now);
445476

446-
$company_migration = str_replace('timestamp', $now->copy()->subSecond()->format('Y_m_d_His'), 'database/migrations/timestamp_create_companies_table.php');
447-
$people_migration = str_replace('timestamp', $now->format('Y_m_d_His'), 'database/migrations/timestamp_create_people_table.php');
477+
$company_migration = str_replace('timestamp', $now->copy()->subSeconds(2)->format('Y_m_d_His'), 'database/migrations/timestamp_create_companies_table.php');
478+
$people_migration = str_replace('timestamp', $now->copy()->subSecond()->format('Y_m_d_His'), 'database/migrations/timestamp_create_people_table.php');
448479
$pivot_migration = str_replace('timestamp', $now->format('Y_m_d_His'), 'database/migrations/timestamp_create_company_person_table.php');
449480

450481
$this->files->expects('exists')->times(3)->andReturn(false);
@@ -474,7 +505,7 @@ public function output_also_creates_pivot_table_migration_with_custom_name()
474505
$now = Carbon::now();
475506
Carbon::setTestNow($now);
476507

477-
$model_migration = str_replace('timestamp', $now->format('Y_m_d_His'), 'database/migrations/timestamp_create_users_table.php');
508+
$model_migration = str_replace('timestamp', $now->copy()->subSecond()->format('Y_m_d_His'), 'database/migrations/timestamp_create_users_table.php');
478509
$pivot_migration = str_replace('timestamp', $now->format('Y_m_d_His'), 'database/migrations/timestamp_create_test_table.php');
479510

480511
$this->files->expects('exists')->twice()->andReturn(false);
@@ -503,7 +534,7 @@ public function output_also_creates_pivot_table_migration_with_custom_name_larav
503534
$now = Carbon::now();
504535
Carbon::setTestNow($now);
505536

506-
$model_migration = str_replace('timestamp', $now->format('Y_m_d_His'), 'database/migrations/timestamp_create_users_table.php');
537+
$model_migration = str_replace('timestamp', $now->copy()->subSecond()->format('Y_m_d_His'), 'database/migrations/timestamp_create_users_table.php');
507538
$pivot_migration = str_replace('timestamp', $now->format('Y_m_d_His'), 'database/migrations/timestamp_create_test_table.php');
508539

509540
$this->files->expects('exists')->twice()->andReturn(false);
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
models:
2+
Broker:
3+
name: string
4+
relationships:
5+
belongsToMany: BrokerType
6+
7+
BrokerType:
8+
name: string
9+
relationships:
10+
belongsToMany: Broker
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<?php
2+
3+
use Illuminate\Database\Migrations\Migration;
4+
use Illuminate\Database\Schema\Blueprint;
5+
use Illuminate\Support\Facades\Schema;
6+
7+
class CreateBrokerBrokerTypeTable extends Migration
8+
{
9+
/**
10+
* Run the migrations.
11+
*
12+
* @return void
13+
*/
14+
public function up()
15+
{
16+
Schema::create('broker_broker_type', function (Blueprint $table) {
17+
$table->foreignId('broker_id');
18+
$table->foreignId('broker_type_id');
19+
});
20+
}
21+
22+
/**
23+
* Reverse the migrations.
24+
*
25+
* @return void
26+
*/
27+
public function down()
28+
{
29+
Schema::dropIfExists('broker_broker_type');
30+
}
31+
}
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 CreateBrokerTypesTable extends Migration
8+
{
9+
/**
10+
* Run the migrations.
11+
*
12+
* @return void
13+
*/
14+
public function up()
15+
{
16+
Schema::create('broker_types', function (Blueprint $table) {
17+
$table->id();
18+
$table->string('name');
19+
$table->timestamps();
20+
});
21+
}
22+
23+
/**
24+
* Reverse the migrations.
25+
*
26+
* @return void
27+
*/
28+
public function down()
29+
{
30+
Schema::dropIfExists('broker_types');
31+
}
32+
}
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 CreateBrokersTable extends Migration
8+
{
9+
/**
10+
* Run the migrations.
11+
*
12+
* @return void
13+
*/
14+
public function up()
15+
{
16+
Schema::create('brokers', function (Blueprint $table) {
17+
$table->id();
18+
$table->string('name');
19+
$table->timestamps();
20+
});
21+
}
22+
23+
/**
24+
* Reverse the migrations.
25+
*
26+
* @return void
27+
*/
28+
public function down()
29+
{
30+
Schema::dropIfExists('brokers');
31+
}
32+
}

0 commit comments

Comments
 (0)