James Cole 2025-02-01 18:59:55 +01:00
parent 25f99b23b2
commit f38e510526
No known key found for this signature in database
GPG Key ID: B49A324B7EAD6D80
2 changed files with 21 additions and 7 deletions

View File

@ -99,10 +99,8 @@ class PiggyBankTransformer extends AbstractTransformer
'id' => (string) $piggyBank->id, 'id' => (string) $piggyBank->id,
'created_at' => $piggyBank->created_at->toAtomString(), 'created_at' => $piggyBank->created_at->toAtomString(),
'updated_at' => $piggyBank->updated_at->toAtomString(), 'updated_at' => $piggyBank->updated_at->toAtomString(),
'accounts' => $this->renderAccounts($piggyBank),
// 'account_id' => (string)$piggyBank->account_id,
// 'account_name' => $piggyBank->account->name,
'name' => $piggyBank->name, 'name' => $piggyBank->name,
'accounts' => $this->renderAccounts($piggyBank),
'currency_id' => (string) $currency->id, 'currency_id' => (string) $currency->id,
'currency_code' => $currency->code, 'currency_code' => $currency->code,
'currency_symbol' => $currency->symbol, 'currency_symbol' => $currency->symbol,
@ -137,6 +135,7 @@ class PiggyBankTransformer extends AbstractTransformer
'id' => $account->id, 'id' => $account->id,
'name' => $account->name, 'name' => $account->name,
'current_amount' => (string) $account->pivot->current_amount, 'current_amount' => (string) $account->pivot->current_amount,
'native_current_amount'=> (string) $account->pivot->native_current_amount,
// TODO add balance, add left to save. // TODO add balance, add left to save.
]; ];
} }

View File

@ -25,10 +25,11 @@ declare(strict_types=1);
use Illuminate\Database\Migrations\Migration; use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint; use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
use Illuminate\Support\Facades\Log; use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\Schema;
return new class () extends Migration { return new class () extends Migration {
/** /**
* Run the migrations. * Run the migrations.
*/ */
@ -37,8 +38,9 @@ return new class () extends Migration {
// make account_id nullable and the relation also nullable. // make account_id nullable and the relation also nullable.
try { try {
Schema::table('piggy_banks', static function (Blueprint $table): void { Schema::table('piggy_banks', static function (Blueprint $table): void {
// 1. drop index if (self::hasForeign('piggy_banks', 'piggy_banks_account_id_foreign')) {
$table->dropForeign('piggy_banks_account_id_foreign'); $table->dropForeign('piggy_banks_account_id_foreign');
}
}); });
} catch (RuntimeException $e) { } catch (RuntimeException $e) {
Log::error('Could not drop foreign key "piggy_banks_account_id_foreign". Probably not an issue.'); Log::error('Could not drop foreign key "piggy_banks_account_id_foreign". Probably not an issue.');
@ -105,7 +107,9 @@ return new class () extends Migration {
$table->renameColumn('target_date_tz', 'targetdate_tz'); $table->renameColumn('target_date_tz', 'targetdate_tz');
// 3. drop currency again + index // 3. drop currency again + index
$table->dropForeign('unique_currency'); if (self::hasForeign('piggy_banks', 'unique_currency')) {
$table->dropForeign('unique_currency');
}
$table->dropColumn('transaction_currency_id'); $table->dropColumn('transaction_currency_id');
// 2. make column non-nullable. // 2. make column non-nullable.
@ -127,4 +131,15 @@ return new class () extends Migration {
Schema::dropIfExists('account_piggy_bank'); Schema::dropIfExists('account_piggy_bank');
} }
protected static function hasForeign(string $table, string $column) {
$foreignKeysDefinitions = Schema::getForeignKeys($table);
foreach($foreignKeysDefinitions as $foreignKeyDefinition) {
if($foreignKeyDefinition['name'] === $column) {
return true;
}
}
return false;
}
}; };