fix(#157): migrations and seeders for pgsql

This commit is contained in:
Benjamin Nussbaum 2026-01-13 16:48:48 +01:00
parent 3032c09778
commit fceacfe4b3
2 changed files with 14 additions and 16 deletions

View file

@ -1,8 +1,8 @@
<?php <?php
use App\Models\Plugin;
use Illuminate\Database\Migrations\Migration; use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint; use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Schema; use Illuminate\Support\Facades\Schema;
return new class extends Migration return new class extends Migration
@ -13,16 +13,16 @@ return new class extends Migration
public function up(): void public function up(): void
{ {
// Find and handle duplicate (user_id, trmnlp_id) combinations // Find and handle duplicate (user_id, trmnlp_id) combinations
$duplicates = DB::table('plugins') $duplicates = Plugin::query()
->select('user_id', 'trmnlp_id', DB::raw('COUNT(*) as count')) ->selectRaw('user_id, trmnlp_id, COUNT(*) as duplicate_count')
->whereNotNull('trmnlp_id') ->whereNotNull('trmnlp_id')
->groupBy('user_id', 'trmnlp_id') ->groupBy('user_id', 'trmnlp_id')
->having('count', '>', 1) ->havingRaw('COUNT(*) > ?', [1])
->get(); ->get();
// For each duplicate combination, keep the first one (by id) and set others to null // For each duplicate combination, keep the first one (by id) and set others to null
foreach ($duplicates as $duplicate) { foreach ($duplicates as $duplicate) {
$plugins = DB::table('plugins') $plugins = Plugin::query()
->where('user_id', $duplicate->user_id) ->where('user_id', $duplicate->user_id)
->where('trmnlp_id', $duplicate->trmnlp_id) ->where('trmnlp_id', $duplicate->trmnlp_id)
->orderBy('id') ->orderBy('id')
@ -37,9 +37,7 @@ return new class extends Migration
continue; continue;
} }
DB::table('plugins') $plugin->update(['trmnlp_id' => null]);
->where('id', $plugin->id)
->update(['trmnlp_id' => null]);
} }
} }

View file

@ -13,8 +13,8 @@ class ExampleRecipesSeeder extends Seeder
public function run($user_id = 1): void public function run($user_id = 1): void
{ {
Plugin::updateOrCreate( Plugin::updateOrCreate(
['uuid' => '9e46c6cf-358c-4bfe-8998-436b3a207fec'],
[ [
'uuid' => '9e46c6cf-358c-4bfe-8998-436b3a207fec',
'name' => 'ÖBB Departures', 'name' => 'ÖBB Departures',
'user_id' => $user_id, 'user_id' => $user_id,
'data_payload' => null, 'data_payload' => null,
@ -32,8 +32,8 @@ class ExampleRecipesSeeder extends Seeder
); );
Plugin::updateOrCreate( Plugin::updateOrCreate(
['uuid' => '3b046eda-34e9-4232-b935-c33b989a284b'],
[ [
'uuid' => '3b046eda-34e9-4232-b935-c33b989a284b',
'name' => 'Weather', 'name' => 'Weather',
'user_id' => $user_id, 'user_id' => $user_id,
'data_payload' => null, 'data_payload' => null,
@ -51,8 +51,8 @@ class ExampleRecipesSeeder extends Seeder
); );
Plugin::updateOrCreate( Plugin::updateOrCreate(
['uuid' => '21464b16-5f5a-4099-a967-f5c915e3da54'],
[ [
'uuid' => '21464b16-5f5a-4099-a967-f5c915e3da54',
'name' => 'Zen Quotes', 'name' => 'Zen Quotes',
'user_id' => $user_id, 'user_id' => $user_id,
'data_payload' => null, 'data_payload' => null,
@ -70,8 +70,8 @@ class ExampleRecipesSeeder extends Seeder
); );
Plugin::updateOrCreate( Plugin::updateOrCreate(
['uuid' => '8d472959-400f-46ee-afb2-4a9f1cfd521f'],
[ [
'uuid' => '8d472959-400f-46ee-afb2-4a9f1cfd521f',
'name' => 'This Day in History', 'name' => 'This Day in History',
'user_id' => $user_id, 'user_id' => $user_id,
'data_payload' => null, 'data_payload' => null,
@ -89,8 +89,8 @@ class ExampleRecipesSeeder extends Seeder
); );
Plugin::updateOrCreate( Plugin::updateOrCreate(
['uuid' => '4349fdad-a273-450b-aa00-3d32f2de788d'],
[ [
'uuid' => '4349fdad-a273-450b-aa00-3d32f2de788d',
'name' => 'Home Assistant', 'name' => 'Home Assistant',
'user_id' => $user_id, 'user_id' => $user_id,
'data_payload' => null, 'data_payload' => null,
@ -108,8 +108,8 @@ class ExampleRecipesSeeder extends Seeder
); );
Plugin::updateOrCreate( Plugin::updateOrCreate(
['uuid' => 'be5f7e1f-3ad8-4d66-93b2-36f7d6dcbd80'],
[ [
'uuid' => 'be5f7e1f-3ad8-4d66-93b2-36f7d6dcbd80',
'name' => 'Sunrise/Sunset', 'name' => 'Sunrise/Sunset',
'user_id' => $user_id, 'user_id' => $user_id,
'data_payload' => null, 'data_payload' => null,
@ -127,8 +127,8 @@ class ExampleRecipesSeeder extends Seeder
); );
Plugin::updateOrCreate( Plugin::updateOrCreate(
['uuid' => '82d3ee14-d578-4969-bda5-2bbf825435fe'],
[ [
'uuid' => '82d3ee14-d578-4969-bda5-2bbf825435fe',
'name' => 'Pollen Forecast', 'name' => 'Pollen Forecast',
'user_id' => $user_id, 'user_id' => $user_id,
'data_payload' => null, 'data_payload' => null,
@ -146,8 +146,8 @@ class ExampleRecipesSeeder extends Seeder
); );
Plugin::updateOrCreate( Plugin::updateOrCreate(
['uuid' => '1d98bca4-837d-4b01-b1a1-e3b6e56eca90'],
[ [
'uuid' => '1d98bca4-837d-4b01-b1a1-e3b6e56eca90',
'name' => 'Holidays (iCal)', 'name' => 'Holidays (iCal)',
'user_id' => $user_id, 'user_id' => $user_id,
'data_payload' => null, 'data_payload' => null,