From 5764578f619de770c49d232dbcf70a67251a949a Mon Sep 17 00:00:00 2001 From: Jyrki De Neve Date: Wed, 22 Sep 2021 16:45:14 +0200 Subject: [PATCH 1/5] If on is a class, we will try to fetch the table from that class --- src/Generators/MigrationGenerator.php | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/Generators/MigrationGenerator.php b/src/Generators/MigrationGenerator.php index a3a8dfc7..04042266 100644 --- a/src/Generators/MigrationGenerator.php +++ b/src/Generators/MigrationGenerator.php @@ -300,6 +300,9 @@ protected function buildForeignKey(string $column_name, ?string $on, string $typ } elseif (Str::contains($on, '.')) { [$table, $column] = explode('.', $on); $table = Str::snake($table); + } elseif (Str::contains($on, '\\') && class_exists($on)) { + $table = (new $on())->getTable(); + $column = Str::afterLast($column_name, '_'); } else { $table = Str::plural($on); $column = Str::afterLast($column_name, '_'); From 31206ed660e25ce4d711e0b82ef17098ddbb9e8e Mon Sep 17 00:00:00 2001 From: Jyrki De Neve Date: Wed, 22 Sep 2021 17:01:26 +0200 Subject: [PATCH 2/5] Write test for new usecase --- src/Generators/MigrationGenerator.php | 2 +- .../Generators/MigrationGeneratorTest.php | 1 + tests/Models/Attachment.php | 8 ++++ tests/fixtures/drafts/foreign-with-class.yaml | 4 ++ .../migrations/foreign-with-class.php | 38 +++++++++++++++++++ 5 files changed, 52 insertions(+), 1 deletion(-) create mode 100644 tests/Models/Attachment.php create mode 100644 tests/fixtures/drafts/foreign-with-class.yaml create mode 100644 tests/fixtures/migrations/foreign-with-class.php diff --git a/src/Generators/MigrationGenerator.php b/src/Generators/MigrationGenerator.php index 04042266..a99031ac 100644 --- a/src/Generators/MigrationGenerator.php +++ b/src/Generators/MigrationGenerator.php @@ -300,7 +300,7 @@ protected function buildForeignKey(string $column_name, ?string $on, string $typ } elseif (Str::contains($on, '.')) { [$table, $column] = explode('.', $on); $table = Str::snake($table); - } elseif (Str::contains($on, '\\') && class_exists($on)) { + } elseif (Str::contains($on, '\\')) { $table = (new $on())->getTable(); $column = Str::afterLast($column_name, '_'); } else { diff --git a/tests/Feature/Generators/MigrationGeneratorTest.php b/tests/Feature/Generators/MigrationGeneratorTest.php index aeb66e48..b1bf9d56 100644 --- a/tests/Feature/Generators/MigrationGeneratorTest.php +++ b/tests/Feature/Generators/MigrationGeneratorTest.php @@ -604,6 +604,7 @@ public function modelTreeDataProvider() ['drafts/enum-options.yaml', 'database/migrations/timestamp_create_messages_table.php', 'migrations/enum-options.php'], ['drafts/columns-with-comments.yaml', 'database/migrations/timestamp_create_professions_table.php', 'migrations/columns-with-comments.php'], ['drafts/boolean-column-default.yaml', 'database/migrations/timestamp_create_posts_table.php', 'migrations/boolean-column-default.php'], + ['drafts/foreign-with-class.yaml', 'database/migrations/timestamp_create_events_table.php', 'migrations/foreign-with-class.php'], ]; } } diff --git a/tests/Models/Attachment.php b/tests/Models/Attachment.php new file mode 100644 index 00000000..6c99c73a --- /dev/null +++ b/tests/Models/Attachment.php @@ -0,0 +1,8 @@ +id(); + $table->string('working_title'); + $table->uuid('image_id')->nullable(); + $table->foreign('image_id')->references('id')->on('attachments'); + $table->timestamps(); + }); + + Schema::enableForeignKeyConstraints(); + } + + /** + * Reverse the migrations. + * + * @return void + */ + public function down() + { + Schema::dropIfExists('events'); + } +} From 4ec21b270be54fd176e1a7dc3322bb79d2996f8b Mon Sep 17 00:00:00 2001 From: Jyrki De Neve Date: Wed, 22 Sep 2021 17:03:33 +0200 Subject: [PATCH 3/5] Fix style issue in test model --- tests/Models/Attachment.php | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/tests/Models/Attachment.php b/tests/Models/Attachment.php index 6c99c73a..3ff2bebb 100644 --- a/tests/Models/Attachment.php +++ b/tests/Models/Attachment.php @@ -5,4 +5,5 @@ use Illuminate\Database\Eloquent\Model; class Attachment extends Model -{} +{ +} From 88e1f07902d552e562dcb69a68b8e4b89a109f26 Mon Sep 17 00:00:00 2001 From: Jyrki De Neve Date: Thu, 23 Sep 2021 09:53:24 +0200 Subject: [PATCH 4/5] Fix tests after changes in master branch. --- tests/fixtures/migrations/foreign-with-class.php | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/tests/fixtures/migrations/foreign-with-class.php b/tests/fixtures/migrations/foreign-with-class.php index b66d1664..039dbd68 100644 --- a/tests/fixtures/migrations/foreign-with-class.php +++ b/tests/fixtures/migrations/foreign-with-class.php @@ -18,8 +18,7 @@ public function up() Schema::create('events', function (Blueprint $table) { $table->id(); $table->string('working_title'); - $table->uuid('image_id')->nullable(); - $table->foreign('image_id')->references('id')->on('attachments'); + $table->foreignUuid('image_id')->nullable()->constrained('attachments'); $table->timestamps(); }); From ecce99043c96df27b75f776fccc18da6a83e03f6 Mon Sep 17 00:00:00 2001 From: Jyrki De Neve Date: Thu, 23 Sep 2021 15:45:10 +0200 Subject: [PATCH 5/5] Do not use model to fetch table, but table is same as classname for on variable --- src/Generators/MigrationGenerator.php | 2 +- tests/Models/Attachment.php | 9 --------- 2 files changed, 1 insertion(+), 10 deletions(-) delete mode 100644 tests/Models/Attachment.php diff --git a/src/Generators/MigrationGenerator.php b/src/Generators/MigrationGenerator.php index fa0c34f9..c281e2eb 100644 --- a/src/Generators/MigrationGenerator.php +++ b/src/Generators/MigrationGenerator.php @@ -295,7 +295,7 @@ protected function buildForeignKey(string $column_name, ?string $on, string $typ [$table, $column] = explode('.', $on); $table = Str::snake($table); } elseif (Str::contains($on, '\\')) { - $table = (new $on())->getTable(); + $table = Str::lower(Str::plural(Str::afterLast($on, '\\'))); $column = Str::afterLast($column_name, '_'); } else { $table = Str::plural($on); diff --git a/tests/Models/Attachment.php b/tests/Models/Attachment.php deleted file mode 100644 index 3ff2bebb..00000000 --- a/tests/Models/Attachment.php +++ /dev/null @@ -1,9 +0,0 @@ -