Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 33 additions & 25 deletions src/Generators/MigrationGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,44 +49,57 @@ public function __construct($files)

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

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

$sequential_timestamp = \Carbon\Carbon::now()->subSeconds(count($tree->models()));

/** @var \Blueprint\Models\Model $model */
foreach ($tree->models() as $model) {
$path = $this->getPath($model, $sequential_timestamp->addSecond(), $overwrite);
$action = $this->files->exists($path) ? 'updated' : 'created';
$this->files->put($path, $this->populateStub($stub, $model));

$this->output[$action][] = $path;
$tables['tableNames'][$model->tableName()] = $this->populateStub($stub, $model);

if (! empty($model->pivotTables())) {
foreach ($model->pivotTables() as $pivotSegments) {
$pivotTable = $this->getPivotTableName($pivotSegments);
$created_pivot_tables[$pivotTable] = $pivotSegments;
$pivotTableName = $this->getPivotTableName($pivotSegments);
$tables['pivotTableNames'][$pivotTableName] = $this->populatePivotStub($stub, $pivotSegments);
}
}
}

foreach ($created_pivot_tables as $pivotTable => $pivotSegments) {
$path = $this->getPivotTablePath($pivotTable, $sequential_timestamp, $overwrite);
$action = $this->files->exists($path) ? 'updated' : 'created';
$this->files->put($path, $this->populatePivotStub($stub, $pivotSegments));
$created_pivot_tables[] = $pivotTable;
$this->output[$action][] = $path;
}

return $this->output;
return $this->createMigrations($tables, $overwrite);
}

public function types(): array
{
return ['migrations'];
}

protected function createMigrations(array $tables, $overwrite = false): array
{
$output = [];

$sequential_timestamp = \Carbon\Carbon::now()->copy()->subSeconds(
collect($tables['tableNames'])->merge($tables['pivotTableNames'])->count()
);

foreach ($tables['tableNames'] as $tableName => $data) {
$path = $this->getTablePath($tableName, $sequential_timestamp->addSecond(), $overwrite);
$action = $this->files->exists($path) ? 'updated' : 'created';
$this->files->put($path, $data);

$output[$action][] = $path;
}

foreach ($tables['pivotTableNames'] as $tableName => $data) {
$path = $this->getTablePath($tableName, $sequential_timestamp->addSecond(), $overwrite);
$action = $this->files->exists($path) ? 'updated' : 'created';
$this->files->put($path, $data);

$output[$action][] = $path;
}

return $output;
}

protected function populateStub(string $stub, Model $model)
{
$stub = str_replace('{{ class }}', $this->getClassName($model), $stub);
Expand Down Expand Up @@ -234,7 +247,7 @@ protected function buildPivotTableDefinition(array $segments)
$definition = '';

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

protected function getPivotTablePath($tableName, Carbon $timestamp, $overwrite = false)
{
return $this->getTablePath($tableName, $timestamp, $overwrite);
}

protected function getTablePath($tableName, Carbon $timestamp, $overwrite = false)
{
$dir = 'database/migrations/';
Expand Down
51 changes: 41 additions & 10 deletions tests/Feature/Generators/MigrationGeneratorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,37 @@ public function output_uses_past_timestamp_for_multiple_migrations()
$this->assertEquals(['created' => [$post_path, $comment_path]], $this->subject->output($tree));
}

/**
* @test
*/
public function output_proper_pascal_case_model_names()
{
$this->files->expects('stub')
->with('migration.stub')
->andReturn($this->stub('migration.stub'));

$now = Carbon::now();
Carbon::setTestNow($now);

$broker_path = str_replace('timestamp', $now->copy()->subSeconds(2)->format('Y_m_d_His'), 'database/migrations/timestamp_create_brokers_table.php');
$broker_type_path = str_replace('timestamp', $now->copy()->subSecond()->format('Y_m_d_His'), 'database/migrations/timestamp_create_broker_types_table.php');
$broker_broker_type_path = str_replace('timestamp', $now->format('Y_m_d_His'), 'database/migrations/timestamp_create_broker_broker_type_table.php');

$this->files->expects('exists')->times(3)->andReturn(false);

$this->files->expects('put')
->with($broker_path, $this->fixture('migrations/pascal-case-model-names-broker.php'));
$this->files->expects('put')
->with($broker_type_path, $this->fixture('migrations/pascal-case-model-names-broker-type.php'));
$this->files->expects('put')
->with($broker_broker_type_path, $this->fixture('migrations/pascal-case-model-names-broker-broker-type.php'));

$tokens = $this->blueprint->parse($this->fixture('drafts/pascal-case-model-names.yaml'));
$tree = $this->blueprint->analyze($tokens);

$this->assertEquals(['created' => [$broker_path, $broker_type_path, $broker_broker_type_path]], $this->subject->output($tree));
}

/**
* @test
* @environment-setup useLaravel6
Expand Down Expand Up @@ -255,7 +286,7 @@ public function output_also_creates_pivot_table_migration()
$now = Carbon::now();
Carbon::setTestNow($now);

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

$this->files->expects('exists')->twice()->andReturn(false);
Expand Down
10 changes: 10 additions & 0 deletions tests/fixtures/drafts/pascal-case-model-names.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
models:
Broker:
name: string
relationships:
belongsToMany: BrokerType

BrokerType:
name: string
relationships:
belongsToMany: Broker
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

class CreateBrokerBrokerTypeTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('broker_broker_type', function (Blueprint $table) {
$table->foreignId('broker_id');
$table->foreignId('broker_type_id');
});
}

/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('broker_broker_type');
}
}
32 changes: 32 additions & 0 deletions tests/fixtures/migrations/pascal-case-model-names-broker-type.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

class CreateBrokerTypesTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('broker_types', function (Blueprint $table) {
$table->id();
$table->string('name');
$table->timestamps();
});
}

/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('broker_types');
}
}
32 changes: 32 additions & 0 deletions tests/fixtures/migrations/pascal-case-model-names-broker.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

class CreateBrokersTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('brokers', function (Blueprint $table) {
$table->id();
$table->string('name');
$table->timestamps();
});
}

/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('brokers');
}
}