Revamp migrations.

This commit is contained in:
James Cole 2014-12-18 19:01:00 +01:00
parent d49dc599a2
commit eed6107ce7
4 changed files with 485 additions and 218 deletions

View File

@ -18,7 +18,7 @@ class CreateComponentTransactionTable extends Migration
*/
public function down()
{
Schema::drop('component_transaction');
Schema::dropIfExists('component_transaction');
}
/**

View File

@ -18,7 +18,7 @@ class CreateComponentRecurringTransactionTable extends Migration
*/
public function down()
{
Schema::drop('component_recurring_transaction');
Schema::dropIfExists('component_recurring_transaction');
}
/**

View File

@ -1,136 +1,206 @@
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\QueryException;
use Illuminate\Database\Schema\Blueprint;
/**
* SuppressWarnings(PHPMD.ShortMethodName)
* Down:
* 1. Create new Components based on Budgets.
* 2. Create new Components based on Categories
* 3. Update all budget_limits entries (component_id).
* 4. Drop column 'budget_id' in budget_limits.
* 5. Create table journal_components.
* 6. create entries for budgets in journal_components.
* 7. create entries for categories in journal_components.
* 8. drop table budget_journals
* 9. drop table category_journals
* 10. drop table budgets
* 11. drop table categories.
* 12. rename budget_limits to limits.
* 13. Rename piggy_bank_events to piggybank_events
* 14. Rename field 'budget_limit_id' to 'limit_id' in 'limit_repetitions'
* 15. Do not recreate component_recurring_transaction
* 16. Do not recreate component_transaction
* 17. Do not recreate field 'piggybank_id' in 'transactions'
*
*
* Up:
*
* 1. Create new budget table.
* 2. Create new category table.
* 3. Create journal_budget table.
* 4. Create journal_category table.
* 5. Move budgets to new budgets table AND move journal_components to budget_components.
* 6. Move categories to categories table AND move journal_components to category_components.
* 7. Rename limits to budget_limits.
* 8. Rename piggybank_events to piggy_bank_events
* 9. Rename field 'limit_id' to 'budget_limit_id' in 'limit_repetitions'
* 10. Create field budget_id in budget_limits.
* 11. Update budget_limits with budgets (instead of components).
* 12. drop table journal_components
* 13. Drop table component_recurring_transaction
* 14. Drop table component_transaction
* 15. Drop field 'piggybank_id' from 'transactions'
*
*
* Class ChangesForV321
*/
class ChangesForV321 extends Migration
{
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
/*
* Undo updates to budget_limits table.
*/
$this->undoBudgetLimits();
$this->undoPiggyBankEvents();
$this->moveBudgetsBack(); // 1.
$this->moveCategoriesBack(); // 2.
$this->updateComponentInBudgetLimits(); // 3.
$this->dropBudgetIdColumnInBudgetLimits(); // 4.
$createJournalComponents = new CreateComponentTransactionJournalTable; // 5.
$createJournalComponents->up();
$this->moveBackEntriesForBudgetsInJoinedTable(); // 6.
$this->moveBackEntriesForCategoriesInJoinedTable(); // 7.
$this->dropBudgetJournalTable(); // 8.
$this->dropCategoryJournalTable(); // 9.
$this->dropBudgetTable(); // 10.
$this->dropCategoryTable(); // 11.
$this->renameBudgetLimits(); // 12.
$this->renamePiggyBankEvents(); // 13.
$this->renameBudgetLimitToBudgetInRepetitions(); // 14.
// 15, 16, 17
$this->undoMoveBudgets();
$this->undoMoveCategories();
$this->undoCreateBudgetTables();
$this->undoCreateCategoryTables();
$this->undoRenameInLimitRepetitions();
$this->undoUpdateTransactionTable();
$this->undoDropCompRecurTable();
$this->undoDropCompTransTable();
}
public function undoBudgetLimits()
public function moveBudgetsBack()
{
Budget::get()->each(
function (Budget $budget) {
Component::firstOrCreate(
[
'name' => $budget->name,
'user_id' => $budget->user_id,
'class' => 'Budget'
]
);
}
);
}
public function moveCategoriesBack()
{
Category::get()->each(
function (Category $category) {
Component::firstOrCreate(
[
'name' => $category->name,
'user_id' => $category->user_id,
'class' => 'Category'
]
);
}
);
}
public function updateComponentInBudgetLimits()
{
BudgetLimit::get()->each(
function (BudgetLimit $bl) {
$budgetId = $bl->budget_id;
$budget = Budget::find($budgetId);
if ($budget) {
$component = Component::where('class', 'Budget')->where('user_id', $budget->user_id)->where('name', $budget->name)->first();
if ($component) {
$bl->component_id = $component->id;
$bl->save();
}
}
}
);
}
public function dropBudgetIdColumnInBudgetLimits()
{
Schema::table(
'budget_limits', function (Blueprint $table) {
$table->dropForeign('bid_foreign');
$table->dropColumn('budget_id');
$table->dropColumn('budget_id'); // also drop foreign key!
}
);
}
public function moveBackEntriesForBudgetsInJoinedTable()
{
$set = DB::table('budget_transaction_journal')->get();
foreach ($set as $entry) {
$budget = Budget::find($entry->budget_id);
if ($budget) {
$component = Component::where('class', 'Budget')->where('name', $budget->name)->where('user_id', $budget->user_id)->first();
if ($component) {
DB::table('component_transaction_journal')->insert(
[
'component_id' => $component->id,
'transaction_journal_id' => $entry->transaction_journal_id
]
);
}
}
}
}
public function moveBackEntriesForCategoriesInJoinedTable()
{
$set = DB::table('category_transaction_journal')->get();
foreach ($set as $entry) {
$category = Category::find($entry->category_id);
if ($category) {
$component = Component::where('class', 'Category')->where('name', $category->name)->where('user_id', $category->user_id)->first();
if ($component) {
DB::table('component_transaction_journal')->insert(
[
'component_id' => $component->id,
'transaction_journal_id' => $entry->transaction_journal_id
]
);
}
}
}
}
public function dropBudgetJournalTable()
{
Schema::dropIfExists('budget_transaction_journal');
}
public function dropCategoryJournalTable()
{
Schema::dropIfExists('category_transaction_journal');
}
public function dropBudgetTable()
{
Schema::dropIfExists('budgets');
}
public function dropCategoryTable()
{
Schema::dropIfExists('categories');
}
public function renameBudgetLimits()
{
Schema::rename('budget_limits', 'limits');
}
public function undoPiggyBankEvents()
public function renamePiggyBankEvents()
{
Schema::rename('piggy_bank_events', 'piggybank_events');
}
public function undoMoveBudgets()
{
Budget::get()->each(
function (Budget $budget) {
$entry = [
'user_id' => $budget->user_id,
'name' => $budget->name,
'class' => 'Budget'
];
$component = Component::firstOrCreate($entry);
Log::debug('Migrated budget back #' . $component->id . ': ' . $component->name);
// create entry in component_transaction_journal
$connections = DB::table('budget_transaction_journal')->where('budget_id', $budget->id)->get();
foreach ($connections as $connection) {
try {
DB::table('component_transaction_journal')->insert(
[
'component_id' => $component->id,
'transaction_journal_id' => $connection->transaction_journal_id
]
);
} catch (QueryException $e) {
}
}
}
);
}
public function undoMoveCategories()
{
Category::get()->each(
function (Category $category) {
$entry = [
'user_id' => $category->user_id,
'name' => $category->name,
'class' => 'Category'
];
$component = Component::firstOrCreate($entry);
Log::debug('Migrated category back #' . $component->id . ': ' . $component->name);
// create entry in component_transaction_journal
$connections = DB::table('category_transaction_journal')->where('category_id', $category->id)->get();
foreach ($connections as $connection) {
try {
DB::table('component_transaction_journal')->insert(
[
'component_id' => $component->id,
'transaction_journal_id' => $connection->transaction_journal_id
]
);
} catch (QueryException $e) {
}
}
}
);
}
public function undoCreateBudgetTables()
{
Schema::drop('budget_transaction_journal');
Schema::drop('budgets');
}
public function undoCreateCategoryTables()
{
Schema::drop('category_transaction_journal');
Schema::drop('categories');
}
public function undoRenameInLimitRepetitions()
public function renameBudgetLimitToBudgetInRepetitions()
{
Schema::table(
'limit_repetitions', function (Blueprint $table) {
@ -139,46 +209,6 @@ class ChangesForV321 extends Migration
);
}
public function undoUpdateTransactionTable()
{
Schema::table(
'transactions', function (Blueprint $table) {
$table->integer('piggybank_id')->nullable()->unsigned();
$table->foreign('piggybank_id')->references('id')->on('piggybanks')->onDelete('set null');
}
);
}
public function undoDropCompRecurTable()
{
Schema::create(
'component_recurring_transaction', function (Blueprint $table) {
$table->increments('id');
$table->integer('component_id')->unsigned();
$table->integer('recurring_transaction_id')->unsigned();
$table->boolean('optional');
$table->foreign('component_id')->references('id')->on('components')->onDelete('cascade');
$table->foreign('recurring_transaction_id')->references('id')->on('recurring_transactions')->onDelete('cascade');
$table->unique(['component_id', 'recurring_transaction_id'], 'cid_rtid_unique');
}
);
}
public function undoDropCompTransTable()
{
Schema::create(
'component_transaction', function (Blueprint $table) {
$table->increments('id');
$table->integer('component_id')->unsigned();
$table->integer('transaction_id')->unsigned();
$table->foreign('component_id')->references('id')->on('components')->onDelete('cascade');
$table->foreign('transaction_id')->references('id')->on('transactions')->onDelete('cascade');
$table->unique(['component_id', 'transaction_id']);
}
);
}
/**
* Run the migrations.
*
@ -186,21 +216,38 @@ class ChangesForV321 extends Migration
*/
public function up()
{
$this->doCreateBudgetTables();
$this->doRenameInLimitRepetitions();
$this->doBudgetLimits();
$this->doPiggyBankEvents();
$this->doCreateCategoryTables();
$this->doUpdateTransactionTable();
$this->doDropCompRecurTable();
$this->doDropCompTransTable();
$this->doMoveBudgets();
$this->doMoveCategories();
$this->createBudgetTable(); // 1.
$this->createCategoryTable(); // 2.
$this->createBudgetJournalTable(); // 3
$this->createCategoryJournalTable(); // 4.
$this->moveBudgets(); // 5.
$this->moveCategories(); // 6.
$this->correctNameForBudgetLimits(); // 7.
$this->correctNameForPiggyBankEvents(); // 8.
$this->renameBudgetToBudgetLimitInRepetitions(); // 9.
$this->addBudgetIdFieldToBudgetLimits(); // 10.
$this->moveComponentIdToBudgetId(); // 11.
$this->dropComponentJournalTable(); // 12.
$this->dropComponentRecurringTransactionTable(); // 13.
$this->dropComponentTransactionTable(); // 14.
$this->dropPiggyBankIdFromTransactions(); // 15.
// $this->doRenameInLimitRepetitions();
// $this->doBudgetLimits();
// $this->doPiggyBankEvents();
// $this->doCreateCategoryTables();
// $this->doUpdateTransactionTable();
// $this->doDropCompRecurTable();
// $this->doDropCompTransTable();
// $this->doMoveBudgets();
// $this->doMoveCategories();
// $this->doMoveLimitReferences();
}
public function doCreateBudgetTables()
public function createBudgetTable()
{
Schema::create(
'budgets', function (Blueprint $table) {
@ -213,45 +260,11 @@ class ChangesForV321 extends Migration
$table->unique(['user_id', 'name']);
}
);
Schema::create(
'budget_transaction_journal', function (Blueprint $table) {
$table->increments('id');
$table->integer('budget_id')->unsigned();
$table->integer('transaction_journal_id')->unsigned();
$table->foreign('budget_id')->references('id')->on('budgets')->onDelete('cascade');
$table->foreign('transaction_journal_id')->references('id')->on('transaction_journals')->onDelete('cascade');
$table->unique(['budget_id', 'transaction_journal_id'], 'bid_tjid_unique');
}
);
}
public function doRenameInLimitRepetitions()
{
Schema::table(
'limit_repetitions', function (Blueprint $table) {
$table->renameColumn('limit_id', 'budget_limit_id');
}
);
}
public function doBudgetLimits()
{
Schema::rename('limits', 'budget_limits');
Schema::table(
'budget_limits', function (Blueprint $table) {
$table->integer('budget_id')->unsigned()->after('updated_at');
$table->foreign('budget_id', 'bid_foreign')->references('id')->on('budgets')->onDelete('cascade');
}
);
}
public function doPiggyBankEvents()
{
Schema::rename('piggybank_events', 'piggy_bank_events');
}
public function doCreateCategoryTables()
public function createCategoryTable()
{
Schema::create(
'categories', function (Blueprint $table) {
@ -264,6 +277,24 @@ class ChangesForV321 extends Migration
$table->unique(['user_id', 'name']);
}
);
}
public function createBudgetJournalTable()
{
Schema::create(
'budget_transaction_journal', function (Blueprint $table) {
$table->increments('id');
$table->integer('budget_id')->unsigned();
$table->integer('transaction_journal_id')->unsigned();
$table->foreign('budget_id')->references('id')->on('budgets')->onDelete('cascade');
$table->foreign('transaction_journal_id')->references('id')->on('transaction_journals')->onDelete('cascade');
$table->unique(['budget_id', 'transaction_journal_id'], 'budid_tjid_unique');
}
);
}
public function createCategoryJournalTable()
{
Schema::create(
'category_transaction_journal', function (Blueprint $table) {
$table->increments('id');
@ -274,31 +305,9 @@ class ChangesForV321 extends Migration
$table->unique(['category_id', 'transaction_journal_id'], 'catid_tjid_unique');
}
);
}
public function doUpdateTransactionTable()
{
Schema::table(
'transactions', function (Blueprint $table) {
$table->dropForeign('transactions_piggybank_id_foreign');
#$table->dropIndex('transactions_piggybank_id_foreign');
$table->dropColumn('piggybank_id');
}
);
}
public function doDropCompRecurTable()
{
Schema::drop('component_recurring_transaction');
}
public function doDropCompTransTable()
{
Schema::drop('component_transaction');
}
public function doMoveBudgets()
public function moveBudgets()
{
Component::where('class', 'Budget')->get()->each(
function (Component $c) {
@ -323,7 +332,7 @@ class ChangesForV321 extends Migration
);
}
public function doMoveCategories()
public function moveCategories()
{
Component::where('class', 'Category')->get()->each(
function (Component $c) {
@ -348,4 +357,208 @@ class ChangesForV321 extends Migration
);
}
public function correctNameForBudgetLimits()
{
Schema::rename('limits', 'budget_limits');
}
public function correctNameForPiggyBankEvents()
{
Schema::rename('piggybank_events', 'piggy_bank_events');
}
public function renameBudgetToBudgetLimitInRepetitions()
{
Schema::table(
'limit_repetitions', function (Blueprint $table) {
$table->renameColumn('limit_id', 'budget_limit_id');
}
);
}
public function addBudgetIdFieldToBudgetLimits()
{
Schema::table(
'budget_limits', function (Blueprint $table) {
$table->integer('budget_id')->unsigned()->after('updated_at');
$table->foreign('budget_id', 'bid_foreign')->references('id')->on('budgets')->onDelete('cascade');
}
);
}
public function moveComponentIdToBudgetId()
{
BudgetLimit::get()->each(
function (BudgetLimit $bl) {
$component = Component::find($bl->component_id);
if ($component) {
$budget = Budget::whereName($component->name)->whereUserId($component->user_id)->first();
if ($budget) {
$bl->budget_id = $budget->id;
$bl->save();
}
}
}
);
}
public function dropComponentJournalTable()
{
Schema::dropIfExists('component_transaction_journal');
}
public function dropComponentRecurringTransactionTable()
{
Schema::dropIfExists('component_recurring_transaction');
}
public function dropComponentTransactionTable()
{
Schema::dropIfExists('component_transaction');
}
public function dropPiggyBankIdFromTransactions()
{
Schema::table(
'transactions', function (Blueprint $table) {
if (Schema::hasColumn('transactions', 'piggybank_id')) {
$table->dropForeign('transactions_piggybank_id_foreign');
$table->dropColumn('piggybank_id');
}
}
);
}
//
// public function doRenameInLimitRepetitions()
// {
// Schema::table(
// 'limit_repetitions', function (Blueprint $table) {
// $table->renameColumn('limit_id', 'budget_limit_id');
// }
// );
// }
//
// public function doBudgetLimits()
// {
// Schema::rename('limits', 'budget_limits');
// Schema::table(
// 'budget_limits', function (Blueprint $table) {
// $table->integer('budget_id')->unsigned()->after('updated_at');
// $table->foreign('budget_id', 'bid_foreign')->references('id')->on('budgets')->onDelete('cascade');
// }
// );
// }
//
// public function doPiggyBankEvents()
// {
// Schema::rename('piggybank_events', 'piggy_bank_events');
//
// }
//
// public function doCreateCategoryTables()
// {
// Schema::create(
// 'categories', function (Blueprint $table) {
// $table->increments('id');
// $table->timestamps();
// $table->softDeletes();
// $table->string('name', 50);
// $table->integer('user_id')->unsigned();
// $table->foreign('user_id')->references('id')->on('users')->onDelete('cascade');
// $table->unique(['user_id', 'name']);
// }
// );
// Schema::create(
// 'category_transaction_journal', function (Blueprint $table) {
// $table->increments('id');
// $table->integer('category_id')->unsigned();
// $table->integer('transaction_journal_id')->unsigned();
// $table->foreign('category_id')->references('id')->on('categories')->onDelete('cascade');
// $table->foreign('transaction_journal_id')->references('id')->on('transaction_journals')->onDelete('cascade');
// $table->unique(['category_id', 'transaction_journal_id'], 'catid_tjid_unique');
// }
// );
//
// }
//
// public function doUpdateTransactionTable()
// {
// Schema::table(
// 'transactions', function (Blueprint $table) {
// $table->dropForeign('transactions_piggybank_id_foreign');
// #$table->dropIndex('transactions_piggybank_id_foreign');
// $table->dropColumn('piggybank_id');
// }
// );
// }
//
// public function doDropCompRecurTable()
// {
// Schema::drop('component_recurring_transaction');
// }
//
// public function doDropCompTransTable()
// {
// Schema::drop('component_transaction');
// }
//
// public function doMoveBudgets()
// {
// Component::where('class', 'Budget')->get()->each(
// function (Component $c) {
// $entry = [
// 'user_id' => $c->user_id,
// 'name' => $c->name
//
// ];
// $budget = Budget::firstOrCreate($entry);
// Log::debug('Migrated budget #' . $budget->id . ': ' . $budget->name);
// // create entry in budget_transaction_journal
// $connections = DB::table('component_transaction_journal')->where('component_id', $c->id)->get();
// foreach ($connections as $connection) {
// DB::table('budget_transaction_journal')->insert(
// [
// 'budget_id' => $budget->id,
// 'transaction_journal_id' => $connection->transaction_journal_id
// ]
// );
// }
// }
// );
// }
//
// public function doMoveCategories()
// {
// Component::where('class', 'Category')->get()->each(
// function (Component $c) {
// $entry = [
// 'user_id' => $c->user_id,
// 'name' => $c->name
//
// ];
// $category = Category::firstOrCreate($entry);
// Log::debug('Migrated category #' . $category->id . ': ' . $category->name);
// // create entry in category_transaction_journal
// $connections = DB::table('component_transaction_journal')->where('component_id', $c->id)->get();
// foreach ($connections as $connection) {
// DB::table('category_transaction_journal')->insert(
// [
// 'category_id' => $category->id,
// 'transaction_journal_id' => $connection->transaction_journal_id
// ]
// );
// }
// }
// );
// }
//
// public function doMoveLimitReferences()
// {
// throw new \FireflyIII\Exception\FireflyException('TODO');
// }
}

66
composer.lock generated
View File

@ -3029,21 +3029,22 @@
},
{
"name": "maximebf/debugbar",
"version": "1.10.1",
"version": "v1.10.2",
"source": {
"type": "git",
"url": "https://github.com/maximebf/php-debugbar.git",
"reference": "f100f27124e8e042074d6264e33c75c6ea2677e6"
"reference": "4971283c1fa8da051e97d1fa62e053d49259f7ef"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/maximebf/php-debugbar/zipball/f100f27124e8e042074d6264e33c75c6ea2677e6",
"reference": "f100f27124e8e042074d6264e33c75c6ea2677e6",
"url": "https://api.github.com/repos/maximebf/php-debugbar/zipball/4971283c1fa8da051e97d1fa62e053d49259f7ef",
"reference": "4971283c1fa8da051e97d1fa62e053d49259f7ef",
"shasum": ""
},
"require": {
"php": ">=5.3.0",
"psr/log": "~1.0"
"psr/log": "~1.0",
"symfony/var-dumper": "~2.6"
},
"require-dev": {
"phpunit/phpunit": "~4.0"
@ -3080,7 +3081,7 @@
"keywords": [
"debug"
],
"time": "2014-11-23 12:05:01"
"time": "2014-12-17 08:39:39"
},
{
"name": "mockery/mockery",
@ -4295,6 +4296,59 @@
"homepage": "http://symfony.com",
"time": "2014-12-02 20:19:20"
},
{
"name": "symfony/var-dumper",
"version": "v2.6.1",
"target-dir": "Symfony/Component/VarDumper",
"source": {
"type": "git",
"url": "https://github.com/symfony/var-dumper.git",
"reference": "8f3ee04faeca3b8418229b2efbfc8b2b8625b8aa"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/symfony/var-dumper/zipball/8f3ee04faeca3b8418229b2efbfc8b2b8625b8aa",
"reference": "8f3ee04faeca3b8418229b2efbfc8b2b8625b8aa",
"shasum": ""
},
"require": {
"php": ">=5.3.3"
},
"suggest": {
"ext-symfony_debug": ""
},
"type": "library",
"extra": {
"branch-alias": {
"dev-master": "2.6-dev"
}
},
"autoload": {
"files": [
"Resources/functions/dump.php"
],
"psr-0": {
"Symfony\\Component\\VarDumper\\": ""
}
},
"notification-url": "https://packagist.org/downloads/",
"license": [
"MIT"
],
"authors": [
{
"name": "Nicolas Grekas",
"email": "p@tchwork.com"
}
],
"description": "Symfony mechanism for exploring and dumping PHP variables",
"homepage": "http://symfony.com",
"keywords": [
"debug",
"dump"
],
"time": "2014-11-18 10:08:24"
},
{
"name": "symfony/yaml",
"version": "v2.6.1",