From a50d6a3e4b81b90e10895367c0c4d1c72b1be9fa Mon Sep 17 00:00:00 2001 From: Nathan E Date: Sun, 13 Sep 2020 19:15:51 -0400 Subject: [PATCH 1/6] chore: add draft file --- tests/fixtures/drafts/pascal-case-model-names.yaml | 10 ++++++++++ 1 file changed, 10 insertions(+) create mode 100644 tests/fixtures/drafts/pascal-case-model-names.yaml diff --git a/tests/fixtures/drafts/pascal-case-model-names.yaml b/tests/fixtures/drafts/pascal-case-model-names.yaml new file mode 100644 index 00000000..fb5480fc --- /dev/null +++ b/tests/fixtures/drafts/pascal-case-model-names.yaml @@ -0,0 +1,10 @@ +models: + Broker: + name: string + relationships: + belongsToMany: BrokerType + + BrokerType: + name: string + relationships: + belongsToMany: Broker From 2bf05c1b3bc66856dd649471b64201eee0e0aba5 Mon Sep 17 00:00:00 2001 From: Nathan E Date: Sun, 13 Sep 2020 19:16:14 -0400 Subject: [PATCH 2/6] chore: add fixtures --- ...al-case-model-names-broker-broker-type.php | 31 ++++++++++++++++++ .../pascal-case-model-names-broker-type.php | 32 +++++++++++++++++++ .../pascal-case-model-names-broker.php | 32 +++++++++++++++++++ 3 files changed, 95 insertions(+) create mode 100644 tests/fixtures/migrations/pascal-case-model-names-broker-broker-type.php create mode 100644 tests/fixtures/migrations/pascal-case-model-names-broker-type.php create mode 100644 tests/fixtures/migrations/pascal-case-model-names-broker.php diff --git a/tests/fixtures/migrations/pascal-case-model-names-broker-broker-type.php b/tests/fixtures/migrations/pascal-case-model-names-broker-broker-type.php new file mode 100644 index 00000000..36455a5b --- /dev/null +++ b/tests/fixtures/migrations/pascal-case-model-names-broker-broker-type.php @@ -0,0 +1,31 @@ +foreignId('broker_id'); + $table->foreignId('broker_type_id'); + }); + } + + /** + * Reverse the migrations. + * + * @return void + */ + public function down() + { + Schema::dropIfExists('broker_broker_type'); + } +} diff --git a/tests/fixtures/migrations/pascal-case-model-names-broker-type.php b/tests/fixtures/migrations/pascal-case-model-names-broker-type.php new file mode 100644 index 00000000..bf63857a --- /dev/null +++ b/tests/fixtures/migrations/pascal-case-model-names-broker-type.php @@ -0,0 +1,32 @@ +id(); + $table->string('name'); + $table->timestamps(); + }); + } + + /** + * Reverse the migrations. + * + * @return void + */ + public function down() + { + Schema::dropIfExists('broker_types'); + } +} diff --git a/tests/fixtures/migrations/pascal-case-model-names-broker.php b/tests/fixtures/migrations/pascal-case-model-names-broker.php new file mode 100644 index 00000000..654431fe --- /dev/null +++ b/tests/fixtures/migrations/pascal-case-model-names-broker.php @@ -0,0 +1,32 @@ +id(); + $table->string('name'); + $table->timestamps(); + }); + } + + /** + * Reverse the migrations. + * + * @return void + */ + public function down() + { + Schema::dropIfExists('brokers'); + } +} From a85aa000632ba6f082650e5634061099a16e3517 Mon Sep 17 00:00:00 2001 From: Nathan E Date: Sun, 13 Sep 2020 19:22:14 -0400 Subject: [PATCH 3/6] chore: update migration generator test --- .../Generators/MigrationGeneratorTest.php | 51 +++++++++++++++---- 1 file changed, 41 insertions(+), 10 deletions(-) diff --git a/tests/Feature/Generators/MigrationGeneratorTest.php b/tests/Feature/Generators/MigrationGeneratorTest.php index 12f25b8e..88cb68d8 100644 --- a/tests/Feature/Generators/MigrationGeneratorTest.php +++ b/tests/Feature/Generators/MigrationGeneratorTest.php @@ -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 @@ -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); @@ -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); @@ -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); @@ -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); @@ -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); @@ -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); @@ -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); @@ -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); From 58acd006b76913b1ebe8fd6a0cfb4b55e4aeda7a Mon Sep 17 00:00:00 2001 From: Nathan E Date: Sun, 13 Sep 2020 19:34:28 -0400 Subject: [PATCH 4/6] fix: "TwoWord" model column names --- src/Generators/MigrationGenerator.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Generators/MigrationGenerator.php b/src/Generators/MigrationGenerator.php index 36031cb0..4ccc9050 100644 --- a/src/Generators/MigrationGenerator.php +++ b/src/Generators/MigrationGenerator.php @@ -233,7 +233,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; From 2c371ce399443b695eab6b5b8c88357e5c6a9ac4 Mon Sep 17 00:00:00 2001 From: Nathan E Date: Sun, 13 Sep 2020 19:35:25 -0400 Subject: [PATCH 5/6] refactor: migration generator class - create migrations sequential (timestamp) - create `modelTables` before `pivotTables` --- src/Generators/MigrationGenerator.php | 53 ++++++++++++++++----------- 1 file changed, 32 insertions(+), 21 deletions(-) diff --git a/src/Generators/MigrationGenerator.php b/src/Generators/MigrationGenerator.php index 4ccc9050..cff25d52 100644 --- a/src/Generators/MigrationGenerator.php +++ b/src/Generators/MigrationGenerator.php @@ -46,39 +46,23 @@ public function __construct($files) public function output(Tree $tree, $overwrite = false): array { - $output = []; - - $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)); - - $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; - $output[$action][] = $path; - } - - return $output; + return $this->createMigrations($tables, $overwrite); } public function types(): array @@ -86,6 +70,33 @@ 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); From 8e543de3e37a027e95ceb05cd94e28e4e5d5d887 Mon Sep 17 00:00:00 2001 From: Nathan E Date: Sun, 13 Sep 2020 19:35:46 -0400 Subject: [PATCH 6/6] chore: remove dead code --- src/Generators/MigrationGenerator.php | 5 ----- 1 file changed, 5 deletions(-) diff --git a/src/Generators/MigrationGenerator.php b/src/Generators/MigrationGenerator.php index cff25d52..bcfb58d4 100644 --- a/src/Generators/MigrationGenerator.php +++ b/src/Generators/MigrationGenerator.php @@ -325,11 +325,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/';