From 4cdb14301d777b8dac9bb74a59eefec8623eeea6 Mon Sep 17 00:00:00 2001 From: James Cole Date: Mon, 1 Apr 2024 18:03:43 +0200 Subject: [PATCH] Add administration specific thing to preferences. --- .../Commands/Correction/CorrectDatabase.php | 1 + .../Correction/MigratePreferences.php | 74 +++++++++++++++++++ .../Commands/Upgrade/UpgradeDatabase.php | 1 + config/firefly.php | 2 +- ..._04_01_174351_expand_preferences_table.php | 38 ++++++++++ 5 files changed, 115 insertions(+), 1 deletion(-) create mode 100644 app/Console/Commands/Correction/MigratePreferences.php create mode 100644 database/migrations/2024_04_01_174351_expand_preferences_table.php diff --git a/app/Console/Commands/Correction/CorrectDatabase.php b/app/Console/Commands/Correction/CorrectDatabase.php index 8c22aaeab5..1bfcb30c9d 100644 --- a/app/Console/Commands/Correction/CorrectDatabase.php +++ b/app/Console/Commands/Correction/CorrectDatabase.php @@ -73,6 +73,7 @@ class CorrectDatabase extends Command // new! 'firefly-iii:unify-group-accounts', 'firefly-iii:trigger-credit-recalculation', + 'firefly-iii:migrate-preferences' ]; foreach ($commands as $command) { $this->friendlyLine(sprintf('Now executing command "%s"', $command)); diff --git a/app/Console/Commands/Correction/MigratePreferences.php b/app/Console/Commands/Correction/MigratePreferences.php new file mode 100644 index 0000000000..0999f2ff5d --- /dev/null +++ b/app/Console/Commands/Correction/MigratePreferences.php @@ -0,0 +1,74 @@ +where('user_id', $user->id)->first(); + if(null === $preference) { + continue; + } + if(null !== $preference->user_group_id) { + $preference->user_group_id = $user->user_group_id; + $preference->save(); + $count++; + } + } + if($count > 0) { + $this->info(sprintf('Migrated %d preference(s) for user #%d ("%s").', $count, $user->id, $user->email)); + } + } + + + return CommandAlias::SUCCESS; + } +} diff --git a/app/Console/Commands/Upgrade/UpgradeDatabase.php b/app/Console/Commands/Upgrade/UpgradeDatabase.php index dae08d5f1a..1614dc5c3c 100644 --- a/app/Console/Commands/Upgrade/UpgradeDatabase.php +++ b/app/Console/Commands/Upgrade/UpgradeDatabase.php @@ -69,6 +69,7 @@ class UpgradeDatabase extends Command 'firefly-iii:create-group-memberships', 'firefly-iii:upgrade-group-information', 'firefly-iii:upgrade-currency-preferences', + 'firefly-iii:correct-database', ]; $args = []; if ($this->option('force')) { diff --git a/config/firefly.php b/config/firefly.php index e52cb6f9a2..8a785c0a09 100644 --- a/config/firefly.php +++ b/config/firefly.php @@ -119,7 +119,7 @@ return [ ], 'version' => '6.1.13', 'api_version' => '2.0.13', - 'db_version' => 23, + 'db_version' => 24, // generic settings 'maxUploadSize' => 1073741824, // 1 GB diff --git a/database/migrations/2024_04_01_174351_expand_preferences_table.php b/database/migrations/2024_04_01_174351_expand_preferences_table.php new file mode 100644 index 0000000000..debec4fbc8 --- /dev/null +++ b/database/migrations/2024_04_01_174351_expand_preferences_table.php @@ -0,0 +1,38 @@ +bigInteger('user_group_id', false, true)->nullable()->after('user_id'); + $table->foreign('user_group_id', 'preferences_to_ugi')->references('id')->on('user_groups')->onDelete('set null')->onUpdate('cascade'); + } + } + ); + } catch (QueryException $e) { + 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.'); + } + } + + /** + * Reverse the migrations. + */ + public function down(): void + { + // + } +};