mirror of
https://github.com/firefly-iii/firefly-iii.git
synced 2025-02-25 18:45:27 -06:00
New migration command.
This commit is contained in:
@@ -76,6 +76,7 @@ class CorrectDatabase extends Command
|
|||||||
'firefly-iii:fix-long-descriptions',
|
'firefly-iii:fix-long-descriptions',
|
||||||
'firefly-iii:fix-recurring-transactions',
|
'firefly-iii:fix-recurring-transactions',
|
||||||
'firefly-iii:restore-oauth-keys',
|
'firefly-iii:restore-oauth-keys',
|
||||||
|
'firefly-iii:upgrade-group-information',
|
||||||
'firefly-iii:fix-transaction-types',
|
'firefly-iii:fix-transaction-types',
|
||||||
'firefly-iii:fix-frontpage-accounts',
|
'firefly-iii:fix-frontpage-accounts',
|
||||||
];
|
];
|
||||||
|
109
app/Console/Commands/Integrity/UpdateGroupInformation.php
Normal file
109
app/Console/Commands/Integrity/UpdateGroupInformation.php
Normal file
@@ -0,0 +1,109 @@
|
|||||||
|
<?php
|
||||||
|
/*
|
||||||
|
* UpdateGroupInformation.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/>.
|
||||||
|
*/
|
||||||
|
|
||||||
|
namespace FireflyIII\Console\Commands\Integrity;
|
||||||
|
|
||||||
|
use FireflyIII\Models\Account;
|
||||||
|
use FireflyIII\Models\Attachment;
|
||||||
|
use FireflyIII\Models\AvailableBudget;
|
||||||
|
use FireflyIII\Models\Bill;
|
||||||
|
use FireflyIII\Models\Budget;
|
||||||
|
use FireflyIII\Models\Category;
|
||||||
|
use FireflyIII\Models\CurrencyExchangeRate;
|
||||||
|
use FireflyIII\Models\Recurrence;
|
||||||
|
use FireflyIII\Models\Rule;
|
||||||
|
use FireflyIII\Models\RuleGroup;
|
||||||
|
use FireflyIII\Models\Tag;
|
||||||
|
use FireflyIII\Models\TransactionGroup;
|
||||||
|
use FireflyIII\Models\TransactionJournal;
|
||||||
|
use FireflyIII\Models\UserGroup;
|
||||||
|
use FireflyIII\Models\Webhook;
|
||||||
|
use FireflyIII\User;
|
||||||
|
use Illuminate\Console\Command;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Class UpdateGroupInformation
|
||||||
|
*/
|
||||||
|
class UpdateGroupInformation extends Command
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* The name and signature of the console command.
|
||||||
|
*
|
||||||
|
* @var string
|
||||||
|
*/
|
||||||
|
protected $signature = 'firefly-iii:upgrade-group-information';
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The console command description.
|
||||||
|
*
|
||||||
|
* @var string
|
||||||
|
*/
|
||||||
|
protected $description = 'Makes sure that every object is linked to a group';
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Execute the console command.
|
||||||
|
*
|
||||||
|
* @return int
|
||||||
|
*/
|
||||||
|
public function handle()
|
||||||
|
{
|
||||||
|
// objects: accounts, attachments, available budgets, bills, budgets, categories, currency_exchange_rates
|
||||||
|
// recurrences, rule groups, rules, tags, transaction groups, transaction journals, webhooks
|
||||||
|
|
||||||
|
$users = User::get();
|
||||||
|
/** @var User $user */
|
||||||
|
foreach ($users as $user) {
|
||||||
|
$this->updateGroupInfo($user);
|
||||||
|
}
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
private function updateGroupInfo(User $user): void
|
||||||
|
{
|
||||||
|
$group = $user->userGroup;
|
||||||
|
if (null === $group) {
|
||||||
|
$this->warn(sprintf('User "%s" has no group.', $user->email));
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
$set = [Account::class, Attachment::class, AvailableBudget::class,
|
||||||
|
Bill::class, Budget::class, Category::class, CurrencyExchangeRate::class,
|
||||||
|
Recurrence::class, RuleGroup::class, Rule::class, Tag::class, TransactionGroup::class,
|
||||||
|
TransactionJournal::class, Webhook::class];
|
||||||
|
foreach ($set as $className) {
|
||||||
|
$this->updateGroupInfoForObject($user, $group, $className);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param User $user
|
||||||
|
* @param UserGroup $group
|
||||||
|
* @param string $className
|
||||||
|
* @return void
|
||||||
|
*/
|
||||||
|
private function updateGroupInfoForObject(User $user, UserGroup $group, string $className): void
|
||||||
|
{
|
||||||
|
$result = $className::where('user_id', $user->id)->where('user_group_id', null)->update(['user_group_id' => $group->id]);
|
||||||
|
if (0 !== $result) {
|
||||||
|
$this->line(sprintf('Moved %d %s objects to the correct group.', $result, str_replace('FireflyIII\\Models\\', '', $className)));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@@ -99,6 +99,7 @@ class UpgradeDatabase extends Command
|
|||||||
'firefly-iii:fix-transaction-types',
|
'firefly-iii:fix-transaction-types',
|
||||||
'firefly-iii:fix-frontpage-accounts',
|
'firefly-iii:fix-frontpage-accounts',
|
||||||
'firefly-iii:fix-ibans',
|
'firefly-iii:fix-ibans',
|
||||||
|
'firefly-iii:upgrade-group-information',
|
||||||
|
|
||||||
// two report commands
|
// two report commands
|
||||||
'firefly-iii:report-empty-objects',
|
'firefly-iii:report-empty-objects',
|
||||||
|
@@ -110,6 +110,7 @@ class InstallController extends Controller
|
|||||||
'firefly-iii:fix-transaction-types' => [],
|
'firefly-iii:fix-transaction-types' => [],
|
||||||
'firefly-iii:fix-frontpage-accounts' => [],
|
'firefly-iii:fix-frontpage-accounts' => [],
|
||||||
'firefly-iii:fix-ibans' => [],
|
'firefly-iii:fix-ibans' => [],
|
||||||
|
'firefly-iii:upgrade-group-information' => [],
|
||||||
|
|
||||||
// final command to set latest version in DB
|
// final command to set latest version in DB
|
||||||
'firefly-iii:set-latest-version' => ['--james-is-cool' => true],
|
'firefly-iii:set-latest-version' => ['--james-is-cool' => true],
|
||||||
|
@@ -192,6 +192,7 @@
|
|||||||
"@php artisan firefly-iii:report-empty-objects",
|
"@php artisan firefly-iii:report-empty-objects",
|
||||||
"@php artisan firefly-iii:report-sum",
|
"@php artisan firefly-iii:report-sum",
|
||||||
"@php artisan firefly-iii:restore-oauth-keys",
|
"@php artisan firefly-iii:restore-oauth-keys",
|
||||||
|
"@php artisan firefly-iii:upgrade-group-information",
|
||||||
"@php artisan firefly-iii:set-latest-version --james-is-cool",
|
"@php artisan firefly-iii:set-latest-version --james-is-cool",
|
||||||
"@php artisan firefly:instructions update",
|
"@php artisan firefly:instructions update",
|
||||||
"@php artisan firefly-iii:verify-security-alerts",
|
"@php artisan firefly-iii:verify-security-alerts",
|
||||||
|
46
database/migrations/2022_08_21_104626_add_user_groups.php
Normal file
46
database/migrations/2022_08_21_104626_add_user_groups.php
Normal file
@@ -0,0 +1,46 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
use Illuminate\Database\Migrations\Migration;
|
||||||
|
use Illuminate\Database\Schema\Blueprint;
|
||||||
|
use Illuminate\Support\Facades\Schema;
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
return new class extends Migration {
|
||||||
|
/**
|
||||||
|
* Run the migrations.
|
||||||
|
*
|
||||||
|
* @return void
|
||||||
|
*/
|
||||||
|
public function up(): void
|
||||||
|
{
|
||||||
|
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');
|
||||||
|
$table->foreign('user_group_id', 'cer_to_ugi')->references('id')->on('user_groups')->onDelete('set null')->onUpdate('cascade');
|
||||||
|
}
|
||||||
|
}
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Reverse the migrations.
|
||||||
|
*
|
||||||
|
* @return void
|
||||||
|
*/
|
||||||
|
public function down(): void
|
||||||
|
{
|
||||||
|
Schema::table(
|
||||||
|
'currency_exchange_rates', function (Blueprint $table) {
|
||||||
|
|
||||||
|
$table->dropForeign('cer_to_ugi');
|
||||||
|
if (Schema::hasColumn('currency_exchange_rates', 'user_group_id')) {
|
||||||
|
$table->dropColumn('user_group_id');
|
||||||
|
}
|
||||||
|
}
|
||||||
|
);
|
||||||
|
}
|
||||||
|
};
|
Reference in New Issue
Block a user