Catch DB errors

This commit is contained in:
James Cole
2023-04-07 19:33:19 +02:00
parent dd11f98be7
commit 79a4eec96e
26 changed files with 985 additions and 656 deletions

View File

@@ -22,6 +22,7 @@
declare(strict_types=1);
use Doctrine\DBAL\Schema\Exception\ColumnDoesNotExist;
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\QueryException;
use Illuminate\Database\Schema\Blueprint;
@@ -39,25 +40,20 @@ return new class () extends Migration {
*/
public function up(): void
{
Schema::table(
'currency_exchange_rates',
function (Blueprint $table) {
if (!Schema::hasColumn('currency_exchange_rates', 'user_group_id')) {
try {
try {
Schema::table(
'currency_exchange_rates',
function (Blueprint $table) {
if (!Schema::hasColumn('currency_exchange_rates', 'user_group_id')) {
$table->bigInteger('user_group_id', false, true)->nullable()->after('user_id');
} catch (QueryException $e) {
Log::error(sprintf('Could not add column "user_group_id" to table "currency_exchange_rates": %s', $e->getMessage()));
Log::error('If the column exists already (see error), this is not a problem. Otherwise, please create a GitHub discussion.');
}
try {
$table->foreign('user_group_id', 'cer_to_ugi')->references('id')->on('user_groups')->onDelete('set null')->onUpdate('cascade');
} catch (QueryException $e) {
Log::error(sprintf('Could not add foreign key "cer_to_ugi" to table "currency_exchange_rates": %s', $e->getMessage()));
Log::error('If the foreign key exists already (see error), this is not a problem. Otherwise, please create a GitHub discussion.');
}
}
}
);
);
} catch (QueryException $e) {
Log::error(sprintf('Could not execute query: %s', $e->getMessage()));
Log::error('If the column or index already exists (see error), this is not an problem. Otherwise, please open a GitHub discussion.');
}
}
/**
@@ -67,24 +63,19 @@ return new class () extends Migration {
*/
public function down(): void
{
Schema::table(
'currency_exchange_rates',
function (Blueprint $table) {
try {
try {
Schema::table(
'currency_exchange_rates',
function (Blueprint $table) {
$table->dropForeign('cer_to_ugi');
} catch (QueryException $e) {
Log::error(sprintf('Could not drop foreign key "cer_to_ugi" from table "currency_exchange_rates": %s', $e->getMessage()));
Log::error('If the foreign key does not exist (see error message), this is not a problem. Otherwise, please create a GitHub discussion.');
}
if (Schema::hasColumn('currency_exchange_rates', 'user_group_id')) {
try {
if (Schema::hasColumn('currency_exchange_rates', 'user_group_id')) {
$table->dropColumn('user_group_id');
} catch (QueryException $e) {
Log::error(sprintf('Could not drop column "user_group_id" from table "currency_exchange_rates": %s', $e->getMessage()));
Log::error('If the column does not exist (see error message), this is not a problem. Otherwise, please create a GitHub discussion.');
}
}
}
);
);
} catch (QueryException|ColumnDoesNotExist $e) {
Log::error(sprintf('Could not execute query: %s', $e->getMessage()));
Log::error('If the column or index already exists (see error), this is not an problem. Otherwise, please open a GitHub discussion.');
}
}
};