From 03ecfd072083b83fd21121b81845091338dcecf6 Mon Sep 17 00:00:00 2001 From: Watheq Alshowaiter Date: Mon, 15 Jul 2024 16:52:03 +0300 Subject: [PATCH] feat: support until version 6 --- ...2024_07_13_093048_create_mothers_table.php | 14 ++++++++-- .../2024_07_13_100247_create_sons_table.php | 26 +++++++++++++++---- src/Constants.php | 6 +++++ tests/RequiredFieldsTest.php | 4 +-- 4 files changed, 41 insertions(+), 9 deletions(-) diff --git a/database/migrations/2024_07_13_093048_create_mothers_table.php b/database/migrations/2024_07_13_093048_create_mothers_table.php index 5d2f0b1..e618bca 100644 --- a/database/migrations/2024_07_13_093048_create_mothers_table.php +++ b/database/migrations/2024_07_13_093048_create_mothers_table.php @@ -11,9 +11,19 @@ public function up(): void { Schema::create('mothers', function (Blueprint $table) { - $table->ulid('id')->primary(); // primary key => ignored + if ((float) App::version() >= Constants::VERSION_AFTER_ULID_SUPPORT) { + $table->ulid('id')->primary(); // primary key => ignored + } else { + $table->bigIncrements('id'); // primary key => ignored + } + $table->enum('types', ['one', 'two'])->default('one'); // default => ignored - $table->uuid('uuid'); // required + + if ((float) App::version() >= Constants::VERSION_AFTER_UUID_SUPPORT) { + $table->uuid('uuid'); // required + } else { + $table->string('uuid'); + } if ((float) App::version() >= Constants::VERSION_AFTER_ULID_SUPPORT) { $table->ulid('ulid'); // required } else { diff --git a/database/migrations/2024_07_13_100247_create_sons_table.php b/database/migrations/2024_07_13_100247_create_sons_table.php index 09ec4b2..0593c5a 100644 --- a/database/migrations/2024_07_13_100247_create_sons_table.php +++ b/database/migrations/2024_07_13_100247_create_sons_table.php @@ -11,19 +11,35 @@ public function up(): void { Schema::create('sons', function (Blueprint $table) { - $table->uuid('id')->primary(); // primary key => ignored - $table->foreignId('parent_id')->constrained(); // required + + if ((float) App::version() >= Constants::VERSION_AFTER_UUID_SUPPORT) { + $table->uuid('id')->primary(); // primary key => ignored + } else { + $table->bigIncrements('id'); // primary key => ignored + } + + if ((float) App::version() >= Constants::VERSION_AFTER_FOREIGN_ID_SUPPORT) { + $table->foreignId('father_id')->constrained(); // required + } else { + $table->unsignedBigInteger('father_id'); + $table->foreign('father_id')->references('id')->on('fathers'); // required + } + if ((float) App::version() >= Constants::VERSION_AFTER_ULID_SUPPORT) { $table->foreignUlid('mother_id')->nullable()->constrained(); // nullable => ignored } else { - $table->foreignId('mother_id')->nullable()->constrained(); // nullable => ignored + if ((float) App::version() >= Constants::VERSION_AFTER_FOREIGN_ID_SUPPORT) { + $table->foreignId('mother_id')->nullable()->constrained(); // nullable => ignored + } else { + $table->unsignedBigInteger('mother_id')->nullable(); + $table->foreign('mother_id')->references('id')->on('mothers'); + } } - $table->foreignId('father_id')->nullable()->constrained(); // nullable => ignored }); } public function down(): void { - Schema::dropIfExists('children'); + Schema::dropIfExists('sons'); } }; diff --git a/src/Constants.php b/src/Constants.php index 01a2e49..7342405 100644 --- a/src/Constants.php +++ b/src/Constants.php @@ -5,4 +5,10 @@ class Constants { public const VERSION_AFTER_ULID_SUPPORT = 9.40; + + public const VERSION_AFTER_FOREIGN_ID_SUPPORT = 7.0; + + public const VERSION_AFTER_UUID_SUPPORT = 7.0; + + public const VERSION_AFTER_ID_METHOD_SUPPORT = 7.0; } diff --git a/tests/RequiredFieldsTest.php b/tests/RequiredFieldsTest.php index 17197a5..9670645 100644 --- a/tests/RequiredFieldsTest.php +++ b/tests/RequiredFieldsTest.php @@ -52,11 +52,11 @@ public function test_get_required_fields_for_another_parent_model(): void public function test_get_required_fields_for_child_model(): void { $this->assertEquals([ - 'parent_id', + 'father_id', ], Son::getRequiredFields()); $this->assertEquals([ - 'parent_id', + 'father_id', ], Son::getRequiredFieldsForOlderVersions()); } }