Repository: ucan-lab/laravel-dacapo (php)

Issue:
Change schema format: foreign_keys.*.on to foreign_keys.*.table
- OLD: foreign_keys.*.on
- NEW: foreign_keys.*.table


Pull request:
Change schema format: foreign_keys.*.on to foreign_keys.*.table
close #146

Patch:
diff --git a/src/Dacapo/Domain/Schema/ForeignKey/Reference.php b/src/Dacapo/Domain/Schema/ForeignKey/Reference.php
index 71689c2..b944158 100644
--- a/src/Dacapo/Domain/Schema/ForeignKey/Reference.php
+++ b/src/Dacapo/Domain/Schema/ForeignKey/Reference.php
@@ -9,25 +9,25 @@ final class Reference
 {
     private $columns;
     private $references;
-    private string $on;
+    private string $table;
     private ?string $name;
 
     /**
      * Reference constructor.
      * @param string|array $columns
      * @param string|array $references
-     * @param string $on
+     * @param string $table
      * @param string|null $name
      */
     private function __construct(
         $columns,
         $references,
-        string $on,
+        string $table,
         ?string $name
     ) {
         $this->columns = $columns;
         $this->references = $references;
-        $this->on = $on;
+        $this->table = $table;
         $this->name = $name;
     }
 
@@ -45,11 +45,16 @@ final class Reference
             throw new InvalidArgumentException('foreign_keys.references field is required');
         }
 
