From d896b03875b9d076dbcbea1544c85ebc3a4cc7a4 Mon Sep 17 00:00:00 2001 From: rcrosbourne Date: Tue, 5 Dec 2023 07:56:29 -0500 Subject: [PATCH 01/30] feat: add hasuuids trait to model --- .gitignore | 1 + src/Generators/ModelGenerator.php | 16 +++++++++++----- src/Models/Model.php | 8 ++++++++ 3 files changed, 20 insertions(+), 5 deletions(-) diff --git a/.gitignore b/.gitignore index ef9390cc..e724bc41 100644 --- a/.gitignore +++ b/.gitignore @@ -3,3 +3,4 @@ .env /.phpunit.cache composer.lock +.idea diff --git a/src/Generators/ModelGenerator.php b/src/Generators/ModelGenerator.php index 26ae818f..871f6e28 100644 --- a/src/Generators/ModelGenerator.php +++ b/src/Generators/ModelGenerator.php @@ -271,14 +271,20 @@ protected function buildRelationships(Model $model): string protected function addTraits(Model $model, $stub): string { - if (!$model->usesSoftDeletes()) { + if (!$model->usesSoftDeletes() && !$model->usesUuids()) { return $stub; } + $traits = ['HasFactory']; + if($model->usesSoftDeletes()) { + $this->addImport($model, 'Illuminate\\Database\\Eloquent\\SoftDeletes'); + $traits[] = 'SoftDeletes'; + } + if($model->usesUuids()) { + $this->addImport($model, 'Illuminate\\Database\\Eloquent\\Concerns\\HasUuids'); + $traits[] = 'HasUuids'; + } - $this->addImport($model, 'Illuminate\\Database\\Eloquent\\SoftDeletes'); - $stub = Str::replaceFirst('use HasFactory', 'use HasFactory, SoftDeletes', $stub); - - return $stub; + return Str::replaceFirst('use HasFactory', 'use '. implode(", ", $traits), $stub); } private function fillableColumns(array $columns): array diff --git a/src/Models/Model.php b/src/Models/Model.php index 0ec12e81..2ce4a802 100644 --- a/src/Models/Model.php +++ b/src/Models/Model.php @@ -96,6 +96,14 @@ public function usesPrimaryKey(): bool return $this->primaryKey !== false; } + public function usesUuids(): bool + { + if($this->usesPrimaryKey()) { + return $this->columns[$this->primaryKey]->dataType() === 'uuid'; + } + return false; + } + public function disablePrimaryKey(): void { $this->primaryKey = false; From 9ad1d23bd330643b0a2e776ecf11b489e080edf5 Mon Sep 17 00:00:00 2001 From: rcrosbourne Date: Tue, 5 Dec 2023 20:07:02 -0500 Subject: [PATCH 02/30] test: add tests for hasuuids trait --- .../Feature/Generators/ModelGeneratorTest.php | 33 +++++++++++++++++++ tests/fixtures/drafts/model-with-uuid-id.yaml | 5 +++ .../fixtures/models/model-with-uuid-trait.php | 31 +++++++++++++++++ 3 files changed, 69 insertions(+) create mode 100644 tests/fixtures/drafts/model-with-uuid-id.yaml create mode 100644 tests/fixtures/models/model-with-uuid-trait.php diff --git a/tests/Feature/Generators/ModelGeneratorTest.php b/tests/Feature/Generators/ModelGeneratorTest.php index fb6cfa1f..2f490719 100644 --- a/tests/Feature/Generators/ModelGeneratorTest.php +++ b/tests/Feature/Generators/ModelGeneratorTest.php @@ -595,6 +595,38 @@ public function output_generates_models_with_custom_pivot(): void $this->assertEquals(['created' => ['app/Models/User.php', 'app/Models/Team.php', 'app/Models/Membership.php']], $this->subject->output($tree)); } + #[Test] + public function output_generates_models_with_hasuuids_trait_if_uuid_id_is_type_uuid(): void + { + $this->filesystem->expects('stub') + ->with('model.class.stub') + ->andReturn($this->stub('model.class.stub')); + $this->filesystem->expects('stub') + ->times(1) + ->with('model.casts.stub') + ->andReturn($this->stub('model.casts.stub')); + $this->filesystem->expects('stub') + ->times(1) + ->with('model.fillable.stub') + ->andReturn($this->stub('model.fillable.stub')); + $this->filesystem->expects('stub') + ->times(1) + ->with('model.method.stub') + ->andReturn($this->stub('model.method.stub')); + + $this->filesystem->expects('exists') + ->times(1) + ->with('app/Models') + ->andReturnTrue(); + + $this->filesystem->expects('put') + ->with('app/Models/User.php', $this->fixture('models/model-with-uuid-trait.php')); + $tokens = $this->blueprint->parse($this->fixture('drafts/model-with-uuid-id.yaml')); + $tree = $this->blueprint->analyze($tokens); + + $this->assertEquals(['created' => ['app/Models/User.php']], $this->subject->output($tree)); + } + public static function modelTreeDataProvider(): array { return [ @@ -610,6 +642,7 @@ public static function modelTreeDataProvider(): array ['drafts/uuid-shorthand-invalid-relationship.yaml', 'app/Models/AgeCohort.php', 'models/uuid-shorthand-invalid-relationship.php'], ['drafts/model-with-meta.yaml', 'app/Models/Post.php', 'models/model-with-meta.php'], ['drafts/infer-belongsto.yaml', 'app/Models/Conference.php', 'models/infer-belongsto.php'], + ['drafts/model-with-uuid-id.yaml', 'app/Models/User.php', 'models/model-with-uuid-trait.php'], ]; } diff --git a/tests/fixtures/drafts/model-with-uuid-id.yaml b/tests/fixtures/drafts/model-with-uuid-id.yaml new file mode 100644 index 00000000..0f4e491d --- /dev/null +++ b/tests/fixtures/drafts/model-with-uuid-id.yaml @@ -0,0 +1,5 @@ +models: + User: + id: uuid primary + name: string + base_pay: decimal:10,2 diff --git a/tests/fixtures/models/model-with-uuid-trait.php b/tests/fixtures/models/model-with-uuid-trait.php new file mode 100644 index 00000000..c6ba2147 --- /dev/null +++ b/tests/fixtures/models/model-with-uuid-trait.php @@ -0,0 +1,31 @@ + 'decimal:2', + ]; +} From 8d307d86e9a8231e8bccfe53d0eab12daa60cb65 Mon Sep 17 00:00:00 2001 From: rcrosbourne Date: Wed, 6 Dec 2023 12:11:55 -0500 Subject: [PATCH 03/30] refactor(lint): fix code formatting --- src/Generators/ModelGenerator.php | 6 +++--- src/Models/Model.php | 3 ++- 2 files changed, 5 insertions(+), 4 deletions(-) diff --git a/src/Generators/ModelGenerator.php b/src/Generators/ModelGenerator.php index 871f6e28..ca9dc1e5 100644 --- a/src/Generators/ModelGenerator.php +++ b/src/Generators/ModelGenerator.php @@ -275,16 +275,16 @@ protected function addTraits(Model $model, $stub): string return $stub; } $traits = ['HasFactory']; - if($model->usesSoftDeletes()) { + if ($model->usesSoftDeletes()) { $this->addImport($model, 'Illuminate\\Database\\Eloquent\\SoftDeletes'); $traits[] = 'SoftDeletes'; } - if($model->usesUuids()) { + if ($model->usesUuids()) { $this->addImport($model, 'Illuminate\\Database\\Eloquent\\Concerns\\HasUuids'); $traits[] = 'HasUuids'; } - return Str::replaceFirst('use HasFactory', 'use '. implode(", ", $traits), $stub); + return Str::replaceFirst('use HasFactory', 'use ' . implode(', ', $traits), $stub); } private function fillableColumns(array $columns): array diff --git a/src/Models/Model.php b/src/Models/Model.php index 2ce4a802..649f027b 100644 --- a/src/Models/Model.php +++ b/src/Models/Model.php @@ -98,9 +98,10 @@ public function usesPrimaryKey(): bool public function usesUuids(): bool { - if($this->usesPrimaryKey()) { + if ($this->usesPrimaryKey()) { return $this->columns[$this->primaryKey]->dataType() === 'uuid'; } + return false; } From 2c2e4f5cbed116474b372a1329b7188c8995cb89 Mon Sep 17 00:00:00 2001 From: rcrosbourne Date: Wed, 6 Dec 2023 12:15:43 -0500 Subject: [PATCH 04/30] refactor(lint): fix lint issues in ModelGeneratorTest.php --- tests/Feature/Generators/ModelGeneratorTest.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/Feature/Generators/ModelGeneratorTest.php b/tests/Feature/Generators/ModelGeneratorTest.php index 2f490719..eb8b2b6f 100644 --- a/tests/Feature/Generators/ModelGeneratorTest.php +++ b/tests/Feature/Generators/ModelGeneratorTest.php @@ -595,7 +595,7 @@ public function output_generates_models_with_custom_pivot(): void $this->assertEquals(['created' => ['app/Models/User.php', 'app/Models/Team.php', 'app/Models/Membership.php']], $this->subject->output($tree)); } - #[Test] + #[Test] public function output_generates_models_with_hasuuids_trait_if_uuid_id_is_type_uuid(): void { $this->filesystem->expects('stub') From cae05674bac8cec749c96fa292ed6da47dfbb6cd Mon Sep 17 00:00:00 2001 From: Jason McCreary Date: Wed, 6 Dec 2023 13:08:43 -0500 Subject: [PATCH 05/30] Revert --- .gitignore | 1 - 1 file changed, 1 deletion(-) diff --git a/.gitignore b/.gitignore index e724bc41..ef9390cc 100644 --- a/.gitignore +++ b/.gitignore @@ -3,4 +3,3 @@ .env /.phpunit.cache composer.lock -.idea From 1e136c4d829c942aba9ae98939a2c5124e2170d0 Mon Sep 17 00:00:00 2001 From: Jason McCreary Date: Wed, 6 Dec 2023 13:11:53 -0500 Subject: [PATCH 06/30] Refactor to alphabetical order --- src/Generators/ModelGenerator.php | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/src/Generators/ModelGenerator.php b/src/Generators/ModelGenerator.php index ca9dc1e5..e0ac31dd 100644 --- a/src/Generators/ModelGenerator.php +++ b/src/Generators/ModelGenerator.php @@ -271,19 +271,20 @@ protected function buildRelationships(Model $model): string protected function addTraits(Model $model, $stub): string { - if (!$model->usesSoftDeletes() && !$model->usesUuids()) { - return $stub; - } - $traits = ['HasFactory']; + $traits['HasFactory']; + if ($model->usesSoftDeletes()) { $this->addImport($model, 'Illuminate\\Database\\Eloquent\\SoftDeletes'); $traits[] = 'SoftDeletes'; } + if ($model->usesUuids()) { $this->addImport($model, 'Illuminate\\Database\\Eloquent\\Concerns\\HasUuids'); $traits[] = 'HasUuids'; } + sort($traits); + return Str::replaceFirst('use HasFactory', 'use ' . implode(', ', $traits), $stub); } From 3eeee207b7c516796a45ff598d6e82d23ebd497a Mon Sep 17 00:00:00 2001 From: Jason McCreary Date: Wed, 6 Dec 2023 13:13:21 -0500 Subject: [PATCH 07/30] Streamline boolean logic --- src/Models/Model.php | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/src/Models/Model.php b/src/Models/Model.php index 649f027b..cee66fd7 100644 --- a/src/Models/Model.php +++ b/src/Models/Model.php @@ -98,11 +98,7 @@ public function usesPrimaryKey(): bool public function usesUuids(): bool { - if ($this->usesPrimaryKey()) { - return $this->columns[$this->primaryKey]->dataType() === 'uuid'; - } - - return false; + return $this->usesPrimaryKey() && $this->columns[$this->primaryKey]->dataType() === 'uuid'; } public function disablePrimaryKey(): void From c71357c2a4039a0974924946136f72451ee223bb Mon Sep 17 00:00:00 2001 From: Jason McCreary Date: Wed, 6 Dec 2023 13:17:40 -0500 Subject: [PATCH 08/30] Fix typo --- src/Generators/ModelGenerator.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Generators/ModelGenerator.php b/src/Generators/ModelGenerator.php index e0ac31dd..5d75ecc8 100644 --- a/src/Generators/ModelGenerator.php +++ b/src/Generators/ModelGenerator.php @@ -271,7 +271,7 @@ protected function buildRelationships(Model $model): string protected function addTraits(Model $model, $stub): string { - $traits['HasFactory']; + $traits = ['HasFactory']; if ($model->usesSoftDeletes()) { $this->addImport($model, 'Illuminate\\Database\\Eloquent\\SoftDeletes'); From ba8aef3a32b52756e6b83068f10a11019d130300 Mon Sep 17 00:00:00 2001 From: rcrosbourne Date: Wed, 6 Dec 2023 23:01:31 -0500 Subject: [PATCH 09/30] Update regular expression in Blueprint.php The change updates the regular expression in Blueprint.php to match 'ulid' and 'uuid' both. The update will reflect in the functionality where these matches are used for id creation. --- src/Blueprint.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Blueprint.php b/src/Blueprint.php index d14f245c..3d2f916a 100644 --- a/src/Blueprint.php +++ b/src/Blueprint.php @@ -65,8 +65,8 @@ public function parse($content, $strip_dashes = true) ); $content = preg_replace_callback( - '/^(\s+)uuid(: true)?$/mi', - fn ($matches) => $matches[1] . 'id: uuid primary', + '/^(\s+)(ulid|uuid)(: true)?$/mi', + fn ($matches) => $matches[1] . 'id: ' . $matches[2] . ' primary', $content ); From 5ac1b7e174ff63b6e3ac484b1a8fb0f7127feb28 Mon Sep 17 00:00:00 2001 From: rcrosbourne Date: Wed, 6 Dec 2023 23:04:00 -0500 Subject: [PATCH 10/30] Add 'ulid' type to ModelLexer.php. --- src/Generators/ModelGenerator.php | 4 ++-- src/Lexers/ModelLexer.php | 1 + 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/src/Generators/ModelGenerator.php b/src/Generators/ModelGenerator.php index 5d75ecc8..1f7cabef 100644 --- a/src/Generators/ModelGenerator.php +++ b/src/Generators/ModelGenerator.php @@ -272,12 +272,12 @@ protected function buildRelationships(Model $model): string protected function addTraits(Model $model, $stub): string { $traits = ['HasFactory']; - + if ($model->usesSoftDeletes()) { $this->addImport($model, 'Illuminate\\Database\\Eloquent\\SoftDeletes'); $traits[] = 'SoftDeletes'; } - + if ($model->usesUuids()) { $this->addImport($model, 'Illuminate\\Database\\Eloquent\\Concerns\\HasUuids'); $traits[] = 'HasUuids'; diff --git a/src/Lexers/ModelLexer.php b/src/Lexers/ModelLexer.php index dcf195fb..f4d2f268 100644 --- a/src/Lexers/ModelLexer.php +++ b/src/Lexers/ModelLexer.php @@ -81,6 +81,7 @@ class ModelLexer implements Lexer 'unsignedmediuminteger' => 'unsignedMediumInteger', 'unsignedsmallinteger' => 'unsignedSmallInteger', 'unsignedtinyinteger' => 'unsignedTinyInteger', + 'ulid' => 'ulid', 'uuid' => 'uuid', 'year' => 'year', ]; From fd7e00958bc646511f92a2de819c28b7aaaecbd3 Mon Sep 17 00:00:00 2001 From: rcrosbourne Date: Wed, 6 Dec 2023 23:04:27 -0500 Subject: [PATCH 11/30] Add support for ULID shorthand in migrations A ULID shorthand has been introduced to the MigrationGeneratorTest, allowing ULID fields to be easily added through drafts. This is captured in new tests and the corresponding fixture files, validating its successful integration. --- .../Generators/MigrationGeneratorTest.php | 1 + tests/fixtures/drafts/ulid-shorthand.yaml | 5 ++++ tests/fixtures/migrations/ulid-shorthand.php | 28 +++++++++++++++++++ 3 files changed, 34 insertions(+) create mode 100644 tests/fixtures/drafts/ulid-shorthand.yaml create mode 100644 tests/fixtures/migrations/ulid-shorthand.php diff --git a/tests/Feature/Generators/MigrationGeneratorTest.php b/tests/Feature/Generators/MigrationGeneratorTest.php index b29d8938..8e520080 100644 --- a/tests/Feature/Generators/MigrationGeneratorTest.php +++ b/tests/Feature/Generators/MigrationGeneratorTest.php @@ -529,6 +529,7 @@ public static function modelTreeDataProvider() ['drafts/disable-auto-columns.yaml', 'database/migrations/timestamp_create_states_table.php', 'migrations/disable-auto-columns.php'], ['drafts/uuid-shorthand.yaml', 'database/migrations/timestamp_create_people_table.php', 'migrations/uuid-shorthand.php'], ['drafts/uuid-shorthand-invalid-relationship.yaml', 'database/migrations/timestamp_create_age_cohorts_table.php', 'migrations/uuid-shorthand-invalid-relationship.php'], + ['drafts/ulid-shorthand.yaml', 'database/migrations/timestamp_create_people_table.php', 'migrations/ulid-shorthand.php'], ['drafts/uuid-without-relationship.yaml', 'database/migrations/timestamp_create_vats_table.php', 'migrations/uuid-without-relationship.php'], ['drafts/unconventional-foreign-key.yaml', 'database/migrations/timestamp_create_states_table.php', 'migrations/unconventional-foreign-key.php'], ['drafts/resource-statements.yaml', 'database/migrations/timestamp_create_users_table.php', 'migrations/resource-statements.php'], diff --git a/tests/fixtures/drafts/ulid-shorthand.yaml b/tests/fixtures/drafts/ulid-shorthand.yaml new file mode 100644 index 00000000..f892cec2 --- /dev/null +++ b/tests/fixtures/drafts/ulid-shorthand.yaml @@ -0,0 +1,5 @@ +models: + Person: + ulid + company_id: ulid + timestamps diff --git a/tests/fixtures/migrations/ulid-shorthand.php b/tests/fixtures/migrations/ulid-shorthand.php new file mode 100644 index 00000000..482cb24b --- /dev/null +++ b/tests/fixtures/migrations/ulid-shorthand.php @@ -0,0 +1,28 @@ +ulid('id')->primary(); + $table->ulid('company_id'); + $table->timestamps(); + }); + } + + /** + * Reverse the migrations. + */ + public function down(): void + { + Schema::dropIfExists('people'); + } +}; From 6b4d86d90bad512102acdd9ece03e08d500cc18d Mon Sep 17 00:00:00 2001 From: rcrosbourne Date: Thu, 7 Dec 2023 04:59:19 -0500 Subject: [PATCH 12/30] Add ULID support to FactoryGenerator and tests ULID column type has been added to the FactoryGenerator and corresponding tests. This update augments the list of column data types that can be used, particularly aiding situations where a universally unique lexicographically sortable identifier (ULID) is required. Test fixtures are updated to validate this new addition. --- src/Generators/FactoryGenerator.php | 7 +++++-- tests/fixtures/drafts/all-column-types.yaml | 1 + tests/fixtures/factories/all-column-types.php | 1 + 3 files changed, 7 insertions(+), 2 deletions(-) diff --git a/src/Generators/FactoryGenerator.php b/src/Generators/FactoryGenerator.php index 87f76d35..b7d860cd 100644 --- a/src/Generators/FactoryGenerator.php +++ b/src/Generators/FactoryGenerator.php @@ -55,7 +55,6 @@ protected function populateStub(string $stub, Model $model): string $stub = str_replace('//', $this->buildDefinition($model), $stub); $stub = str_replace('{{ namespace }}', 'Database\Factories' . ($model->namespace() ? '\\' . $model->namespace() : ''), $stub); $stub = str_replace('use {{ namespacedModel }};', $this->buildImports($model), $stub); - return $stub; } @@ -110,7 +109,7 @@ protected function buildDefinition(Model $model): string $definition .= sprintf('%s::factory()->create()->%s', $class, $key); $definition .= ',' . PHP_EOL; } - } elseif ($column->dataType() === 'id' || ($column->dataType() === 'uuid' && Str::endsWith($column->name(), '_id'))) { + } elseif ($column->dataType() === 'id' || (in_array($column->dataType(), ['uuid', 'ulid']) && Str::endsWith($column->name(), '_id'))) { $name = Str::beforeLast($column->name(), '_id'); $class = Str::studly($column->attributes()[0] ?? $name); $reference = $this->fullyQualifyModelReference($class) ?? $model; @@ -156,6 +155,10 @@ protected function buildDefinition(Model $model): string $definition .= str_repeat(self::INDENT, 3) . "'{$column->name()}' => "; $definition .= 'Str::random(10)'; $definition .= ',' . PHP_EOL; + } elseif ($column->dataType() === 'ulid') { + $definition .= str_repeat(self::INDENT, 3) . "'{$column->name()}' => "; + $definition .= '(string) Str::ulid()'; + $definition .= ',' . PHP_EOL; } else { $definition .= str_repeat(self::INDENT, 3) . "'{$column->name()}' => "; diff --git a/tests/fixtures/drafts/all-column-types.yaml b/tests/fixtures/drafts/all-column-types.yaml index 123d44fe..59b98c8e 100644 --- a/tests/fixtures/drafts/all-column-types.yaml +++ b/tests/fixtures/drafts/all-column-types.yaml @@ -50,5 +50,6 @@ models: unsignedMediumInteger: unsignedMediumInteger unsignedSmallInteger: unsignedSmallInteger unsignedTinyInteger: unsignedTinyInteger + ulid: ulid uuid: uuid year: year diff --git a/tests/fixtures/factories/all-column-types.php b/tests/fixtures/factories/all-column-types.php index 741b3ad8..317d4116 100644 --- a/tests/fixtures/factories/all-column-types.php +++ b/tests/fixtures/factories/all-column-types.php @@ -68,6 +68,7 @@ public function definition(): array 'unsignedMediumInteger' => $this->faker->randomNumber(), 'unsignedSmallInteger' => $this->faker->randomNumber(), 'unsignedTinyInteger' => $this->faker->randomDigitNotNull(), + 'ulid' => (string) Str::ulid(), 'uuid' => $this->faker->uuid(), 'year' => $this->faker->year(), ]; From dece6d98058584373e239058d000fdf4c3fe07f9 Mon Sep 17 00:00:00 2001 From: rcrosbourne Date: Thu, 7 Dec 2023 05:01:42 -0500 Subject: [PATCH 13/30] Add ULID support in all-column-types test fixture The ULID column type has been added to the 'all-column-types' test fixture. --- tests/fixtures/models/all-column-types.php | 1 + 1 file changed, 1 insertion(+) diff --git a/tests/fixtures/models/all-column-types.php b/tests/fixtures/models/all-column-types.php index be6427be..5b064ac0 100644 --- a/tests/fixtures/models/all-column-types.php +++ b/tests/fixtures/models/all-column-types.php @@ -64,6 +64,7 @@ class AllType extends Model 'unsignedMediumInteger', 'unsignedSmallInteger', 'unsignedTinyInteger', + 'ulid', 'uuid', 'year', ]; From 381bff87e995831929720b891cfd83212b17a2d5 Mon Sep 17 00:00:00 2001 From: rcrosbourne Date: Thu, 7 Dec 2023 05:02:42 -0500 Subject: [PATCH 14/30] Update FactoryGenerator fixed code format. --- src/Generators/FactoryGenerator.php | 1 + 1 file changed, 1 insertion(+) diff --git a/src/Generators/FactoryGenerator.php b/src/Generators/FactoryGenerator.php index b7d860cd..04d5ffa1 100644 --- a/src/Generators/FactoryGenerator.php +++ b/src/Generators/FactoryGenerator.php @@ -55,6 +55,7 @@ protected function populateStub(string $stub, Model $model): string $stub = str_replace('//', $this->buildDefinition($model), $stub); $stub = str_replace('{{ namespace }}', 'Database\Factories' . ($model->namespace() ? '\\' . $model->namespace() : ''), $stub); $stub = str_replace('use {{ namespacedModel }};', $this->buildImports($model), $stub); + return $stub; } From bf058c9a375f88d25367bee472dd18351f337f98 Mon Sep 17 00:00:00 2001 From: rcrosbourne Date: Fri, 8 Dec 2023 12:38:59 -0500 Subject: [PATCH 15/30] Add tests and migration for ulid shorthand invalid relationship Two new files were created to handle the case of a ulid shorthand with an invalid relationship. This includes a test case in the MigrationGeneratorTest.php file and a matching migration file. A draft file for ulid-shorthand-invalid-relationship was also added to the fixtures folder. --- .../Generators/MigrationGeneratorTest.php | 3 +- .../ulid-shorthand-invalid-relationship.yaml | 9 ++++++ .../ulid-shorthand-invalid-relationship.php | 32 +++++++++++++++++++ 3 files changed, 43 insertions(+), 1 deletion(-) create mode 100644 tests/fixtures/drafts/ulid-shorthand-invalid-relationship.yaml create mode 100644 tests/fixtures/migrations/ulid-shorthand-invalid-relationship.php diff --git a/tests/Feature/Generators/MigrationGeneratorTest.php b/tests/Feature/Generators/MigrationGeneratorTest.php index 8e520080..de80813b 100644 --- a/tests/Feature/Generators/MigrationGeneratorTest.php +++ b/tests/Feature/Generators/MigrationGeneratorTest.php @@ -527,9 +527,10 @@ public static function modelTreeDataProvider() ['drafts/optimize.yaml', 'database/migrations/timestamp_create_optimizes_table.php', 'migrations/optimize.php'], ['drafts/model-key-constraints.yaml', 'database/migrations/timestamp_create_orders_table.php', 'migrations/model-key-constraints.php'], ['drafts/disable-auto-columns.yaml', 'database/migrations/timestamp_create_states_table.php', 'migrations/disable-auto-columns.php'], + ['drafts/ulid-shorthand.yaml', 'database/migrations/timestamp_create_people_table.php', 'migrations/ulid-shorthand.php'], + ['drafts/ulid-shorthand-invalid-relationship.yaml', 'database/migrations/timestamp_create_age_cohorts_table.php', 'migrations/ulid-shorthand-invalid-relationship.php'], ['drafts/uuid-shorthand.yaml', 'database/migrations/timestamp_create_people_table.php', 'migrations/uuid-shorthand.php'], ['drafts/uuid-shorthand-invalid-relationship.yaml', 'database/migrations/timestamp_create_age_cohorts_table.php', 'migrations/uuid-shorthand-invalid-relationship.php'], - ['drafts/ulid-shorthand.yaml', 'database/migrations/timestamp_create_people_table.php', 'migrations/ulid-shorthand.php'], ['drafts/uuid-without-relationship.yaml', 'database/migrations/timestamp_create_vats_table.php', 'migrations/uuid-without-relationship.php'], ['drafts/unconventional-foreign-key.yaml', 'database/migrations/timestamp_create_states_table.php', 'migrations/unconventional-foreign-key.php'], ['drafts/resource-statements.yaml', 'database/migrations/timestamp_create_users_table.php', 'migrations/resource-statements.php'], diff --git a/tests/fixtures/drafts/ulid-shorthand-invalid-relationship.yaml b/tests/fixtures/drafts/ulid-shorthand-invalid-relationship.yaml new file mode 100644 index 00000000..9c0e431a --- /dev/null +++ b/tests/fixtures/drafts/ulid-shorthand-invalid-relationship.yaml @@ -0,0 +1,9 @@ +models: + AgeCohort: + id + timestamps + name: string:100 + description: string:500 nullable + min_age: integer nullable + max_age: integer nullable + ulid: ulid unique diff --git a/tests/fixtures/migrations/ulid-shorthand-invalid-relationship.php b/tests/fixtures/migrations/ulid-shorthand-invalid-relationship.php new file mode 100644 index 00000000..c72a0cd5 --- /dev/null +++ b/tests/fixtures/migrations/ulid-shorthand-invalid-relationship.php @@ -0,0 +1,32 @@ +id(); + $table->string('name', 100); + $table->string('description', 500)->nullable(); + $table->integer('min_age')->nullable(); + $table->integer('max_age')->nullable(); + $table->ulid('ulid')->unique(); + $table->timestamps(); + }); + } + + /** + * Reverse the migrations. + */ + public function down(): void + { + Schema::dropIfExists('age_cohorts'); + } +}; From bb24726f1f7357e02be1d6e34bb5442a5d192d87 Mon Sep 17 00:00:00 2001 From: rcrosbourne Date: Fri, 8 Dec 2023 12:44:20 -0500 Subject: [PATCH 16/30] Add test for ULID without relationship scenario A new test for the case of a ULID without a relationship was added in MigrationGeneratorTest.php file. Along with this, corresponding migration and draft files were created under the fixtures directory. This update aids in handling scenarios with ULID that do not have any specified relationships. --- .../Generators/MigrationGeneratorTest.php | 1 + .../drafts/ulid-without-relationship.yaml | 3 ++ .../migrations/ulid-without-relationship.php | 28 +++++++++++++++++++ 3 files changed, 32 insertions(+) create mode 100644 tests/fixtures/drafts/ulid-without-relationship.yaml create mode 100644 tests/fixtures/migrations/ulid-without-relationship.php diff --git a/tests/Feature/Generators/MigrationGeneratorTest.php b/tests/Feature/Generators/MigrationGeneratorTest.php index de80813b..0c3a32cc 100644 --- a/tests/Feature/Generators/MigrationGeneratorTest.php +++ b/tests/Feature/Generators/MigrationGeneratorTest.php @@ -529,6 +529,7 @@ public static function modelTreeDataProvider() ['drafts/disable-auto-columns.yaml', 'database/migrations/timestamp_create_states_table.php', 'migrations/disable-auto-columns.php'], ['drafts/ulid-shorthand.yaml', 'database/migrations/timestamp_create_people_table.php', 'migrations/ulid-shorthand.php'], ['drafts/ulid-shorthand-invalid-relationship.yaml', 'database/migrations/timestamp_create_age_cohorts_table.php', 'migrations/ulid-shorthand-invalid-relationship.php'], + ['drafts/ulid-without-relationship.yaml', 'database/migrations/timestamp_create_vats_table.php', 'migrations/ulid-without-relationship.php'], ['drafts/uuid-shorthand.yaml', 'database/migrations/timestamp_create_people_table.php', 'migrations/uuid-shorthand.php'], ['drafts/uuid-shorthand-invalid-relationship.yaml', 'database/migrations/timestamp_create_age_cohorts_table.php', 'migrations/uuid-shorthand-invalid-relationship.php'], ['drafts/uuid-without-relationship.yaml', 'database/migrations/timestamp_create_vats_table.php', 'migrations/uuid-without-relationship.php'], diff --git a/tests/fixtures/drafts/ulid-without-relationship.yaml b/tests/fixtures/drafts/ulid-without-relationship.yaml new file mode 100644 index 00000000..688eefd0 --- /dev/null +++ b/tests/fixtures/drafts/ulid-without-relationship.yaml @@ -0,0 +1,3 @@ +models: + Vat: + ulid: ulid diff --git a/tests/fixtures/migrations/ulid-without-relationship.php b/tests/fixtures/migrations/ulid-without-relationship.php new file mode 100644 index 00000000..f51c5750 --- /dev/null +++ b/tests/fixtures/migrations/ulid-without-relationship.php @@ -0,0 +1,28 @@ +id(); + $table->ulid('ulid'); + $table->timestamps(); + }); + } + + /** + * Reverse the migrations. + */ + public function down(): void + { + Schema::dropIfExists('vats'); + } +}; From 0ff644a496b3c3257c0539fb7aece84ab43d74cb Mon Sep 17 00:00:00 2001 From: rcrosbourne Date: Fri, 8 Dec 2023 12:57:05 -0500 Subject: [PATCH 17/30] Add ULID Morphs to test fixtures 'ulidMorphs' was added to the test fixtures in all-column-types for drafts, models, and factories. --- tests/fixtures/drafts/all-column-types.yaml | 1 + tests/fixtures/factories/all-column-types.php | 1 + tests/fixtures/models/all-column-types.php | 1 + 3 files changed, 3 insertions(+) diff --git a/tests/fixtures/drafts/all-column-types.yaml b/tests/fixtures/drafts/all-column-types.yaml index 59b98c8e..61c3606f 100644 --- a/tests/fixtures/drafts/all-column-types.yaml +++ b/tests/fixtures/drafts/all-column-types.yaml @@ -25,6 +25,7 @@ models: mediumInteger: mediumInteger mediumText: mediumText morphs: morphs + ulidMorphs: ulidMorphs uuidMorphs: uuidMorphs multiLineString: multiLineString multiPoint: multiPoint diff --git a/tests/fixtures/factories/all-column-types.php b/tests/fixtures/factories/all-column-types.php index 317d4116..d45699b2 100644 --- a/tests/fixtures/factories/all-column-types.php +++ b/tests/fixtures/factories/all-column-types.php @@ -46,6 +46,7 @@ public function definition(): array 'mediumText' => $this->faker->text(), 'morphs_id' => $this->faker->randomDigitNotNull(), 'morphs_type' => $this->faker->word(), + 'ulidMorphs' => $this->faker->word(), 'uuidMorphs' => $this->faker->word(), 'multiLineString' => $this->faker->word(), 'multiPoint' => $this->faker->word(), diff --git a/tests/fixtures/models/all-column-types.php b/tests/fixtures/models/all-column-types.php index 5b064ac0..abef46d7 100644 --- a/tests/fixtures/models/all-column-types.php +++ b/tests/fixtures/models/all-column-types.php @@ -39,6 +39,7 @@ class AllType extends Model 'mediumInteger', 'mediumText', 'morphs', + 'ulidMorphs', 'uuidMorphs', 'multiLineString', 'multiPoint', From 14887d717729612341425339ae2d0ff73d037ce2 Mon Sep 17 00:00:00 2001 From: rcrosbourne Date: Fri, 8 Dec 2023 16:18:00 -0500 Subject: [PATCH 18/30] Update Foreign Key Constraints and Migration Generator Foreign key constraints updated in model test fixtures and new foreign keys 'customer_id' and 'tran_id' added. Migration Generator has been modified to handle ULID datatype along with ID and UUID. This commit adapts the Migration Generator to recognize and handle the ULID datatype and updates tests to reflect this change. --- src/Generators/MigrationGenerator.php | 27 +++++++++---------- .../drafts/model-key-constraints.yaml | 2 ++ .../factories/model-key-constraints.php | 4 +++ .../migrations/model-key-constraints.php | 2 ++ 4 files changed, 21 insertions(+), 14 deletions(-) diff --git a/src/Generators/MigrationGenerator.php b/src/Generators/MigrationGenerator.php index 0910e2fa..fc2b0f1e 100644 --- a/src/Generators/MigrationGenerator.php +++ b/src/Generators/MigrationGenerator.php @@ -194,7 +194,7 @@ protected function buildDefinition(Model $model): string ); } - if (!empty($columnAttributes) && !$this->isIdOrUuid($column->dataType())) { + if (!empty($columnAttributes) && !$this->isIdUlidOrUuid($column->dataType())) { $column_definition .= ', '; if (in_array($column->dataType(), ['set', 'enum'])) { @@ -221,7 +221,7 @@ protected function buildDefinition(Model $model): string $column->modifiers() ); - if ($this->isIdOrUuid($column->dataType())) { + if ($this->isIdUlidOrUuid($column->dataType())) { $column_definition = $foreign; $foreign = ''; } @@ -232,7 +232,7 @@ protected function buildDefinition(Model $model): string || (is_array($modifier) && key($modifier) === 'onDelete') || (is_array($modifier) && key($modifier) === 'onUpdate') || $modifier === 'foreign' - || ($modifier === 'nullable' && $this->isIdOrUuid($column->dataType())) + || ($modifier === 'nullable' && $this->isIdUlidOrUuid($column->dataType())) ); } @@ -347,7 +347,7 @@ protected function buildForeignKey(string $column_name, ?string $on, string $typ $column = Str::afterLast($column_name, '_'); } - if ($this->isIdOrUuid($type) && !empty($attributes)) { + if ($this->isIdUlidOrUuid($type) && !empty($attributes)) { $table = Str::lower(Str::plural($attributes[0])); } @@ -364,12 +364,12 @@ protected function buildForeignKey(string $column_name, ?string $on, string $typ $on_update_suffix = self::ON_UPDATE_CLAUSES[$on_update_clause]; } - if ($this->isIdOrUuid($type)) { - if ($type === 'uuid') { - $method = 'foreignUuid'; - } else { - $method = 'foreignId'; - } + if ($this->isIdUlidOrUuid($type)) { + $method = match ($type) { + 'ulid' => 'foreignUlid', + 'uuid' => 'foreignUuid', + default => 'foreignId', + }; $prefix = in_array('nullable', $modifiers) ? '$table->' . "{$method}('{$column_name}')->nullable()" @@ -381,7 +381,6 @@ protected function buildForeignKey(string $column_name, ?string $on, string $typ if ($on_update_clause === 'cascade') { $on_update_suffix = '->cascadeOnUpdate()'; } - if ($column_name === Str::singular($table) . '_' . $column) { return self::INDENT . "{$prefix}->constrained(){$on_delete_suffix}{$on_update_suffix}"; } @@ -469,7 +468,7 @@ private function shouldAddForeignKeyConstraint(\Blueprint\Models\Column $column) } return config('blueprint.use_constraints') - && ($this->isIdOrUuid($column->dataType()) && Str::endsWith($column->name(), '_id')); + && ($this->isIdUlidOrUuid($column->dataType()) && Str::endsWith($column->name(), '_id')); } protected function isNumericDefault(string $type, string $value): bool @@ -486,8 +485,8 @@ protected function isNumericDefault(string $type, string $value): bool ->contains(fn ($value) => strtolower($value) === strtolower($type)); } - protected function isIdOrUuid(string $dataType): bool + protected function isIdUlidOrUuid(string $dataType): bool { - return in_array($dataType, ['id', 'uuid']); + return in_array($dataType, ['id', 'ulid', 'uuid']); } } diff --git a/tests/fixtures/drafts/model-key-constraints.yaml b/tests/fixtures/drafts/model-key-constraints.yaml index ce109f72..4cb9f9ba 100644 --- a/tests/fixtures/drafts/model-key-constraints.yaml +++ b/tests/fixtures/drafts/model-key-constraints.yaml @@ -6,3 +6,5 @@ models: sub_id: uuid foreign:subscription expires_at: timestamp nullable index meta: json default:'[]' + customer_id: ulid foreign + tran_id: ulid foreign:transaction diff --git a/tests/fixtures/factories/model-key-constraints.php b/tests/fixtures/factories/model-key-constraints.php index 3cd3dc09..2e270d11 100644 --- a/tests/fixtures/factories/model-key-constraints.php +++ b/tests/fixtures/factories/model-key-constraints.php @@ -4,8 +4,10 @@ use Illuminate\Database\Eloquent\Factories\Factory; use Illuminate\Support\Str; +use App\Models\Customer; use App\Models\Order; use App\Models\Subscription; +use App\Models\Transaction; use App\Models\User; class OrderFactory extends Factory @@ -28,6 +30,8 @@ public function definition(): array 'sub_id' => Subscription::factory(), 'expires_at' => $this->faker->dateTime(), 'meta' => '[]', + 'customer_id' => Customer::factory(), + 'tran_id' => Transaction::factory(), ]; } } diff --git a/tests/fixtures/migrations/model-key-constraints.php b/tests/fixtures/migrations/model-key-constraints.php index 60834d68..0526a610 100644 --- a/tests/fixtures/migrations/model-key-constraints.php +++ b/tests/fixtures/migrations/model-key-constraints.php @@ -20,6 +20,8 @@ public function up(): void $table->foreignUuid('sub_id')->constrained('subscriptions'); $table->timestamp('expires_at')->nullable()->index(); $table->json('meta')->default('[]'); + $table->foreignUlid('customer_id')->constrained(); + $table->foreignUlid('tran_id')->constrained('transactions'); $table->timestamps(); }); From 5e827d3952e78724b0d1cc8427763560cd2398df Mon Sep 17 00:00:00 2001 From: rcrosbourne Date: Fri, 8 Dec 2023 16:22:17 -0500 Subject: [PATCH 19/30] Add ULID shorthand test to BlueprintTest A new function, `it_parses_ulid_shorthand`, is added to BlueprintTest to ensure correct parsing of ULID shorthand. Accordingly, assertions have also been updated to reflect this new generation method in the relevant test fixtures and new keys handling. --- tests/Feature/BlueprintTest.php | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/tests/Feature/BlueprintTest.php b/tests/Feature/BlueprintTest.php index 2cb1d459..659bcb9d 100644 --- a/tests/Feature/BlueprintTest.php +++ b/tests/Feature/BlueprintTest.php @@ -117,6 +117,22 @@ public function it_parses_shorthands(): void ], $this->subject->parse($blueprint)); } + #[Test] + public function it_parses_ulid_shorthand(): void + { + $blueprint = $this->fixture('drafts/ulid-shorthand.yaml'); + + $this->assertEquals([ + 'models' => [ + 'Person' => [ + 'id' => 'ulid primary', + 'timestamps' => 'timestamps', + 'company_id' => 'ulid', + ], + ], + ], $this->subject->parse($blueprint)); + } + #[Test] public function it_parses_uuid_shorthand(): void { From 690bcf7b30dedbdeffdec803ac3e245637a8942e Mon Sep 17 00:00:00 2001 From: rcrosbourne Date: Fri, 8 Dec 2023 22:44:27 -0500 Subject: [PATCH 20/30] Add support for `nullableUlidMorphs` and `ulidMorphs` column types The `nullableUlidMorphs` and `ulidMorphs` column types have been added to the `all-column-types` test fixtures and model generator. The appropriate handling for these new column types were added to the `ModelGenerator` PHP class. This addition ensures that these column types are properly processed and reflected in the models. --- src/Generators/ModelGenerator.php | 10 ++++++++++ tests/fixtures/drafts/all-column-types.yaml | 1 + tests/fixtures/models/all-column-types.php | 1 + 3 files changed, 12 insertions(+) diff --git a/src/Generators/ModelGenerator.php b/src/Generators/ModelGenerator.php index 1f7cabef..f039105d 100644 --- a/src/Generators/ModelGenerator.php +++ b/src/Generators/ModelGenerator.php @@ -90,6 +90,16 @@ protected function buildClassPhpDoc(Model $model): string $phpDoc .= PHP_EOL; $phpDoc .= ' * @property string|null $' . $column->name() . '_type'; $phpDoc .= PHP_EOL; + } elseif ($column->dataType() === 'ulidMorphs') { + $phpDoc .= ' * @property string $' . $column->name() . '_id'; + $phpDoc .= PHP_EOL; + $phpDoc .= ' * @property string $' . $column->name() . '_type'; + $phpDoc .= PHP_EOL; + } elseif ($column->dataType() === 'nullableUlidMorphs') { + $phpDoc .= ' * @property string|null $' . $column->name() . '_id'; + $phpDoc .= PHP_EOL; + $phpDoc .= ' * @property string|null $' . $column->name() . '_type'; + $phpDoc .= PHP_EOL; } elseif ($column->dataType() === 'uuidMorphs') { $phpDoc .= ' * @property string $' . $column->name() . '_id'; $phpDoc .= PHP_EOL; diff --git a/tests/fixtures/drafts/all-column-types.yaml b/tests/fixtures/drafts/all-column-types.yaml index 61c3606f..7311b6db 100644 --- a/tests/fixtures/drafts/all-column-types.yaml +++ b/tests/fixtures/drafts/all-column-types.yaml @@ -31,6 +31,7 @@ models: multiPoint: multiPoint multiPolygon: multiPolygon nullableMorphs: nullableMorphs + nullableUlidMorphs: nullableUuidMorphs nullableUuidMorphs: nullableUuidMorphs nullableTimestamps: nullableTimestamps point: point diff --git a/tests/fixtures/models/all-column-types.php b/tests/fixtures/models/all-column-types.php index abef46d7..b8b8f227 100644 --- a/tests/fixtures/models/all-column-types.php +++ b/tests/fixtures/models/all-column-types.php @@ -45,6 +45,7 @@ class AllType extends Model 'multiPoint', 'multiPolygon', 'nullableMorphs', + 'nullableUlidMorphs', 'nullableUuidMorphs', 'nullableTimestamps', 'point', From d63b4415b7b8554bf690782dda3a6697729bd886 Mon Sep 17 00:00:00 2001 From: rcrosbourne Date: Fri, 8 Dec 2023 22:53:43 -0500 Subject: [PATCH 21/30] Added ULID support in Model and ModelGenerator classes Added a function to check if ULIDs are used in the 'Model' class. Expanded 'ModelGenerator' class to act accordingly if ULIDs are used. Added respective test case and fixture files to ensure correct functionality. --- src/Generators/ModelGenerator.php | 6 ++++ src/Models/Model.php | 4 +++ .../Feature/Generators/ModelGeneratorTest.php | 1 + tests/fixtures/drafts/model-with-ulid-id.yaml | 5 +++ .../fixtures/models/model-with-ulid-trait.php | 31 +++++++++++++++++++ 5 files changed, 47 insertions(+) create mode 100644 tests/fixtures/drafts/model-with-ulid-id.yaml create mode 100644 tests/fixtures/models/model-with-ulid-trait.php diff --git a/src/Generators/ModelGenerator.php b/src/Generators/ModelGenerator.php index f039105d..52c42f4b 100644 --- a/src/Generators/ModelGenerator.php +++ b/src/Generators/ModelGenerator.php @@ -64,6 +64,7 @@ protected function populateStub(string $stub, Model $model): string $stub = $this->addTraits($model, $stub); $stub = str_replace('{{ imports }}', $this->buildImports($model), $stub); + dump($stub); return $stub; } @@ -288,6 +289,11 @@ protected function addTraits(Model $model, $stub): string $traits[] = 'SoftDeletes'; } + if ($model->usesUlids()) { + $this->addImport($model, 'Illuminate\\Database\\Eloquent\\Concerns\\HasUlids'); + $traits[] = 'HasUlids'; + } + if ($model->usesUuids()) { $this->addImport($model, 'Illuminate\\Database\\Eloquent\\Concerns\\HasUuids'); $traits[] = 'HasUuids'; diff --git a/src/Models/Model.php b/src/Models/Model.php index cee66fd7..ce890476 100644 --- a/src/Models/Model.php +++ b/src/Models/Model.php @@ -96,6 +96,10 @@ public function usesPrimaryKey(): bool return $this->primaryKey !== false; } + public function usesUlids(): bool + { + return $this->usesPrimaryKey() && $this->columns[$this->primaryKey]->dataType() === 'ulid'; + } public function usesUuids(): bool { return $this->usesPrimaryKey() && $this->columns[$this->primaryKey]->dataType() === 'uuid'; diff --git a/tests/Feature/Generators/ModelGeneratorTest.php b/tests/Feature/Generators/ModelGeneratorTest.php index eb8b2b6f..52f88a17 100644 --- a/tests/Feature/Generators/ModelGeneratorTest.php +++ b/tests/Feature/Generators/ModelGeneratorTest.php @@ -642,6 +642,7 @@ public static function modelTreeDataProvider(): array ['drafts/uuid-shorthand-invalid-relationship.yaml', 'app/Models/AgeCohort.php', 'models/uuid-shorthand-invalid-relationship.php'], ['drafts/model-with-meta.yaml', 'app/Models/Post.php', 'models/model-with-meta.php'], ['drafts/infer-belongsto.yaml', 'app/Models/Conference.php', 'models/infer-belongsto.php'], + ['drafts/model-with-ulid-id.yaml', 'app/Models/User.php', 'models/model-with-ulid-trait.php'], ['drafts/model-with-uuid-id.yaml', 'app/Models/User.php', 'models/model-with-uuid-trait.php'], ]; } diff --git a/tests/fixtures/drafts/model-with-ulid-id.yaml b/tests/fixtures/drafts/model-with-ulid-id.yaml new file mode 100644 index 00000000..db022ffa --- /dev/null +++ b/tests/fixtures/drafts/model-with-ulid-id.yaml @@ -0,0 +1,5 @@ +models: + User: + id: ulid primary + name: string + base_pay: decimal:10,2 diff --git a/tests/fixtures/models/model-with-ulid-trait.php b/tests/fixtures/models/model-with-ulid-trait.php new file mode 100644 index 00000000..5738e6ef --- /dev/null +++ b/tests/fixtures/models/model-with-ulid-trait.php @@ -0,0 +1,31 @@ + 'decimal:2', + ]; +} From d8926797d2896311e18aefc793104a6c0d98ade9 Mon Sep 17 00:00:00 2001 From: rcrosbourne Date: Fri, 8 Dec 2023 22:53:43 -0500 Subject: [PATCH 22/30] Added ULID support in Model and ModelGenerator classes Added a function to check if ULIDs are used in the 'Model' class. Expanded 'ModelGenerator' class to act accordingly if ULIDs are used. Added respective test case and fixture files to ensure correct functionality. --- src/Generators/ModelGenerator.php | 6 ++++ src/Models/Model.php | 4 +++ .../Feature/Generators/ModelGeneratorTest.php | 1 + tests/fixtures/drafts/model-with-ulid-id.yaml | 5 +++ .../fixtures/models/model-with-ulid-trait.php | 31 +++++++++++++++++++ 5 files changed, 47 insertions(+) create mode 100644 tests/fixtures/drafts/model-with-ulid-id.yaml create mode 100644 tests/fixtures/models/model-with-ulid-trait.php diff --git a/src/Generators/ModelGenerator.php b/src/Generators/ModelGenerator.php index f039105d..52c42f4b 100644 --- a/src/Generators/ModelGenerator.php +++ b/src/Generators/ModelGenerator.php @@ -64,6 +64,7 @@ protected function populateStub(string $stub, Model $model): string $stub = $this->addTraits($model, $stub); $stub = str_replace('{{ imports }}', $this->buildImports($model), $stub); + dump($stub); return $stub; } @@ -288,6 +289,11 @@ protected function addTraits(Model $model, $stub): string $traits[] = 'SoftDeletes'; } + if ($model->usesUlids()) { + $this->addImport($model, 'Illuminate\\Database\\Eloquent\\Concerns\\HasUlids'); + $traits[] = 'HasUlids'; + } + if ($model->usesUuids()) { $this->addImport($model, 'Illuminate\\Database\\Eloquent\\Concerns\\HasUuids'); $traits[] = 'HasUuids'; diff --git a/src/Models/Model.php b/src/Models/Model.php index cee66fd7..ce890476 100644 --- a/src/Models/Model.php +++ b/src/Models/Model.php @@ -96,6 +96,10 @@ public function usesPrimaryKey(): bool return $this->primaryKey !== false; } + public function usesUlids(): bool + { + return $this->usesPrimaryKey() && $this->columns[$this->primaryKey]->dataType() === 'ulid'; + } public function usesUuids(): bool { return $this->usesPrimaryKey() && $this->columns[$this->primaryKey]->dataType() === 'uuid'; diff --git a/tests/Feature/Generators/ModelGeneratorTest.php b/tests/Feature/Generators/ModelGeneratorTest.php index eb8b2b6f..52f88a17 100644 --- a/tests/Feature/Generators/ModelGeneratorTest.php +++ b/tests/Feature/Generators/ModelGeneratorTest.php @@ -642,6 +642,7 @@ public static function modelTreeDataProvider(): array ['drafts/uuid-shorthand-invalid-relationship.yaml', 'app/Models/AgeCohort.php', 'models/uuid-shorthand-invalid-relationship.php'], ['drafts/model-with-meta.yaml', 'app/Models/Post.php', 'models/model-with-meta.php'], ['drafts/infer-belongsto.yaml', 'app/Models/Conference.php', 'models/infer-belongsto.php'], + ['drafts/model-with-ulid-id.yaml', 'app/Models/User.php', 'models/model-with-ulid-trait.php'], ['drafts/model-with-uuid-id.yaml', 'app/Models/User.php', 'models/model-with-uuid-trait.php'], ]; } diff --git a/tests/fixtures/drafts/model-with-ulid-id.yaml b/tests/fixtures/drafts/model-with-ulid-id.yaml new file mode 100644 index 00000000..db022ffa --- /dev/null +++ b/tests/fixtures/drafts/model-with-ulid-id.yaml @@ -0,0 +1,5 @@ +models: + User: + id: ulid primary + name: string + base_pay: decimal:10,2 diff --git a/tests/fixtures/models/model-with-ulid-trait.php b/tests/fixtures/models/model-with-ulid-trait.php new file mode 100644 index 00000000..5738e6ef --- /dev/null +++ b/tests/fixtures/models/model-with-ulid-trait.php @@ -0,0 +1,31 @@ + 'decimal:2', + ]; +} From 1170a341d9dd996b6a941202ac6f363059386abc Mon Sep 17 00:00:00 2001 From: rcrosbourne Date: Fri, 8 Dec 2023 22:55:56 -0500 Subject: [PATCH 23/30] Removed debug line and linted the code --- src/Generators/ModelGenerator.php | 1 - src/Models/Model.php | 1 + 2 files changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Generators/ModelGenerator.php b/src/Generators/ModelGenerator.php index 52c42f4b..7b9bddec 100644 --- a/src/Generators/ModelGenerator.php +++ b/src/Generators/ModelGenerator.php @@ -64,7 +64,6 @@ protected function populateStub(string $stub, Model $model): string $stub = $this->addTraits($model, $stub); $stub = str_replace('{{ imports }}', $this->buildImports($model), $stub); - dump($stub); return $stub; } diff --git a/src/Models/Model.php b/src/Models/Model.php index ce890476..e9cc3723 100644 --- a/src/Models/Model.php +++ b/src/Models/Model.php @@ -100,6 +100,7 @@ public function usesUlids(): bool { return $this->usesPrimaryKey() && $this->columns[$this->primaryKey]->dataType() === 'ulid'; } + public function usesUuids(): bool { return $this->usesPrimaryKey() && $this->columns[$this->primaryKey]->dataType() === 'uuid'; From 0f57f5bad4fdd2e2363f9bfefae0e0f93930eb1b Mon Sep 17 00:00:00 2001 From: rcrosbourne Date: Fri, 8 Dec 2023 23:14:37 -0500 Subject: [PATCH 24/30] remove dump and linted code --- src/Generators/ModelGenerator.php | 1 - src/Models/Model.php | 1 + 2 files changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Generators/ModelGenerator.php b/src/Generators/ModelGenerator.php index 52c42f4b..7b9bddec 100644 --- a/src/Generators/ModelGenerator.php +++ b/src/Generators/ModelGenerator.php @@ -64,7 +64,6 @@ protected function populateStub(string $stub, Model $model): string $stub = $this->addTraits($model, $stub); $stub = str_replace('{{ imports }}', $this->buildImports($model), $stub); - dump($stub); return $stub; } diff --git a/src/Models/Model.php b/src/Models/Model.php index ce890476..e9cc3723 100644 --- a/src/Models/Model.php +++ b/src/Models/Model.php @@ -100,6 +100,7 @@ public function usesUlids(): bool { return $this->usesPrimaryKey() && $this->columns[$this->primaryKey]->dataType() === 'ulid'; } + public function usesUuids(): bool { return $this->usesPrimaryKey() && $this->columns[$this->primaryKey]->dataType() === 'uuid'; From 47d05c6f604baf9ac1ebaa443dd3e37d9f048352 Mon Sep 17 00:00:00 2001 From: rcrosbourne Date: Sat, 9 Dec 2023 12:14:41 -0500 Subject: [PATCH 25/30] Added support for ULID and UUID in pivot tables migration This commit expands the migration generation function to support ULID and UUID primary keys, specifically in the context of pivot tables. Tests have been created for this new functionality, and related migration stubs have been updated. This allows the creation of pivot tables with ULID and UUID references, catering to a broader range of database design choices. --- src/Generators/MigrationGenerator.php | 23 +++++++++---- .../Generators/MigrationGeneratorTest.php | 29 ++++++++++++++++ .../drafts/belongs-to-many-using-ulids.yaml | 11 +++++++ .../belongs-to-many-pivot-using-ulids.php | 31 +++++++++++++++++ ...elongs-to-many-using-ulids-diary-model.php | 31 +++++++++++++++++ ...ongs-to-many-using-ulids-journey-model.php | 33 +++++++++++++++++++ 6 files changed, 152 insertions(+), 6 deletions(-) create mode 100644 tests/fixtures/drafts/belongs-to-many-using-ulids.yaml create mode 100644 tests/fixtures/migrations/belongs-to-many-pivot-using-ulids.php create mode 100644 tests/fixtures/migrations/belongs-to-many-using-ulids-diary-model.php create mode 100644 tests/fixtures/migrations/belongs-to-many-using-ulids-journey-model.php diff --git a/src/Generators/MigrationGenerator.php b/src/Generators/MigrationGenerator.php index fc2b0f1e..94487135 100644 --- a/src/Generators/MigrationGenerator.php +++ b/src/Generators/MigrationGenerator.php @@ -71,7 +71,7 @@ public function output(Tree $tree, $overwrite = false): array if (!empty($model->pivotTables())) { foreach ($model->pivotTables() as $pivotSegments) { $pivotTableName = $this->getPivotTableName($pivotSegments); - $tables['pivotTableNames'][$pivotTableName] = $this->populatePivotStub($stub, $pivotSegments); + $tables['pivotTableNames'][$pivotTableName] = $this->populatePivotStub($stub, $pivotSegments, $tree->models()); } } @@ -128,10 +128,10 @@ protected function populateStub(string $stub, Model $model): string return $stub; } - protected function populatePivotStub(string $stub, array $segments): string + protected function populatePivotStub(string $stub, array $segments, array $models = []): string { $stub = str_replace('{{ table }}', $this->getPivotTableName($segments), $stub); - $stub = str_replace('{{ definition }}', $this->buildPivotTableDefinition($segments), $stub); + $stub = str_replace('{{ definition }}', $this->buildPivotTableDefinition($segments, $models), $stub); if ($this->hasForeignKeyConstraints) { $stub = $this->disableForeignKeyConstraints($stub); @@ -290,10 +290,9 @@ protected function buildDefinition(Model $model): string return trim($definition); } - protected function buildPivotTableDefinition(array $segments): string + protected function buildPivotTableDefinition(array $segments, array $models = []): string { $definition = ''; - foreach ($segments as $segment) { $column = Str::before(Str::snake($segment), ':'); $references = 'id'; @@ -304,7 +303,19 @@ protected function buildPivotTableDefinition(array $segments): string $this->hasForeignKeyConstraints = true; $definition .= $this->buildForeignKey($foreign, $on, 'id') . ';' . PHP_EOL; } else { - $definition .= self::INDENT . '$table->foreignId(\'' . $foreign . '\');' . PHP_EOL; + $definition .= self::INDENT; + if (count($models) > 0 && array_key_exists($segment, $models)) { + $model = $models[$segment]; + if ($model->name() === $segment) { + $dataType = $model->columns()[$model->primaryKey()]->dataType(); + $definition .= $dataType === 'ulid' ? '$table->foreignUlid(\'' . $foreign . '\');' : + ($dataType === 'uuid' ? '$table->foreignUuid(\'' . $foreign . '\');' : + '$table->foreignId(\'' . $foreign . '\');'); + } + } else { + $definition .= '$table->foreignId(\'' . $foreign . '\');'; + } + $definition .= PHP_EOL; } } diff --git a/tests/Feature/Generators/MigrationGeneratorTest.php b/tests/Feature/Generators/MigrationGeneratorTest.php index 0c3a32cc..c4330475 100644 --- a/tests/Feature/Generators/MigrationGeneratorTest.php +++ b/tests/Feature/Generators/MigrationGeneratorTest.php @@ -150,6 +150,35 @@ public function output_creates_constraints_for_unconventional_foreign_reference_ $this->assertEquals(['created' => [$model_migration]], $this->subject->output($tree)); } + #[Test] + public function using_ulids_output_also_creates_pivot_table_migration(): void + { + $this->filesystem->expects('stub') + ->with('migration.stub') + ->andReturn($this->stub('migration.stub')); + + $now = Carbon::now(); + Carbon::setTestNow($now); + + $journey_model_migration = str_replace('timestamp', $now->copy()->subSeconds(2)->format('Y_m_d_His'), 'database/migrations/timestamp_create_journeys_table.php'); + $diary_model_migration = str_replace('timestamp', $now->copy()->subSecond()->format('Y_m_d_His'), 'database/migrations/timestamp_create_diaries_table.php'); + $pivot_migration = str_replace('timestamp', $now->format('Y_m_d_His'), 'database/migrations/timestamp_create_diary_journey_table.php'); + + $this->filesystem->expects('exists')->times(3)->andReturn(false); + + $this->filesystem->expects('put') + ->with($journey_model_migration, $this->fixture('migrations/belongs-to-many-using-ulids-journey-model.php')); + $this->filesystem->expects('put') + ->with($diary_model_migration, $this->fixture('migrations/belongs-to-many-using-ulids-diary-model.php')); + $this->filesystem->expects('put') + ->with($pivot_migration, $this->fixture('migrations/belongs-to-many-pivot-using-ulids.php')); + + $tokens = $this->blueprint->parse($this->fixture('drafts/belongs-to-many-using-ulids.yaml')); + $tree = $this->blueprint->analyze($tokens); + + $this->assertEquals(['created' => [$journey_model_migration, $diary_model_migration, $pivot_migration]], $this->subject->output($tree)); + } + #[Test] public function output_also_creates_pivot_table_migration(): void { diff --git a/tests/fixtures/drafts/belongs-to-many-using-ulids.yaml b/tests/fixtures/drafts/belongs-to-many-using-ulids.yaml new file mode 100644 index 00000000..64a84f73 --- /dev/null +++ b/tests/fixtures/drafts/belongs-to-many-using-ulids.yaml @@ -0,0 +1,11 @@ +models: + Journey: + id: ulid primary + name: string + user_id: ulid foreign + relationships: + belongsToMany: Diary + Diary: + id: ulid primary + relationships: + belongsToMany: Journey diff --git a/tests/fixtures/migrations/belongs-to-many-pivot-using-ulids.php b/tests/fixtures/migrations/belongs-to-many-pivot-using-ulids.php new file mode 100644 index 00000000..60445c38 --- /dev/null +++ b/tests/fixtures/migrations/belongs-to-many-pivot-using-ulids.php @@ -0,0 +1,31 @@ +foreignUlid('diary_id'); + $table->foreignUlid('journey_id'); + }); + + Schema::enableForeignKeyConstraints(); + } + + /** + * Reverse the migrations. + */ + public function down(): void + { + Schema::dropIfExists('diary_journey'); + } +}; diff --git a/tests/fixtures/migrations/belongs-to-many-using-ulids-diary-model.php b/tests/fixtures/migrations/belongs-to-many-using-ulids-diary-model.php new file mode 100644 index 00000000..96987a13 --- /dev/null +++ b/tests/fixtures/migrations/belongs-to-many-using-ulids-diary-model.php @@ -0,0 +1,31 @@ +ulid('id')->primary(); + $table->timestamps(); + }); + + Schema::enableForeignKeyConstraints(); + } + + /** + * Reverse the migrations. + */ + public function down(): void + { + Schema::dropIfExists('diaries'); + } +}; diff --git a/tests/fixtures/migrations/belongs-to-many-using-ulids-journey-model.php b/tests/fixtures/migrations/belongs-to-many-using-ulids-journey-model.php new file mode 100644 index 00000000..831e41fb --- /dev/null +++ b/tests/fixtures/migrations/belongs-to-many-using-ulids-journey-model.php @@ -0,0 +1,33 @@ +ulid('id')->primary(); + $table->string('name'); + $table->foreignUlid('user_id')->constrained(); + $table->timestamps(); + }); + + Schema::enableForeignKeyConstraints(); + } + + /** + * Reverse the migrations. + */ + public function down(): void + { + Schema::dropIfExists('journeys'); + } +}; From 461f4e13f74f1c227baf2772c76a4017c46c1506 Mon Sep 17 00:00:00 2001 From: rcrosbourne Date: Sat, 9 Dec 2023 12:54:39 -0500 Subject: [PATCH 26/30] Refactor foreign key generation in migrations The foreign key generation code in migration files has been refactored into a separate method. This update simplifies the main function by extracting complex logic and improves readability of the code. Additionally, the method now supports generation of primary keys with 'ulid' and 'uuid' data types for pivot tables. --- src/Generators/MigrationGenerator.php | 50 ++++++++++++++++++++------- 1 file changed, 37 insertions(+), 13 deletions(-) diff --git a/src/Generators/MigrationGenerator.php b/src/Generators/MigrationGenerator.php index 94487135..d9023999 100644 --- a/src/Generators/MigrationGenerator.php +++ b/src/Generators/MigrationGenerator.php @@ -303,25 +303,49 @@ protected function buildPivotTableDefinition(array $segments, array $models = [] $this->hasForeignKeyConstraints = true; $definition .= $this->buildForeignKey($foreign, $on, 'id') . ';' . PHP_EOL; } else { - $definition .= self::INDENT; - if (count($models) > 0 && array_key_exists($segment, $models)) { - $model = $models[$segment]; - if ($model->name() === $segment) { - $dataType = $model->columns()[$model->primaryKey()]->dataType(); - $definition .= $dataType === 'ulid' ? '$table->foreignUlid(\'' . $foreign . '\');' : - ($dataType === 'uuid' ? '$table->foreignUuid(\'' . $foreign . '\');' : - '$table->foreignId(\'' . $foreign . '\');'); - } - } else { - $definition .= '$table->foreignId(\'' . $foreign . '\');'; - } - $definition .= PHP_EOL; + $definition .= $this->generateForeignKeyDefinition($segment, $foreign, $models); } } return trim($definition); } + /** + * Generates the foreign key definition for a pivot table. + * + * This function generates the foreign key definition for a pivot table in a migration file. + * It checks if the model exists and its name matches the pivot table segment. If it does, + * it determines the data type of the primary key and appends the appropriate method call + * to the `$definition` string. If the model does not exist or its name does not match the + * pivot table segment, it defaults to appending `$table->foreignId(\'' . $foreignKeyColumnName . '\');` + * to the `$definition` string. The function then returns the `$definition` string. + * + * @param string $pivotTableSegment The segment of the pivot table. e.g 'dive_job' it would be 'Dive' or 'Job'. + * @param string $foreignKeyColumnName The name of the foreign key column. e.g 'dive_id' or 'job_id'. + * @param array $models An array of models. e.g ['Dive' => $diveModel, 'Job' => $jobModel]. + * @return string The foreign key definition. e.g '$table->foreignUlid('dive_id');' + */ + protected function generateForeignKeyDefinition(string $pivotTableSegment, string $foreignKeyColumnName, array $models = []): string + { + $definition = self::INDENT; + if (count($models) > 0 && array_key_exists($pivotTableSegment, $models)) { + $model = $models[$pivotTableSegment]; + if ($model->name() === $pivotTableSegment) { + $dataType = $model->columns()[$model->primaryKey()]->dataType(); + $definition .= match ($dataType) { + 'ulid' => '$table->foreignUlid(\'' . $foreignKeyColumnName . '\');', + 'uuid' => '$table->foreignUuid(\'' . $foreignKeyColumnName . '\');', + default => '$table->foreignId(\'' . $foreignKeyColumnName . '\');', + }; + } + } else { + $definition .= '$table->foreignId(\'' . $foreignKeyColumnName . '\');'; + } + $definition .= PHP_EOL; + + return $definition; + } + protected function buildPolyTableDefinition(string $parentTable): string { $definition = ''; From 6036e377c1d497f1f69f4c32a0edc7c991f4f165 Mon Sep 17 00:00:00 2001 From: rcrosbourne Date: Sun, 10 Dec 2023 15:33:30 -0500 Subject: [PATCH 27/30] refactor(MigrationGenerator): change method isIdUlidOrUuid to the more generic isIdColumnType --- src/Generators/MigrationGenerator.php | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/src/Generators/MigrationGenerator.php b/src/Generators/MigrationGenerator.php index d9023999..bf4cb6c1 100644 --- a/src/Generators/MigrationGenerator.php +++ b/src/Generators/MigrationGenerator.php @@ -194,7 +194,7 @@ protected function buildDefinition(Model $model): string ); } - if (!empty($columnAttributes) && !$this->isIdUlidOrUuid($column->dataType())) { + if (!empty($columnAttributes) && !$this->isIdColumnType($column->dataType())) { $column_definition .= ', '; if (in_array($column->dataType(), ['set', 'enum'])) { @@ -221,7 +221,7 @@ protected function buildDefinition(Model $model): string $column->modifiers() ); - if ($this->isIdUlidOrUuid($column->dataType())) { + if ($this->isIdColumnType($column->dataType())) { $column_definition = $foreign; $foreign = ''; } @@ -232,7 +232,7 @@ protected function buildDefinition(Model $model): string || (is_array($modifier) && key($modifier) === 'onDelete') || (is_array($modifier) && key($modifier) === 'onUpdate') || $modifier === 'foreign' - || ($modifier === 'nullable' && $this->isIdUlidOrUuid($column->dataType())) + || ($modifier === 'nullable' && $this->isIdColumnType($column->dataType())) ); } @@ -382,7 +382,7 @@ protected function buildForeignKey(string $column_name, ?string $on, string $typ $column = Str::afterLast($column_name, '_'); } - if ($this->isIdUlidOrUuid($type) && !empty($attributes)) { + if ($this->isIdColumnType($type) && !empty($attributes)) { $table = Str::lower(Str::plural($attributes[0])); } @@ -399,7 +399,7 @@ protected function buildForeignKey(string $column_name, ?string $on, string $typ $on_update_suffix = self::ON_UPDATE_CLAUSES[$on_update_clause]; } - if ($this->isIdUlidOrUuid($type)) { + if ($this->isIdColumnType($type)) { $method = match ($type) { 'ulid' => 'foreignUlid', 'uuid' => 'foreignUuid', @@ -503,7 +503,7 @@ private function shouldAddForeignKeyConstraint(\Blueprint\Models\Column $column) } return config('blueprint.use_constraints') - && ($this->isIdUlidOrUuid($column->dataType()) && Str::endsWith($column->name(), '_id')); + && ($this->isIdColumnType($column->dataType()) && Str::endsWith($column->name(), '_id')); } protected function isNumericDefault(string $type, string $value): bool @@ -520,7 +520,7 @@ protected function isNumericDefault(string $type, string $value): bool ->contains(fn ($value) => strtolower($value) === strtolower($type)); } - protected function isIdUlidOrUuid(string $dataType): bool + protected function isIdColumnType(string $dataType): bool { return in_array($dataType, ['id', 'ulid', 'uuid']); } From a2f911993157e58468ba1d563633c7ff36525298 Mon Sep 17 00:00:00 2001 From: rcrosbourne Date: Sun, 10 Dec 2023 15:34:23 -0500 Subject: [PATCH 28/30] refactor(lint): fix code format. --- src/Generators/ModelGenerator.php | 2 +- src/Lexers/ConfigLexer.php | 2 +- src/Models/Controller.php | 2 +- src/Models/Statements/SendStatement.php | 2 +- src/Tracer.php | 2 +- 5 files changed, 5 insertions(+), 5 deletions(-) diff --git a/src/Generators/ModelGenerator.php b/src/Generators/ModelGenerator.php index 745d16c4..7b9bddec 100644 --- a/src/Generators/ModelGenerator.php +++ b/src/Generators/ModelGenerator.php @@ -297,7 +297,7 @@ protected function addTraits(Model $model, $stub): string $this->addImport($model, 'Illuminate\\Database\\Eloquent\\Concerns\\HasUuids'); $traits[] = 'HasUuids'; } - + sort($traits); return Str::replaceFirst('use HasFactory', 'use ' . implode(', ', $traits), $stub); diff --git a/src/Lexers/ConfigLexer.php b/src/Lexers/ConfigLexer.php index d9b98700..1e5ec731 100644 --- a/src/Lexers/ConfigLexer.php +++ b/src/Lexers/ConfigLexer.php @@ -9,7 +9,7 @@ class ConfigLexer implements Lexer { private Container $app; - public function __construct(?Container $app = null) + public function __construct(Container $app = null) { $this->app = $app ?? Container::getInstance(); } diff --git a/src/Models/Controller.php b/src/Models/Controller.php index a1aa2e4d..67bda5a3 100644 --- a/src/Models/Controller.php +++ b/src/Models/Controller.php @@ -79,7 +79,7 @@ public function addMethod(string $name, array $statements): void $this->methods[$name] = $statements; } - public function policy(?Policy $policy = null): ?Policy + public function policy(Policy $policy = null): ?Policy { if ($policy) { $this->policy = $policy; diff --git a/src/Models/Statements/SendStatement.php b/src/Models/Statements/SendStatement.php index 89e114d7..bf11bc9a 100644 --- a/src/Models/Statements/SendStatement.php +++ b/src/Models/Statements/SendStatement.php @@ -24,7 +24,7 @@ class SendStatement private string $subject; - public function __construct(string $mail, ?string $to, array $data, string $type, ?string $view = null) + public function __construct(string $mail, ?string $to, array $data, string $type, string $view = null) { $this->mail = $mail; $this->data = $data; diff --git a/src/Tracer.php b/src/Tracer.php index 49207190..4c99be52 100644 --- a/src/Tracer.php +++ b/src/Tracer.php @@ -10,7 +10,7 @@ class Tracer { private Filesystem $filesystem; - public function execute(Blueprint $blueprint, Filesystem $filesystem, ?array $paths = null): array + public function execute(Blueprint $blueprint, Filesystem $filesystem, array $paths = null): array { $this->filesystem = $filesystem; From 3e8a73b70d881292e5969d77b4cb3a32c1dec72c Mon Sep 17 00:00:00 2001 From: rcrosbourne Date: Sun, 10 Dec 2023 15:37:05 -0500 Subject: [PATCH 29/30] refactor(fixtures): update the verbose `id: ulid primary` from fixtures to the shorthand `ulid` --- tests/fixtures/drafts/belongs-to-many-using-ulids.yaml | 4 ++-- tests/fixtures/drafts/model-with-ulid-id.yaml | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/tests/fixtures/drafts/belongs-to-many-using-ulids.yaml b/tests/fixtures/drafts/belongs-to-many-using-ulids.yaml index 64a84f73..7ec717f3 100644 --- a/tests/fixtures/drafts/belongs-to-many-using-ulids.yaml +++ b/tests/fixtures/drafts/belongs-to-many-using-ulids.yaml @@ -1,11 +1,11 @@ models: Journey: - id: ulid primary + ulid name: string user_id: ulid foreign relationships: belongsToMany: Diary Diary: - id: ulid primary + ulid relationships: belongsToMany: Journey diff --git a/tests/fixtures/drafts/model-with-ulid-id.yaml b/tests/fixtures/drafts/model-with-ulid-id.yaml index db022ffa..d0b8183b 100644 --- a/tests/fixtures/drafts/model-with-ulid-id.yaml +++ b/tests/fixtures/drafts/model-with-ulid-id.yaml @@ -1,5 +1,5 @@ models: User: - id: ulid primary + ulid name: string base_pay: decimal:10,2 From e9b35caf4df4d338927f9e10031597e6e3d99eca Mon Sep 17 00:00:00 2001 From: rcrosbourne Date: Sun, 10 Dec 2023 16:07:29 -0500 Subject: [PATCH 30/30] refactor(lint): undo the removal of the nullable type declaration for optional parameters. --- src/Lexers/ConfigLexer.php | 2 +- src/Models/Controller.php | 2 +- src/Models/Statements/SendStatement.php | 2 +- src/Tracer.php | 2 +- 4 files changed, 4 insertions(+), 4 deletions(-) diff --git a/src/Lexers/ConfigLexer.php b/src/Lexers/ConfigLexer.php index 1e5ec731..d9b98700 100644 --- a/src/Lexers/ConfigLexer.php +++ b/src/Lexers/ConfigLexer.php @@ -9,7 +9,7 @@ class ConfigLexer implements Lexer { private Container $app; - public function __construct(Container $app = null) + public function __construct(?Container $app = null) { $this->app = $app ?? Container::getInstance(); } diff --git a/src/Models/Controller.php b/src/Models/Controller.php index 67bda5a3..a1aa2e4d 100644 --- a/src/Models/Controller.php +++ b/src/Models/Controller.php @@ -79,7 +79,7 @@ public function addMethod(string $name, array $statements): void $this->methods[$name] = $statements; } - public function policy(Policy $policy = null): ?Policy + public function policy(?Policy $policy = null): ?Policy { if ($policy) { $this->policy = $policy; diff --git a/src/Models/Statements/SendStatement.php b/src/Models/Statements/SendStatement.php index bf11bc9a..89e114d7 100644 --- a/src/Models/Statements/SendStatement.php +++ b/src/Models/Statements/SendStatement.php @@ -24,7 +24,7 @@ class SendStatement private string $subject; - public function __construct(string $mail, ?string $to, array $data, string $type, string $view = null) + public function __construct(string $mail, ?string $to, array $data, string $type, ?string $view = null) { $this->mail = $mail; $this->data = $data; diff --git a/src/Tracer.php b/src/Tracer.php index 4c99be52..49207190 100644 --- a/src/Tracer.php +++ b/src/Tracer.php @@ -10,7 +10,7 @@ class Tracer { private Filesystem $filesystem; - public function execute(Blueprint $blueprint, Filesystem $filesystem, array $paths = null): array + public function execute(Blueprint $blueprint, Filesystem $filesystem, ?array $paths = null): array { $this->filesystem = $filesystem;