Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,13 @@ public function up(): void
{
Schema::create('component_checks', function (Blueprint $table) {
$table->id();
$table->unsignedInteger('component_id');

if ($this->componentsTableUsesBigIntegerIds()) {
$table->unsignedBigInteger('component_id');
} else {
$table->unsignedInteger('component_id');
}

$table->unsignedTinyInteger('status');
$table->boolean('successful')->default(false);
$table->unsignedSmallInteger('response_code')->nullable();
Expand All @@ -32,4 +38,17 @@ public function down(): void
{
Schema::dropIfExists('component_checks');
}

/**
* Whether the components table keys its rows by big integers.
*
* components.id is an unsigned integer on fresh installations and on
* upgrades from 2.x, but a big integer on databases created by early 3.x
* builds. MySQL refuses a foreign key whose column type does not match
* the column it references, so component_id must mirror the parent.
*/
private function componentsTableUsesBigIntegerIds(): bool
{
return in_array(Schema::getColumnType('components', 'id'), ['bigint', 'int8'], true);
}
};
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,13 @@ public function up(): void
{
Schema::create('component_status_changes', function (Blueprint $table) {
$table->id();
$table->unsignedInteger('component_id');

if ($this->componentsTableUsesBigIntegerIds()) {
$table->unsignedBigInteger('component_id');
} else {
$table->unsignedInteger('component_id');
}

$table->unsignedTinyInteger('old_status')->nullable();
$table->unsignedTinyInteger('new_status');
$table->string('source');
Expand All @@ -33,4 +39,17 @@ public function down(): void
{
Schema::dropIfExists('component_status_changes');
}

/**
* Whether the components table keys its rows by big integers.
*
* components.id is an unsigned integer on fresh installations and on
* upgrades from 2.x, but a big integer on databases created by early 3.x
* builds. MySQL refuses a foreign key whose column type does not match
* the column it references, so component_id must mirror the parent.
*/
private function componentsTableUsesBigIntegerIds(): bool
{
return in_array(Schema::getColumnType('components', 'id'), ['bigint', 'int8'], true);
}
};
20 changes: 20 additions & 0 deletions tests/Unit/Database/ComponentStatusChangesTableTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

use Illuminate\Support\Facades\Schema;

it('creates component_id with the same column type as components.id', function () {
$componentsId = collect(Schema::getColumns('components'))->firstWhere('name', 'id');
$componentId = collect(Schema::getColumns('component_status_changes'))->firstWhere('name', 'component_id');

expect($componentId['type'])->toBe($componentsId['type']);
});

it('constrains component_id to components.id with cascading deletes', function () {
$foreignKey = collect(Schema::getForeignKeys('component_status_changes'))
->first(fn (array $key) => $key['columns'] === ['component_id']);

expect($foreignKey)->not->toBeNull()
->and($foreignKey['foreign_table'])->toBe('components')
->and($foreignKey['foreign_columns'])->toBe(['id'])
->and(strtolower($foreignKey['on_delete']))->toBe('cascade');
});
Loading