Files
firefly-iii/database/migrations/2022_08_21_104626_add_user_groups.php

77 lines
2.9 KiB
PHP
Raw Normal View History

2022-08-23 05:43:40 +02:00
<?php
2022-10-16 19:29:53 +02:00
/*
* 2022_08_21_104626_add_user_groups.php
* Copyright (c) 2022 james@firefly-iii.org
*
* This file is part of Firefly III (https://github.com/firefly-iii).
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
2022-09-17 07:07:25 +02:00
declare(strict_types=1);
2022-08-23 05:43:40 +02:00
use Illuminate\Database\Migrations\Migration;
2023-04-05 20:22:17 +02:00
use Illuminate\Database\QueryException;
2022-08-23 05:43:40 +02:00
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
return new class () extends Migration {
2022-08-23 05:43:40 +02:00
/**
* Run the migrations.
2023-12-20 19:41:37 +01:00
*
* @SuppressWarnings(PHPMD.ShortMethodName)
2022-08-23 05:43:40 +02:00
*/
public function up(): void
{
2023-04-07 19:33:19 +02:00
try {
Schema::table(
'currency_exchange_rates',
2023-12-21 05:06:17 +01:00
static function (Blueprint $table): void {
2023-04-07 19:33:19 +02:00
if (!Schema::hasColumn('currency_exchange_rates', 'user_group_id')) {
2023-04-05 20:22:17 +02:00
$table->bigInteger('user_group_id', false, true)->nullable()->after('user_id');
$table->foreign('user_group_id', 'cer_to_ugi')->references('id')->on('user_groups')->onDelete('set null')->onUpdate('cascade');
}
2022-12-29 19:43:43 +01:00
}
2023-04-07 19:33:19 +02:00
);
} catch (QueryException $e) {
2023-08-11 19:37:52 +02:00
app('log')->error(sprintf('Could not execute query: %s', $e->getMessage()));
app('log')->error('If the column or index already exists (see error), this is not an problem. Otherwise, please open a GitHub discussion.');
2023-04-07 19:33:19 +02:00
}
2022-08-23 05:43:40 +02:00
}
/**
* Reverse the migrations.
*/
public function down(): void
{
2023-04-07 19:33:19 +02:00
try {
Schema::table(
'currency_exchange_rates',
2023-12-21 05:06:17 +01:00
static function (Blueprint $table): void {
2023-05-13 05:56:49 +02:00
if ('sqlite' !== config('database.default')) {
$table->dropForeign('cer_to_ugi');
}
2023-04-07 19:33:19 +02:00
if (Schema::hasColumn('currency_exchange_rates', 'user_group_id')) {
2023-04-05 20:22:17 +02:00
$table->dropColumn('user_group_id');
}
2022-12-29 19:43:43 +01:00
}
2023-04-07 19:33:19 +02:00
);
} catch (QueryException $e) {
2023-08-11 19:37:52 +02:00
app('log')->error(sprintf('Could not execute query: %s', $e->getMessage()));
app('log')->error('If the column or index already exists (see error), this is not an problem. Otherwise, please open a GitHub discussion.');
2023-04-07 19:33:19 +02:00
}
2022-08-23 05:43:40 +02:00
}
};