Skip to content

Commit 8f04d6d

Browse files
authored
Merge pull request #49 from iFixit/fix--group-migrations
Fix Migrations: Don't Use Group Eloquent Model
2 parents 0371e37 + 952d25a commit 8f04d6d

1 file changed

Lines changed: 18 additions & 9 deletions

File tree

database/migrations/2024_09_16_093650_inactive_groups.php

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
use Illuminate\Database\Migrations\Migration;
44
use Illuminate\Database\Schema\Blueprint;
5+
use Illuminate\Support\Facades\DB;
56
use Illuminate\Support\Facades\Schema;
67

78
return new class extends Migration
@@ -11,18 +12,25 @@
1112
*/
1213
public function up(): void
1314
{
14-
$groups = \App\Models\Group::join('grouptags_groups', 'groups.idgroups', '=', 'grouptags_groups.group')
15+
// Use query builder instead of Eloquent model to avoid SoftDeletes
16+
// scope referencing a deleted_at column that doesn't exist yet.
17+
$groups = DB::table('groups')
18+
->join('grouptags_groups', 'groups.idgroups', '=', 'grouptags_groups.group')
1519
->join('group_tags', 'grouptags_groups.group_tag', '=', 'group_tags.id')
1620
->where('group_tags.id', \App\Models\GroupTags::INACTIVE)
21+
->select('groups.*')
1722
->get();
1823

1924
foreach ($groups as $group) {
20-
$group->archived_at = $group->updated_at;
25+
$name = str_replace('[INACTIVE] ', '', $group->name);
26+
$name = str_replace('[INACTIVE]', '', $name);
2127

22-
// Remove [INACTIVE] from the group name - this is now indicated via archived_at.
23-
$group->name = str_replace('[INACTIVE] ', '', $group->name);
24-
$group->name = str_replace('[INACTIVE]', '', $group->name);
25-
$group->save();
28+
DB::table('groups')
29+
->where('idgroups', $group->idgroups)
30+
->update([
31+
'archived_at' => $group->updated_at,
32+
'name' => $name,
33+
]);
2634
}
2735
}
2836

@@ -32,11 +40,12 @@ public function up(): void
3240
public function down(): void
3341
{
3442
// Add [INACTIVE] into all groups with archived_at.
35-
$groups = \App\Models\Group::whereNotNull('archived_at')->get();
43+
$groups = DB::table('groups')->whereNotNull('archived_at')->get();
3644

3745
foreach ($groups as $group) {
38-
$group->name = '[INACTIVE] ' . $group->name;
39-
$group->save();
46+
DB::table('groups')
47+
->where('idgroups', $group->idgroups)
48+
->update(['name' => '[INACTIVE] ' . $group->name]);
4049
}
4150
}
4251
};

0 commit comments

Comments
 (0)