Skip to content

Commit

Permalink
feat: ensure migrations are compliant with pgsql (#655)
Browse files Browse the repository at this point in the history
  • Loading branch information
warlof committed Aug 20, 2023
1 parent d9c3fd5 commit 47240bd
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 15 deletions.
33 changes: 24 additions & 9 deletions src/database/migrations/2019_11_12_220840_drop_groups_table.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
/*
* This file is part of SeAT
*
* Copyright (C) 2015 to 2022 Leon Jacobs
* Copyright (C) 2015 to present Leon Jacobs
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
Expand Down Expand Up @@ -37,6 +37,8 @@ class DropGroupsTable extends Migration
*/
public function up()
{
$engine = DB::getDriverName();

// drop admin account - if exists
$entry = DB::table('users')
->where('name', 'admin')
Expand Down Expand Up @@ -67,12 +69,25 @@ public function up()
->whereNotIn('group_id', DB::table('groups')->select('id'))
->delete();

// remove duplicate entries using group_id and name as pivot
DB::statement('DELETE a FROM user_settings a INNER JOIN user_settings b WHERE a.id > b.id AND a.group_id = b.group_id AND a.name = b.name');

// remove duplicate main character setting using name and value as pivot
DB::statement('DELETE a FROM user_settings a INNER JOIN user_settings b WHERE a.id < b.id AND a.name = b.name AND a.name = "main_character_id" AND a.value = b.value');
DB::statement('DELETE a FROM user_settings a INNER JOIN user_settings b WHERE a.id < b.id AND a.name = b.name AND a.name = "main_character_name" AND a.value = b.value');
switch ($engine) {
case 'mysql':
// remove duplicate entries using group_id and name as pivot
DB::statement('DELETE a FROM user_settings a INNER JOIN user_settings b WHERE a.id > b.id AND a.group_id = b.group_id AND a.name = b.name');

// remove duplicate main character setting using name and value as pivot
DB::statement('DELETE a FROM user_settings a INNER JOIN user_settings b WHERE a.id < b.id AND a.name = b.name AND a.name = ? AND a.value = b.value', ['main_character_id']);
DB::statement('DELETE a FROM user_settings a INNER JOIN user_settings b WHERE a.id < b.id AND a.name = b.name AND a.name = ? AND a.value = b.value', ['main_character_name']);
break;
case 'pgsql':
case 'postgresql':
// remove duplicate entries using group_id and name as pivot
DB::statement('DELETE FROM user_settings a USING user_settings b WHERE a.id > b.id AND a.group_id = b.group_id AND a.name = b.name');

// remove duplicate main character setting using name and value as pivot
DB::statement('DELETE FROM user_settings a USING user_settings b WHERE a.id < b.id AND a.name = b.name AND a.name = ? AND a.value = b.value', ['main_character_id']);
DB::statement('DELETE FROM user_settings a USING user_settings b WHERE a.id < b.id AND a.name = b.name AND a.name = ? AND a.value = b.value', ['main_character_name']);
break;
}

///
/// Pre-check regarding users table structure and associated main_character
Expand Down Expand Up @@ -250,7 +265,7 @@ public function up()
$join->where('user_settings.name', 'main_character_id');
})
->whereNotNull('user_settings.value')
->select('users.group_id', 'users.id', 'users.character_owner_hash', 'user_settings.value'));
->selectRaw('users.group_id, users.id, users.character_owner_hash, CAST(user_settings.value AS INT)'));

// fetch duplicated users which are not related to main character
$entries = DB::table('mig_groups')
Expand Down Expand Up @@ -308,7 +323,7 @@ public function up()
->update([
'id' => ($key + 2),
]);
});
});

// switch id field from simple integer to auto-increment field
Schema::table('users', function (Blueprint $table) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
/*
* This file is part of SeAT
*
* Copyright (C) 2015 to 2022 Leon Jacobs
* Copyright (C) 2015 to present Leon Jacobs
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
Expand All @@ -30,8 +30,19 @@ class RemoveDuplicateEntriesFromUniverseMoonContentsTable extends Migration
{
public function up()
{
// remove any duplicated entry base on moon_id and type_id
DB::statement('DELETE a FROM universe_moon_contents a INNER JOIN universe_moon_contents b WHERE a.id < b.id AND a.moon_id = b.moon_id AND a.type_id = b.type_id');
$engine = DB::getDriverName();

switch ($engine) {
case 'mysql':
// remove any duplicated entry base on moon_id and type_id
DB::statement('DELETE a FROM universe_moon_contents a INNER JOIN universe_moon_contents b WHERE a.id < b.id AND a.moon_id = b.moon_id AND a.type_id = b.type_id');
break;
case 'pgsql':
case 'postgresql':
// remove any duplicated entry base on moon_id and type_id
DB::statement('DELETE FROM universe_moon_contents a USING universe_moon_contents b WHERE a.id < b.id AND a.moon_id = b.moon_id AND a.type_id = b.type_id');
break;
}
}

public function down()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
/*
* This file is part of SeAT
*
* Copyright (C) 2015 to 2022 Leon Jacobs
* Copyright (C) 2015 to present Leon Jacobs
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
Expand All @@ -21,7 +21,9 @@
*/

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Schema;

/**
* Class RenameElementIDToEntityIDIntoStandingsProfileStandingsTable.
Expand All @@ -35,7 +37,17 @@ class RenameElementIDToEntityIDIntoStandingsProfileStandingsTable extends Migrat
*/
public function up()
{
DB::statement('ALTER TABLE standings_profile_standings CHANGE elementID entity_id bigint UNSIGNED NOT NULL');
Schema::table('standings_profile_standings', function (Blueprint $table) {
$table->bigInteger('entity_id')->unsigned()->nullable()->after('type');
});

DB::table('standings_profile_standings')
->update(['entity_id' => DB::raw('"elementID"')]);

Schema::table('standings_profile_standings', function (Blueprint $table) {
$table->bigInteger('entity_id')->unsigned()->nullable(false)->change();
$table->dropColumn('elementID');
});
}

/**
Expand All @@ -45,6 +57,15 @@ public function up()
*/
public function down()
{
DB::statement('ALTER TABLE standings_profile_standings CHANGE entity_id elementID int');
Schema::table('standings_profile_standings', function (Blueprint $table) {
$table->integer('elementID')->nullable();
});

DB::table('standings_profile_standings')
->update(['elementID' => DB::raw('"entity_id"')]);

Schema::table('standings_profile_standings', function (Blueprint $table) {
$table->dropColumn('entity_id');
});
}
}

0 comments on commit 47240bd

Please sign in to comment.