-        if (isset($attributes['on']) === false) {
-            throw new InvalidArgumentException('foreign_keys.on field is required');
+        if (isset($attributes['table'])) {
+            $table = $attributes['table'];
+        } elseif (isset($attributes['on'])) {
+            $table = $attributes['on']; // @deprecated
+            echo 'Deprecated: foreign_keys.*.on to foreign_keys.*.table' . PHP_EOL;
+        } else {
+            throw new InvalidArgumentException('foreign_keys.table field is required');
         }
 
-        return new self($attributes['columns'], $attributes['references'], $attributes['on'], $attributes['name'] ?? null);
+        return new self($attributes['columns'], $attributes['references'], $table, $attributes['name'] ?? null);
     }
 
     /**
@@ -58,10 +63,10 @@ final class Reference
     public function makeForeignMigration(): string
     {
         if ($this->name) {
-            return sprintf("->foreign(%s, '%s')->references(%s)->on('%s')", $this->makeColumnsMigration(), $this->name, $this->makeReferencesMigration(), $this->on);
+            return sprintf("->foreign(%s, '%s')->references(%s)->on('%s')", $this->makeColumnsMigration(), $this->name, $this->makeReferencesMigration(), $this->table);
         }
 
-        return sprintf("->foreign(%s)->references(%s)->on('%s')", $this->makeColumnsMigration(), $this->makeReferencesMigration(), $this->on);
+        return sprintf("->foreign(%s)->references(%s)->on('%s')", $this->makeColumnsMigration(), $this->makeReferencesMigration(), $this->table);
     }
 
     /**


Test patch:
diff --git a/tests/Application/UseCase/DacapoCommandTest/mysql/foreign_keys/migrations/1970_01_01_000001_create_child_table.php b/tests/Application/UseCase/DacapoCommandTest/mysql/foreign_keys/migrations/1970_01_01_000001_create_child_table.php
new file mode 100644
index 0000000..8bbac00
--- /dev/null
+++ b/tests/Application/UseCase/DacapoCommandTest/mysql/foreign_keys/migrations/1970_01_01_000001_create_child_table.php
@@ -0,0 +1,31 @@
+<?php
+
+use Illuminate\Database\Migrations\Migration;
+use Illuminate\Database\Schema\Blueprint;
+use Illuminate\Support\Facades\Schema;
+
+class CreateChildTable extends Migration
+{
+    /**
+     * Run the migrations.
+     *
+     * @return void
+     */
+    public function up()
+    {
+        Schema::create('child', function (Blueprint $table) {
+            $table->increments('id');
+            $table->unsignedInteger('parent_id');
+        });
+    }
+
+    /**
+     * Reverse the migrations.
+     *
+     * @return void
+     */
+    public function down()
+    {
+        Schema::dropIfExists('child');
+    }
+}
diff --git a/tests/Application/UseCase/DacapoCommandTest/mysql/foreign_keys/migrations/1970_01_01_000001_create_parent_table.php b/tests/Application/UseCase/DacapoCommandTest/mysql/foreign_keys/migrations/1970_01_01_000001_create_parent_table.php
new file mode 100644
index 0000000..76c1d6c
--- /dev/null
+++ b/tests/Application/UseCase/DacapoCommandTest/mysql/foreign_keys/migrations/1970_01_01_000001_create_parent_table.php
@@ -0,0 +1,30 @@
+<?php
+
+use Illuminate\Database\Migrations\Migration;
+use Illuminate\Database\Schema\Blueprint;
+use Illuminate\Support\Facades\Schema;
+
+class CreateParentTable extends Migration
+{
+    /**
+     * Run the migrations.
+     *
+     * @return void
+     */
+    public function up()
+    {
+        Schema::create('parent', function (Blueprint $table) {
+            $table->increments('id');
+        });
+    }
+
+    /**
+     * Reverse the migrations.
+     *
+     * @return void
+     */
+    public function down()
+    {
+        Schema::dropIfExists('parent');
+    }
+}
diff --git a/tests/Application/UseCase/DacapoCommandTest/mysql/foreign_keys/migrations/1970_01_01_000003_constraint_child_foreign_key.php b/tests/Application/UseCase/DacapoCommandTest/mysql/foreign_keys/migrations/1970_01_01_000003_constraint_child_foreign_key.php
new file mode 100644
index 0000000..d6779f6
--- /dev/null
+++ b/tests/Application/UseCase/DacapoCommandTest/mysql/foreign_keys/migrations/1970_01_01_000003_constraint_child_foreign_key.php
@@ -0,0 +1,32 @@
+<?php
+
+use Illuminate\Database\Migrations\Migration;
+use Illuminate\Database\Schema\Blueprint;
+use Illuminate\Support\Facades\Schema;
+
+class ConstraintChildForeignKey extends Migration
+{
+    /**
+     * Run the migrations.
+     *
+     * @return void
+     */
+    public function up()
+    {
+        Schema::table('child', function (Blueprint $table) {
+            $table->foreign('parent_id')->references('id')->on('parent');
+        });
+    }
+
+    /**
+     * Reverse the migrations.
+     *
+     * @return void
+     */
+    public function down()
+    {
+        Schema::table('child', function (Blueprint $table) {
+            $table->dropForeign(['parent_id']);
+        });
+    }
+}
diff --git a/tests/Application/UseCase/DacapoCommandTest/mysql/foreign_keys/schemas/schema.yml b/tests/Application/UseCase/DacapoCommandTest/mysql/foreign_keys/schemas/schema.yml
new file mode 100644
index 0000000..370d5b7
--- /dev/null
+++ b/tests/Application/UseCase/DacapoCommandTest/mysql/foreign_keys/schemas/schema.yml
@@ -0,0 +1,13 @@
+# Foreign keys
+parent:
+  columns:
+    id: increments
+
+child:
+  columns:
+    id: increments
+    parent_id: unsignedInteger
+  foreign_keys:
+    - columns: parent_id
+      references: id
+      table: parent
diff --git a/tests/Application/UseCase/DacapoCommandTest/mysql/foreign_keys_deprecated/migrations/1970_01_01_000001_create_child_table.php b/tests/Application/UseCase/DacapoCommandTest/mysql/foreign_keys_deprecated/migrations/1970_01_01_000001_create_child_table.php
new file mode 100644
index 0000000..8bbac00
--- /dev/null
+++ b/tests/Application/UseCase/DacapoCommandTest/mysql/foreign_keys_deprecated/migrations/1970_01_01_000001_create_child_table.php
@@ -0,0 +1,31 @@
+<?php
+
+use Illuminate\Database\Migrations\Migration;
+use Illuminate\Database\Schema\Blueprint;
+use Illuminate\Support\Facades\Schema;
+
+class CreateChildTable extends Migration
+{
+    /**
+     * Run the migrations.
+     *
+     * @return void
+     */
+    public function up()
+    {
+        Schema::create('child', function (Blueprint $table) {
+            $table->increments('id');
+            $table->unsignedInteger('parent_id');
+        });
+    }
+
+    /**
+     * Reverse the migrations.
+     *
+     * @return void
+     */
+    public function down()
+    {
+        Schema::dropIfExists('child');
+    }
+}
diff --git a/tests/Application/UseCase/DacapoCommandTest/mysql/foreign_keys_deprecated/migrations/1970_01_01_000001_create_parent_table.php b/tests/Application/UseCase/DacapoCommandTest/mysql/foreign_keys_deprecated/migrations/1970_01_01_000001_create_parent_table.php
new file mode 100644
index 0000000..76c1d6c
--- /dev/null
+++ b/tests/Application/UseCase/DacapoCommandTest/mysql/foreign_keys_deprecated/migrations/1970_01_01_000001_create_parent_table.php
@@ -0,0 +1,30 @@
+<?php
+
+use Illuminate\Database\Migrations\Migration;
+use Illuminate\Database\Schema\Blueprint;
+use Illuminate\Support\Facades\Schema;
+
+class CreateParentTable extends Migration
+{
+    /**
+     * Run the migrations.
+     *
+     * @return void
+     */
+    public function up()
+    {
+        Schema::create('parent', function (Blueprint $table) {
+            $table->increments('id');
+        });
+    }
+
+    /**
+     * Reverse the migrations.
+     *
+     * @return void
+     */
+    public function down()
+    {
+        Schema::dropIfExists('parent');
+    }
+}
diff --git a/tests/Application/UseCase/DacapoCommandTest/mysql/foreign_keys_deprecated/migrations/1970_01_01_000003_constraint_child_foreign_key.php b/tests/Application/UseCase/DacapoCommandTest/mysql/foreign_keys_deprecated/migrations/1970_01_01_000003_constraint_child_foreign_key.php
new file mode 100644
index 0000000..d6779f6
--- /dev/null
+++ b/tests/Application/UseCase/DacapoCommandTest/mysql/foreign_keys_deprecated/migrations/1970_01_01_000003_constraint_child_foreign_key.php
@@ -0,0 +1,32 @@
+<?php
+
+use Illuminate\Database\Migrations\Migration;
+use Illuminate\Database\Schema\Blueprint;
+use Illuminate\Support\Facades\Schema;
+
+class ConstraintChildForeignKey extends Migration
+{
+    /**
+     * Run the migrations.
+     *
+     * @return void
+     */
+    public function up()
+    {
+        Schema::table('child', function (Blueprint $table) {
+            $table->foreign('parent_id')->references('id')->on('parent');
+        });
+    }
+
+    /**
+     * Reverse the migrations.
+     *
+     * @return void
+     */
+    public function down()
+    {
+        Schema::table('child', function (Blueprint $table) {
+            $table->dropForeign(['parent_id']);
+        });
+    }
+}
diff --git a/tests/Application/UseCase/DacapoCommandTest/mysql/foreign_keys_deprecated/schemas/schema.yml b/tests/Application/UseCase/DacapoCommandTest/mysql/foreign_keys_deprecated/schemas/schema.yml
new file mode 100644
index 0000000..a4a83c0
--- /dev/null
+++ b/tests/Application/UseCase/DacapoCommandTest/mysql/foreign_keys_deprecated/schemas/schema.yml
@@ -0,0 +1,13 @@
+# Foreign keys
+parent:
+  columns:
+    id: increments
+
+child:
+  columns:
+    id: increments
+    parent_id: unsignedInteger
+  foreign_keys:
+    - columns: parent_id
+      references: id
+      on: parent