mirror of
https://github.com/firefly-iii/firefly-iii.git
synced 2026-01-09 20:11:22 +00:00
Updates to budgets.
This commit is contained in:
@@ -1,39 +0,0 @@
|
||||
<?php
|
||||
|
||||
use Illuminate\Database\Schema\Blueprint;
|
||||
use Illuminate\Database\Migrations\Migration;
|
||||
|
||||
class ChangesFor385 extends Migration
|
||||
{
|
||||
/**
|
||||
* Run the migrations.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function up()
|
||||
{
|
||||
// remove an index.
|
||||
Schema::table(
|
||||
'budget_limits', function (Blueprint $table) {
|
||||
$table->dropUnique('unique_limit');
|
||||
}
|
||||
);
|
||||
|
||||
// create it again, correctly.
|
||||
Schema::table(
|
||||
'budget_limits', function (Blueprint $table) {
|
||||
$table->unique(['budget_id', 'startdate','repeat_freq'], 'unique_limit');
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Reverse the migrations.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function down()
|
||||
{
|
||||
//
|
||||
}
|
||||
}
|
||||
148
database/migrations/2016_04_25_093451_changes_for_v385.php
Normal file
148
database/migrations/2016_04_25_093451_changes_for_v385.php
Normal file
@@ -0,0 +1,148 @@
|
||||
<?php
|
||||
|
||||
use FireflyIII\Models\BudgetLimit;
|
||||
use Illuminate\Database\Migrations\Migration;
|
||||
use Illuminate\Database\Schema\Blueprint;
|
||||
|
||||
/**
|
||||
* Class ChangesForV385
|
||||
*/
|
||||
class ChangesForV385 extends Migration
|
||||
{
|
||||
/**
|
||||
* Reverse the migrations.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function down()
|
||||
{
|
||||
$backup = $this->backupRepeatFreqsFromString();
|
||||
|
||||
// drop string and create enum field
|
||||
Schema::table(
|
||||
'budget_limits', function (Blueprint $table) {
|
||||
$table->dropColumn('repeat_freq');
|
||||
}
|
||||
);
|
||||
Schema::table(
|
||||
'budget_limits', function (Blueprint $table) {
|
||||
$table->enum('repeat_freq', ['daily', 'weekly', 'monthly', 'quarterly', 'half-year', 'yearly']);
|
||||
}
|
||||
);
|
||||
|
||||
// restore backup. Change unknowns to "monthly".
|
||||
$this->restoreRepeatFreqsToEnum($backup);
|
||||
}
|
||||
|
||||
/**
|
||||
* Run the migrations.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function up()
|
||||
{
|
||||
// remove an index.
|
||||
Schema::table(
|
||||
'budget_limits', function (Blueprint $table) {
|
||||
$table->dropUnique('unique_limit');
|
||||
$table->dropForeign('bid_foreign');
|
||||
$table->dropUnique('unique_bl_combi');
|
||||
}
|
||||
);
|
||||
|
||||
// recreate foreign key:
|
||||
Schema::table(
|
||||
'budget_limits', function (Blueprint $table) {
|
||||
$table->foreign('budget_id', 'bid_foreign')->references('id')->on('budgets')->onDelete('cascade');
|
||||
}
|
||||
);
|
||||
|
||||
|
||||
|
||||
// backup values
|
||||
$backup = $this->backupRepeatFreqsFromEnum();
|
||||
|
||||
// drop enum and create varchar field
|
||||
Schema::table(
|
||||
'budget_limits', function (Blueprint $table) {
|
||||
$table->dropColumn('repeat_freq');
|
||||
}
|
||||
);
|
||||
Schema::table(
|
||||
'budget_limits', function (Blueprint $table) {
|
||||
$table->string('repeat_freq', 20)->default('monthly');
|
||||
}
|
||||
);
|
||||
|
||||
// put data back:
|
||||
$this->restoreRepeatFreqsToVarchar($backup);
|
||||
|
||||
|
||||
// create it again, correctly.
|
||||
Schema::table(
|
||||
'budget_limits', function (Blueprint $table) {
|
||||
$table->unique(['budget_id', 'startdate', 'repeat_freq'], 'unique_limit');
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array
|
||||
*/
|
||||
private function backupRepeatFreqsFromEnum(): array
|
||||
{
|
||||
$backup = [];
|
||||
$set = BudgetLimit::get();
|
||||
/** @var BudgetLimit $entry */
|
||||
foreach ($set as $entry) {
|
||||
$backup[$entry->id] = $entry->repeat_freq;
|
||||
}
|
||||
|
||||
return $backup;
|
||||
}
|
||||
|
||||
/**
|
||||
* Same routine.
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
private function backupRepeatFreqsFromString()
|
||||
{
|
||||
return $this->backupRepeatFreqsFromEnum();
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array $backup
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
private function restoreRepeatFreqsToEnum(array $backup): bool
|
||||
{
|
||||
foreach ($backup as $id => $repeatFreq) {
|
||||
$budgetLimit = BudgetLimit::find($id);
|
||||
if (!in_array($repeatFreq, ['daily', 'weekly', 'monthly', 'quarterly', 'half-year', 'yearly'])) {
|
||||
$repeatFreq = 'monthly';
|
||||
}
|
||||
$budgetLimit->repeat_freq = $repeatFreq;
|
||||
$budgetLimit->save();
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array $backup
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
private function restoreRepeatFreqsToVarchar(array $backup): bool
|
||||
{
|
||||
foreach ($backup as $id => $repeatFreq) {
|
||||
$budgetLimit = BudgetLimit::find($id);
|
||||
$budgetLimit->repeat_freq = $repeatFreq;
|
||||
$budgetLimit->save();
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user