mirror of
https://github.com/firefly-iii/firefly-iii.git
synced 2025-10-22 20:16:22 +00:00
Refactor upgrade and verify commands.
This commit is contained in:
@@ -21,6 +21,7 @@
|
||||
|
||||
namespace FireflyIII\Console\Commands\Upgrade;
|
||||
|
||||
use DB;
|
||||
use FireflyIII\Models\Budget;
|
||||
use FireflyIII\Models\Category;
|
||||
use FireflyIII\Models\Transaction;
|
||||
@@ -53,6 +54,7 @@ class BackToJournals extends Command
|
||||
*/
|
||||
public function handle(): int
|
||||
{
|
||||
$start = microtime(true);
|
||||
if (!$this->isMigrated()) {
|
||||
$this->error('Please run firefly-iii:migrate-to-groups first.');
|
||||
}
|
||||
@@ -66,13 +68,35 @@ class BackToJournals extends Command
|
||||
}
|
||||
|
||||
$this->migrateAll();
|
||||
|
||||
$this->info('Updated category and budget info for all journals.');
|
||||
$end = round(microtime(true) - $start, 2);
|
||||
$this->info(sprintf('Updated category and budget info for all transaction journals in %s seconds.', $end));
|
||||
$this->markAsExecuted();
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array
|
||||
*/
|
||||
private function getIdsForBudgets(): array
|
||||
{
|
||||
$transactions = DB::table('budget_transaction')->distinct()->get(['transaction_id'])->pluck('transaction_id')->toArray();
|
||||
|
||||
return DB::table('transactions')->whereIn('transactions.id', $transactions)->get(['transaction_journal_id'])->pluck('transaction_journal_id')->toArray(
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array
|
||||
*/
|
||||
private function getIdsForCategories(): array
|
||||
{
|
||||
$transactions = DB::table('category_transaction')->distinct()->get(['transaction_id'])->pluck('transaction_id')->toArray();
|
||||
|
||||
return DB::table('transactions')->whereIn('transactions.id', $transactions)->get(['transaction_journal_id'])->pluck('transaction_journal_id')->toArray(
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return bool
|
||||
*/
|
||||
@@ -112,10 +136,24 @@ class BackToJournals extends Command
|
||||
*/
|
||||
private function migrateAll(): void
|
||||
{
|
||||
$journals = TransactionJournal::get();
|
||||
$this->migrateBudgets();
|
||||
$this->migrateCategories();
|
||||
|
||||
// empty tables
|
||||
DB::table('budget_transaction')->delete();
|
||||
DB::table('categories_transaction')->delete();
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
private function migrateBudgets(): void
|
||||
{
|
||||
$journalIds = $this->getIdsForBudgets();
|
||||
$journals = TransactionJournal::whereIn('id', $journalIds)->with(['transactions', 'budgets', 'transactions.budgets'])->get();
|
||||
$this->line(sprintf('Check %d transaction journals for budget info.', $journals->count()));
|
||||
/** @var TransactionJournal $journal */
|
||||
foreach ($journals as $journal) {
|
||||
$this->migrateCategoriesForJournal($journal);
|
||||
$this->migrateBudgetsForJournal($journal);
|
||||
}
|
||||
}
|
||||
@@ -127,19 +165,38 @@ class BackToJournals extends Command
|
||||
{
|
||||
// grab category from first transaction
|
||||
/** @var Transaction $transaction */
|
||||
$transaction = $journal->transactions()->first();
|
||||
$transaction = $journal->transactions->first();
|
||||
if (null === $transaction) {
|
||||
$this->info(sprintf('Transaction journal #%d has no transactions. Will be fixed later.', $journal->id));
|
||||
|
||||
return;
|
||||
}
|
||||
/** @var Budget $budget */
|
||||
$budget = $transaction->budgets()->first();
|
||||
if (null !== $budget) {
|
||||
$budget = $transaction->budgets->first();
|
||||
/** @var Budget $journalBudget */
|
||||
$journalBudget = $journal->budgets->first();
|
||||
if (null !== $budget && null !== $journalBudget && $budget->id !== $journalBudget->id) {
|
||||
// sync to journal:
|
||||
$journal->budgets()->sync([(int)$budget->id]);
|
||||
}
|
||||
|
||||
// remove from transactions:
|
||||
$journal->transactions()->each(
|
||||
function (Transaction $transaction) {
|
||||
$transaction->budgets()->sync([]);
|
||||
}
|
||||
);
|
||||
// budget in transaction overrules journal.
|
||||
if (null === $budget && null !== $journalBudget) {
|
||||
$journal->budgets()->sync([]);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
private function migrateCategories(): void
|
||||
{
|
||||
$journalIds = $this->getIdsForCategories();
|
||||
$journals = TransactionJournal::whereIn('id', $journalIds)->with(['transactions', 'categories', 'transactions.categories'])->get();
|
||||
$this->line(sprintf('Check %d transaction journals for category info.', $journals->count()));
|
||||
/** @var TransactionJournal $journal */
|
||||
foreach ($journals as $journal) {
|
||||
$this->migrateCategoriesForJournal($journal);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -150,19 +207,24 @@ class BackToJournals extends Command
|
||||
{
|
||||
// grab category from first transaction
|
||||
/** @var Transaction $transaction */
|
||||
$transaction = $journal->transactions()->first();
|
||||
$transaction = $journal->transactions->first();
|
||||
if (null === $transaction) {
|
||||
$this->info(sprintf('Transaction journal #%d has no transactions. Will be fixed later.', $journal->id));
|
||||
|
||||
return;
|
||||
}
|
||||
/** @var Category $category */
|
||||
$category = $transaction->categories()->first();
|
||||
if (null !== $category) {
|
||||
$category = $transaction->categories->first();
|
||||
/** @var Category $journalCategory */
|
||||
$journalCategory = $journal->categories->first();
|
||||
if (null !== $category && null !== $journalCategory && $category->id !== $journalCategory->id) {
|
||||
// sync to journal:
|
||||
$journal->categories()->sync([(int)$category->id]);
|
||||
}
|
||||
|
||||
// remove from transactions:
|
||||
$journal->transactions()->each(
|
||||
function (Transaction $transaction) {
|
||||
$transaction->categories()->sync([]);
|
||||
}
|
||||
);
|
||||
// category in transaction overrules journal.
|
||||
if (null === $category && null !== $journalCategory) {
|
||||
$journal->categories()->sync([]);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user