-
Notifications
You must be signed in to change notification settings - Fork 77
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Set up basic build group Eloquent relationships (#2394)
This PR is incremental progress towards an entirely Eloquent-based query system, and ultimately an introduction of build groups to the GraphQL API. In addition to setting up Eloquent relationships between build group-related models, this PR also adds additional constraints and indexes for better performance and data integrity.
- Loading branch information
1 parent
0439c98
commit ddf49ce
Showing
7 changed files
with
173 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
<?php | ||
|
||
namespace App\Models; | ||
|
||
use Illuminate\Database\Eloquent\Relations\BelongsTo; | ||
use Illuminate\Support\Carbon; | ||
use Illuminate\Database\Eloquent\Builder; | ||
use Illuminate\Database\Eloquent\Model; | ||
|
||
/** | ||
* @property int $id | ||
* @property int $buildgroupid | ||
* @property int $position | ||
* @property Carbon $starttime | ||
* @property Carbon $endtime | ||
* | ||
* @mixin Builder<BuildGroupPosition> | ||
*/ | ||
class BuildGroupPosition extends Model | ||
{ | ||
protected $table = 'buildgroup'; | ||
|
||
public $timestamps = false; | ||
|
||
protected $fillable = [ | ||
'position', | ||
'starttime', | ||
'endtime', | ||
]; | ||
|
||
protected $casts = [ | ||
'id' => 'integer', | ||
'buildgroupid' => 'integer', | ||
'position' => 'integer', | ||
'starttime' => 'datetime', | ||
'endtime' => 'datetime', | ||
]; | ||
|
||
/** | ||
* @return BelongsTo<BuildGroup,self> | ||
*/ | ||
public function buildGroup(): BelongsTo | ||
{ | ||
return $this->belongsTo(BuildGroup::class, 'buildgroupid'); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
41 changes: 41 additions & 0 deletions
41
database/migrations/2024_08_27_173953_build2group_foreign_key_constraints.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
<?php | ||
|
||
use Illuminate\Database\Migrations\Migration; | ||
use Illuminate\Database\Schema\Blueprint; | ||
use Illuminate\Support\Facades\DB; | ||
use Illuminate\Support\Facades\Schema; | ||
|
||
return new class extends Migration { | ||
public function up(): void | ||
{ | ||
$num_deleted = DB::delete(' | ||
DELETE FROM build2group | ||
WHERE groupid NOT IN ( | ||
SELECT id FROM buildgroup | ||
) | ||
'); | ||
|
||
if ($num_deleted > 0) { | ||
echo "Deleted $num_deleted invalid build2group rows."; | ||
} | ||
|
||
Schema::table('build2group', function (Blueprint $table) { | ||
$table->dropForeign(['buildid']); // Drop the foreign key so we can drop the primary key (MySQL is particular about this) | ||
$table->dropPrimary(); | ||
$table->foreign('buildid')->references('id')->on('build')->cascadeOnDelete(); | ||
$table->unique(['buildid', 'groupid']); | ||
$table->unique(['groupid', 'buildid']); | ||
$table->foreign('groupid')->references('id')->on('buildgroup')->cascadeOnDelete(); | ||
}); | ||
} | ||
|
||
public function down(): void | ||
{ | ||
Schema::table('build2group', function (Blueprint $table) { | ||
$table->primary(['buildid']); | ||
$table->dropUnique(['buildid', 'groupid']); | ||
$table->dropUnique(['groupid', 'buildid']); | ||
$table->dropForeign(['groupid']); | ||
}); | ||
} | ||
}; |
39 changes: 39 additions & 0 deletions
39
database/migrations/2024_08_27_191729_buildgroupposition_relationships.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
<?php | ||
|
||
use Illuminate\Database\Migrations\Migration; | ||
use Illuminate\Database\Schema\Blueprint; | ||
use Illuminate\Support\Facades\DB; | ||
use Illuminate\Support\Facades\Schema; | ||
|
||
return new class extends Migration { | ||
public function up(): void | ||
{ | ||
$num_deleted = DB::delete(' | ||
DELETE FROM buildgroupposition | ||
WHERE buildgroupid NOT IN ( | ||
SELECT id | ||
FROM buildgroup | ||
) | ||
'); | ||
|
||
if ($num_deleted > 0) { | ||
echo "Deleted $num_deleted invalid buildgroupposition rows."; | ||
} | ||
|
||
Schema::table('buildgroupposition', function (Blueprint $table) { | ||
$table->increments('id'); | ||
$table->foreign('buildgroupid') | ||
->references('id') | ||
->on('buildgroup') | ||
->cascadeOnDelete(); | ||
}); | ||
} | ||
|
||
public function down(): void | ||
{ | ||
Schema::table('buildgroupposition', function (Blueprint $table) { | ||
$table->dropColumn('id'); | ||
$table->dropForeign(['buildgroupid']); | ||
}); | ||
} | ||
}; |