Skip to content

Firefly III: a personal finances manager

add some checks and balances to migrations.

James Cole7e4fece63de4
diff --git a/database/migrations/2024_11_30_075826_multi_piggy.php b/database/migrations/2024_11_30_075826_multi_piggy.php
index 99718041eb..7a59159f8b 100644
--- a/database/migrations/2024_11_30_075826_multi_piggy.php
+++ b/database/migrations/2024_11_30_075826_multi_piggy.php
@@ -46,46 +46,76 @@ return new class () extends Migration {
}
Schema::table('piggy_banks', static function (Blueprint $table): void {
// 2. make column nullable.
- $table->unsignedInteger('account_id')->nullable()->change();
+ if (!Schema::hasColumn('piggy_banks', 'account_id')) {
+ $table->unsignedInteger('account_id')->nullable()->change();
+ }
});
Schema::table('piggy_banks', static function (Blueprint $table): void {
// 3. add currency
- $table->integer('transaction_currency_id', false, true)->after('account_id')->nullable();
- $table->foreign('transaction_currency_id', 'unique_currency')->references('id')->on('transaction_currencies')->onDelete('cascade');
+ if (!Schema::hasColumn('piggy_banks', 'transaction_currency_id')) {
+ $table->integer('transaction_currency_id', false, true)->after('account_id')->nullable();
+ }
+ if (!self::hasForeign('piggy_banks', 'unique_currency')) {
+ $table->foreign('transaction_currency_id', 'unique_currency')->references('id')->on('transaction_currencies')->onDelete('cascade');
+ }
});
Schema::table('piggy_banks', static function (Blueprint $table): void {
// 4. rename columns
- $table->renameColumn('targetamount', 'target_amount');
- $table->renameColumn('startdate', 'start_date');
- $table->renameColumn('targetdate', 'target_date');
- $table->renameColumn('startdate_tz', 'start_date_tz');
- $table->renameColumn('targetdate_tz', 'target_date_tz');
+ if (Schema::hasColumn('piggy_banks', 'targetamount') && !Schema::hasColumn('piggy_banks', 'target_amount')) {
+ $table->renameColumn('targetamount', 'target_amount');
+ }
+ if (Schema::hasColumn('piggy_banks', 'startdate') && !Schema::hasColumn('piggy_banks', 'start_date')) {
+ $table->renameColumn('startdate', 'start_date');
+ }
+ if (Schema::hasColumn('piggy_banks', 'targetdate') && !Schema::hasColumn('piggy_banks', 'target_date')) {
+ $table->renameColumn('targetdate', 'target_date');
+ }
+ if (Schema::hasColumn('piggy_banks', 'targetdate') && !Schema::hasColumn('startdate_tz', 'start_date_tz')) {
+ $table->renameColumn('startdate_tz', 'start_date_tz');
+ }
+ if (Schema::hasColumn('piggy_banks', 'targetdate_tz') && !Schema::hasColumn('target_date_tz', 'start_date_tz')) {
+ $table->renameColumn('targetdate_tz', 'target_date_tz');
+ }
});
Schema::table('piggy_banks', static function (Blueprint $table): void {
// 5. add new index
- $table->foreign('account_id')->references('id')->on('accounts')->onDelete('set null');
+ if (!self::hasForeign('piggy_banks', 'piggy_banks_account_id_foreign')) {
+ $table->foreign('account_id')->references('id')->on('accounts')->onDelete('set null');
+ }
});
// rename some fields in piggy bank reps.
Schema::table('piggy_bank_repetitions', static function (Blueprint $table): void {
// 6. rename columns
- $table->renameColumn('currentamount', 'current_amount');
- $table->renameColumn('startdate', 'start_date');
- $table->renameColumn('targetdate', 'target_date');
- $table->renameColumn('startdate_tz', 'start_date_tz');
- $table->renameColumn('targetdate_tz', 'target_date_tz');
+ if (Schema::hasColumn('piggy_bank_repetitions', 'currentamount') && !Schema::hasColumn('piggy_bank_repetitions', 'current_amount')) {
+ $table->renameColumn('currentamount', 'current_amount');
+ }
+ if (Schema::hasColumn('piggy_bank_repetitions', 'startdate') && !Schema::hasColumn('piggy_bank_repetitions', 'start_date')) {
+ $table->renameColumn('startdate', 'start_date');
+ }
+ if (Schema::hasColumn('piggy_bank_repetitions', 'targetdate') && !Schema::hasColumn('piggy_bank_repetitions', 'target_date')) {
+ $table->renameColumn('targetdate', 'target_date');
+ }
+ if (Schema::hasColumn('piggy_bank_repetitions', 'startdate_tz') && !Schema::hasColumn('piggy_bank_repetitions', 'start_date_tz')) {
+ $table->renameColumn('startdate_tz', 'start_date_tz');
+ }
+ if (Schema::hasColumn('piggy_bank_repetitions', 'targetdate_tz') && !Schema::hasColumn('piggy_bank_repetitions', 'target_date_tz')) {
+ $table->renameColumn('targetdate_tz', 'target_date_tz');
+ }
});
// create table account_piggy_bank
- Schema::create('account_piggy_bank', static function (Blueprint $table): void {
- $table->id();
- $table->integer('account_id', false, true);
- $table->integer('piggy_bank_id', false, true);
- $table->decimal('current_amount', 32, 12)->default('0');
- $table->foreign('account_id')->references('id')->on('accounts')->onDelete('cascade');
- $table->foreign('piggy_bank_id')->references('id')->on('piggy_banks')->onDelete('cascade');
- $table->unique(['account_id', 'piggy_bank_id'], 'unique_piggy_save');
- });
+ if (!Schema::hasTable('account_piggy_bank')) {
+ Schema::create('account_piggy_bank', static function (Blueprint $table): void {
+ $table->id();
+ $table->integer('account_id', false, true);
+ $table->integer('piggy_bank_id', false, true);
+ $table->decimal('current_amount', 32, 12)->default('0');
+ $table->foreign('account_id')->references('id')->on('accounts')->onDelete('cascade');
+ $table->foreign('piggy_bank_id')->references('id')->on('piggy_banks')->onDelete('cascade');
+ $table->unique(['account_id', 'piggy_bank_id'], 'unique_piggy_save');
+ });
+ }
}
diff --git a/database/migrations/2024_12_19_061003_add_native_amount_column.php b/database/migrations/2024_12_19_061003_add_native_amount_column.php
index 2bbd8e4c97..87877bc503 100644
--- a/database/migrations/2024_12_19_061003_add_native_amount_column.php
+++ b/database/migrations/2024_12_19_061003_add_native_amount_column.php
@@ -41,8 +41,6 @@ return new class () extends Migration {
'piggy_banks' => ['native_target_amount'], // works
'transactions' => ['native_amount', 'native_foreign_amount'], // works
- // TODO button to recalculate all native amounts on selected pages?
-
];
/**
@@ -52,9 +50,11 @@ return new class () extends Migration {
{
foreach ($this->tables as $table => $fields) {
foreach ($fields as $field) {
- Schema::table($table, static function (Blueprint $table) use ($field): void {
+ Schema::table($table, static function (Blueprint $tableObject) use ($table, $field): void {
// add amount column
- $table->decimal($field, 32, 12)->nullable();
+ if(!Schema::hasColumn($table, $field)) {
+ $tableObject->decimal($field, 32, 12)->nullable();
+ }
});
}
}