From 6a13dd317d98dcbf67fe9ec9c514675f6130231b Mon Sep 17 00:00:00 2001 From: James Cole Date: Thu, 29 Dec 2016 20:19:20 +0100 Subject: [PATCH] Will also upgrade database. #508 --- app/Console/Commands/UpgradeDatabase.php | 27 ++++++++++++++++++- .../2016_12_28_203205_changes_for_v431.php | 24 ++++++++++++----- 2 files changed, 44 insertions(+), 7 deletions(-) diff --git a/app/Console/Commands/UpgradeDatabase.php b/app/Console/Commands/UpgradeDatabase.php index a526e31a5a..7b3e905b88 100644 --- a/app/Console/Commands/UpgradeDatabase.php +++ b/app/Console/Commands/UpgradeDatabase.php @@ -15,6 +15,8 @@ namespace FireflyIII\Console\Commands; use DB; +use FireflyIII\Models\BudgetLimit; +use FireflyIII\Models\LimitRepetition; use FireflyIII\Models\Transaction; use FireflyIII\Models\TransactionJournal; use Illuminate\Console\Command; @@ -57,6 +59,29 @@ class UpgradeDatabase extends Command public function handle() { $this->setTransactionIdentifier(); + $this->migrateRepetitions(); + } + + private function migrateRepetitions() + { + if (!Schema::hasTable('budget_limits')) { + return; + } + // get all budget limits with end_date NULL + $set = BudgetLimit::whereNull('end_date')->get(); + $this->line(sprintf('Found %d budget limit(s) to update', $set->count())); + /** @var BudgetLimit $budgetLimit */ + foreach ($set as $budgetLimit) { + // get limit repetition (should be just one): + /** @var LimitRepetition $repetition */ + $repetition = $budgetLimit->limitrepetitions()->first(); + if (!is_null($repetition)) { + $budgetLimit->end_date = $repetition->enddate; + $budgetLimit->save(); + $this->line(sprintf('Updated budget limit #%d', $budgetLimit->id)); + $repetition->delete(); + } + } } /** @@ -83,7 +108,7 @@ class UpgradeDatabase extends Command $journalIds = array_unique($result->pluck('id')->toArray()); foreach ($journalIds as $journalId) { - $this->updateJournal($journalId); + $this->updateJournal(intval($journalId)); } } diff --git a/database/migrations/2016_12_28_203205_changes_for_v431.php b/database/migrations/2016_12_28_203205_changes_for_v431.php index ad640bf06c..310ccac5a8 100644 --- a/database/migrations/2016_12_28_203205_changes_for_v431.php +++ b/database/migrations/2016_12_28_203205_changes_for_v431.php @@ -25,10 +25,16 @@ class ChangesForV431 extends Migration // reinstate "repeats" and "repeat_freq". Schema::table( 'budget_limits', function (Blueprint $table) { - $table->string('repeat_freq', 30); + $table->string('repeat_freq', 30)->nullable(); + } + ); + Schema::table( + 'budget_limits', function (Blueprint $table) { $table->boolean('repeats')->default(0); } ); + + // remove date field "end_date" Schema::table( 'budget_limits', function (Blueprint $table) { @@ -37,11 +43,11 @@ class ChangesForV431 extends Migration ); // change field "start_date" to "startdate" - Schema::table( - 'budget_limits', function (Blueprint $table) { - $table->renameColumn('startdate', 'start_date'); - } - ); +// Schema::table( +// 'budget_limits', function (Blueprint $table) { +// $table->renameColumn('startdate', 'start_date'); +// } +// ); } @@ -77,9 +83,15 @@ class ChangesForV431 extends Migration Schema::table( 'budget_limits', function (Blueprint $table) { $table->dropColumn('repeats'); + + } + ); + Schema::table( + 'budget_limits', function (Blueprint $table) { $table->dropColumn('repeat_freq'); } ); + } }