Skip to content

Commit

Permalink
feat: support until version 6
Browse files Browse the repository at this point in the history
  • Loading branch information
WatheqAlshowaiter committed Jul 15, 2024
1 parent 415196e commit 03ecfd0
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 9 deletions.
14 changes: 12 additions & 2 deletions database/migrations/2024_07_13_093048_create_mothers_table.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
26 changes: 21 additions & 5 deletions database/migrations/2024_07_13_100247_create_sons_table.php
Original file line number Diff line number Diff line change
Expand Up @@ -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');
}
};
6 changes: 6 additions & 0 deletions src/Constants.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
4 changes: 2 additions & 2 deletions tests/RequiredFieldsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}
}

0 comments on commit 03ecfd0

Please sign in to comment.