From 79b8890dd2f12026d80f6285bf4b0aac8867b34b Mon Sep 17 00:00:00 2001 From: Yongxin Wang Date: Thu, 2 Oct 2014 15:35:22 +0800 Subject: [PATCH 1/3] Rewrite upgrade db versin 6 to 7 --- .../gnucash/android/db/DatabaseHelper.java | 108 +++++++++++++++--- 1 file changed, 89 insertions(+), 19 deletions(-) diff --git a/app/src/org/gnucash/android/db/DatabaseHelper.java b/app/src/org/gnucash/android/db/DatabaseHelper.java index 7a95bd6c0..c9c6c00a7 100644 --- a/app/src/org/gnucash/android/db/DatabaseHelper.java +++ b/app/src/org/gnucash/android/db/DatabaseHelper.java @@ -24,6 +24,7 @@ import android.util.Log; import android.widget.Toast; import org.gnucash.android.model.AccountType; +import org.gnucash.android.model.Transaction; import static org.gnucash.android.db.DatabaseSchema.*; @@ -33,6 +34,7 @@ * @author Ngewi Fet * */ +@SuppressWarnings("deprecation") public class DatabaseHelper extends SQLiteOpenHelper { /** @@ -222,27 +224,95 @@ public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { if (oldVersion == 6 && newVersion >= DatabaseSchema.SPLITS_DB_VERSION){ Log.i(LOG_TAG, "Upgrading database to version 7"); - - //for users who do not have double-entry activated, we create imbalance accounts for their splits - //TODO: Enable when we can hide imbalance accounts from user -// List currencies = MigrationHelper.getCurrencies(db); -// AccountsDbAdapter accountsDbAdapter = new AccountsDbAdapter(db); -// for (Currency currency : currencies) { -// accountsDbAdapter.getOrCreateImbalanceAccountUID(currency); -// } - + db.beginTransaction(); try { - String filepath = MigrationHelper.exportGnucashXML(db); - - dropAllDatabaseTables(db); - createDatabaseTables(db); - - MigrationHelper.importGnucashXML(db, filepath); - } catch (Exception e){ - Toast.makeText(mContext, "Error upgrading database.\n" + e.getMessage(), Toast.LENGTH_LONG).show(); - throw new RuntimeException(e); + // backup transaction table + db.execSQL("ALTER TABLE " + TransactionEntry.TABLE_NAME + " RENAME TO " + TransactionEntry.TABLE_NAME + "_bak"); + // create new transaction table + db.execSQL("create table " + TransactionEntry.TABLE_NAME + " (" + + TransactionEntry._ID + " integer primary key autoincrement, " + + TransactionEntry.COLUMN_UID + " varchar(255) not null, " + + TransactionEntry.COLUMN_DESCRIPTION + " varchar(255), " + + TransactionEntry.COLUMN_NOTES + " text, " + + TransactionEntry.COLUMN_TIMESTAMP + " integer not null, " + + TransactionEntry.COLUMN_EXPORTED + " tinyint default 0, " + + TransactionEntry.COLUMN_CURRENCY + " varchar(255) not null, " + + TransactionEntry.COLUMN_RECURRENCE_PERIOD + " integer default 0, " + + "UNIQUE (" + TransactionEntry.COLUMN_UID + ") " + + ");"); + // initialize new transaction table wiht data from old table + db.execSQL("INSERT INTO " + TransactionEntry.TABLE_NAME + " ( " + + TransactionEntry._ID + " , " + + TransactionEntry.COLUMN_UID + " , " + + TransactionEntry.COLUMN_DESCRIPTION + " , " + + TransactionEntry.COLUMN_NOTES + " , " + + TransactionEntry.COLUMN_TIMESTAMP + " , " + + TransactionEntry.COLUMN_EXPORTED + " , " + + TransactionEntry.COLUMN_CURRENCY + " , " + + TransactionEntry.COLUMN_RECURRENCE_PERIOD + " ) SELECT " + + TransactionEntry.TABLE_NAME + "_bak." + TransactionEntry._ID + " , " + + TransactionEntry.TABLE_NAME + "_bak." + TransactionEntry.COLUMN_UID + " , " + + TransactionEntry.TABLE_NAME + "_bak." + TransactionEntry.COLUMN_DESCRIPTION + " , " + + TransactionEntry.TABLE_NAME + "_bak." + TransactionEntry.COLUMN_NOTES + " , " + + TransactionEntry.TABLE_NAME + "_bak." + TransactionEntry.COLUMN_TIMESTAMP + " , " + + TransactionEntry.TABLE_NAME + "_bak." + TransactionEntry.COLUMN_EXPORTED + " , " + + AccountEntry.TABLE_NAME + "." + AccountEntry.COLUMN_CURRENCY + " , " + + TransactionEntry.TABLE_NAME + "_bak." + TransactionEntry.COLUMN_RECURRENCE_PERIOD + + " FROM " + TransactionEntry.TABLE_NAME + "_bak , " + AccountEntry.TABLE_NAME + + " ON " + TransactionEntry.TABLE_NAME + "_bak.account_uid == " + AccountEntry.TABLE_NAME + "." + AccountEntry.COLUMN_UID + ); + // create split table + db.execSQL("CREATE TABLE " + SplitEntry.TABLE_NAME + " (" + + SplitEntry._ID + " integer primary key autoincrement, " + + SplitEntry.COLUMN_UID + " varchar(255) not null, " + + SplitEntry.COLUMN_MEMO + " text, " + + SplitEntry.COLUMN_TYPE + " varchar(255) not null, " + + SplitEntry.COLUMN_AMOUNT + " varchar(255) not null, " + + SplitEntry.COLUMN_ACCOUNT_UID + " varchar(255) not null, " + + SplitEntry.COLUMN_TRANSACTION_UID + " varchar(255) not null, " + + "FOREIGN KEY (" + SplitEntry.COLUMN_ACCOUNT_UID + ") REFERENCES " + AccountEntry.TABLE_NAME + " (" + AccountEntry.COLUMN_UID + "), " + + "FOREIGN KEY (" + SplitEntry.COLUMN_TRANSACTION_UID + ") REFERENCES " + TransactionEntry.TABLE_NAME + " (" + TransactionEntry.COLUMN_UID + "), " + + "UNIQUE (" + SplitEntry.COLUMN_UID + ") " + + ");"); + // Initialize split table with data from backup transaction table + // New split table is initialized after the new transaction table as the + // foreign key constraint will stop any data from being inserted + // If new split table is created before the backup is made, the foreign key + // constraint will be rewritten to refer to the backup transaction table + db.execSQL("INSERT INTO " + SplitEntry.TABLE_NAME + " ( " + + SplitEntry.COLUMN_UID + " , " + + SplitEntry.COLUMN_TYPE + " , " + + SplitEntry.COLUMN_AMOUNT + " , " + + SplitEntry.COLUMN_ACCOUNT_UID + " , " + + SplitEntry.COLUMN_TRANSACTION_UID + " ) SELECT " + // _ID, autogenerated + + "LOWER(HEX(RANDOMBLOB(16))) , " + // COLUMN_MEMO, empty + + "CASE WHEN " + TransactionEntry.TABLE_NAME + "_bak.type IN ( 'CASH' , 'BANK', 'ASSET', 'EXPENSE', 'RECEIVABLE', 'STOCK', 'MUTUAL' ) THEN CASE WHEN " + + SplitEntry.COLUMN_AMOUNT + " < 0 THEN 'DEBIT' ELSE 'CREDIT' END ELSE CASE WHEN " + + SplitEntry.COLUMN_AMOUNT + " < 0 THEN 'CREDIT' ELSE 'DEBIT' END END , " + + "ABS ( " + TransactionEntry.TABLE_NAME + "_bak.amount ) , " + + TransactionEntry.TABLE_NAME + "_bak.account_uid , " + + TransactionEntry.TABLE_NAME + "_bak." + TransactionEntry.COLUMN_UID + + " FROM " + TransactionEntry.TABLE_NAME + "_bak UNION SELECT " + // _ID, autogenerated + + "LOWER(HEX(RANDOMBLOB(16))) AS " + SplitEntry.COLUMN_UID + " , " + // COLUMN_MEMO, empty + + "CASE WHEN " + TransactionEntry.TABLE_NAME + "_bak.type IN ( 'CASH' , 'BANK', 'ASSET', 'EXPENSE', 'RECEIVABLE', 'STOCK', 'MUTUAL' ) THEN CASE WHEN " + + SplitEntry.COLUMN_AMOUNT + " < 0 THEN 'CREDIT' ELSE 'DEBIT' END ELSE CASE WHEN " + + SplitEntry.COLUMN_AMOUNT + " < 0 THEN 'DEBIT' ELSE 'CREDIT' END END , " + + "ABS ( " + TransactionEntry.TABLE_NAME + "_bak.amount ) , " + + TransactionEntry.TABLE_NAME + "_bak." + KEY_DOUBLE_ENTRY_ACCOUNT_UID + " , " + + TransactionEntry.TABLE_NAME + "_baK." + TransactionEntry.COLUMN_UID + + " FROM " + TransactionEntry.TABLE_NAME + "_bak WHERE " + TransactionEntry.TABLE_NAME + "_bak." + KEY_DOUBLE_ENTRY_ACCOUNT_UID + " IS NOT NULL" + ); + // drop backup transaction table + db.execSQL("DROP TABLE " + TransactionEntry.TABLE_NAME + "_bak"); + db.setTransactionSuccessful(); + oldVersion = DatabaseSchema.SPLITS_DB_VERSION; + } finally { + db.endTransaction(); } - oldVersion = DatabaseSchema.SPLITS_DB_VERSION; } } From 84859f24d7d602f2d5381c5c6daa97585cbc284b Mon Sep 17 00:00:00 2001 From: Yongxin Wang Date: Thu, 9 Oct 2014 23:13:54 +0800 Subject: [PATCH 2/3] fix split type --- .../gnucash/android/db/DatabaseHelper.java | 24 +++++++++---------- 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/app/src/org/gnucash/android/db/DatabaseHelper.java b/app/src/org/gnucash/android/db/DatabaseHelper.java index c9c6c00a7..f6107f1fd 100644 --- a/app/src/org/gnucash/android/db/DatabaseHelper.java +++ b/app/src/org/gnucash/android/db/DatabaseHelper.java @@ -285,26 +285,26 @@ public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { + SplitEntry.COLUMN_AMOUNT + " , " + SplitEntry.COLUMN_ACCOUNT_UID + " , " + SplitEntry.COLUMN_TRANSACTION_UID + " ) SELECT " - // _ID, autogenerated + "LOWER(HEX(RANDOMBLOB(16))) , " - // COLUMN_MEMO, empty - + "CASE WHEN " + TransactionEntry.TABLE_NAME + "_bak.type IN ( 'CASH' , 'BANK', 'ASSET', 'EXPENSE', 'RECEIVABLE', 'STOCK', 'MUTUAL' ) THEN CASE WHEN " - + SplitEntry.COLUMN_AMOUNT + " < 0 THEN 'DEBIT' ELSE 'CREDIT' END ELSE CASE WHEN " - + SplitEntry.COLUMN_AMOUNT + " < 0 THEN 'CREDIT' ELSE 'DEBIT' END END , " + + "CASE WHEN " + AccountEntry.TABLE_NAME + "." + AccountEntry.COLUMN_TYPE + " IN ( 'CASH' , 'BANK', 'ASSET', 'EXPENSE', 'RECEIVABLE', 'STOCK', 'MUTUAL' ) THEN CASE WHEN " + + SplitEntry.COLUMN_AMOUNT + " < 0 THEN 'CREDIT' ELSE 'DEBIT' END ELSE CASE WHEN " + + SplitEntry.COLUMN_AMOUNT + " < 0 THEN 'DEBIT' ELSE 'CREDIT' END END , " + "ABS ( " + TransactionEntry.TABLE_NAME + "_bak.amount ) , " + TransactionEntry.TABLE_NAME + "_bak.account_uid , " + TransactionEntry.TABLE_NAME + "_bak." + TransactionEntry.COLUMN_UID - + " FROM " + TransactionEntry.TABLE_NAME + "_bak UNION SELECT " - // _ID, autogenerated + + " FROM " + TransactionEntry.TABLE_NAME + "_bak , " + AccountEntry.TABLE_NAME + + " ON " + TransactionEntry.TABLE_NAME + "_bak.account_uid = " + AccountEntry.TABLE_NAME + "." + AccountEntry.COLUMN_UID + + " UNION SELECT " + "LOWER(HEX(RANDOMBLOB(16))) AS " + SplitEntry.COLUMN_UID + " , " - // COLUMN_MEMO, empty - + "CASE WHEN " + TransactionEntry.TABLE_NAME + "_bak.type IN ( 'CASH' , 'BANK', 'ASSET', 'EXPENSE', 'RECEIVABLE', 'STOCK', 'MUTUAL' ) THEN CASE WHEN " - + SplitEntry.COLUMN_AMOUNT + " < 0 THEN 'CREDIT' ELSE 'DEBIT' END ELSE CASE WHEN " - + SplitEntry.COLUMN_AMOUNT + " < 0 THEN 'DEBIT' ELSE 'CREDIT' END END , " + + "CASE WHEN " + AccountEntry.TABLE_NAME + "." + AccountEntry.COLUMN_TYPE + " IN ( 'CASH' , 'BANK', 'ASSET', 'EXPENSE', 'RECEIVABLE', 'STOCK', 'MUTUAL' ) THEN CASE WHEN " + + SplitEntry.COLUMN_AMOUNT + " < 0 THEN 'DEBIT' ELSE 'CREDIT' END ELSE CASE WHEN " + + SplitEntry.COLUMN_AMOUNT + " < 0 THEN 'CREDIT' ELSE 'DEBIT' END END , " + "ABS ( " + TransactionEntry.TABLE_NAME + "_bak.amount ) , " + TransactionEntry.TABLE_NAME + "_bak." + KEY_DOUBLE_ENTRY_ACCOUNT_UID + " , " + TransactionEntry.TABLE_NAME + "_baK." + TransactionEntry.COLUMN_UID - + " FROM " + TransactionEntry.TABLE_NAME + "_bak WHERE " + TransactionEntry.TABLE_NAME + "_bak." + KEY_DOUBLE_ENTRY_ACCOUNT_UID + " IS NOT NULL" + + " FROM " + TransactionEntry.TABLE_NAME + "_bak , " + AccountEntry.TABLE_NAME + + " ON " + TransactionEntry.TABLE_NAME + "_bak." + KEY_DOUBLE_ENTRY_ACCOUNT_UID + " = " + AccountEntry.TABLE_NAME + "." + AccountEntry.COLUMN_UID + + " WHERE " + TransactionEntry.TABLE_NAME + "_bak." + KEY_DOUBLE_ENTRY_ACCOUNT_UID + " IS NOT NULL" ); // drop backup transaction table db.execSQL("DROP TABLE " + TransactionEntry.TABLE_NAME + "_bak"); From 3f2a1d1b0a04ec9d6afea332834dee53fe868b6e Mon Sep 17 00:00:00 2001 From: Yongxin Wang Date: Fri, 10 Oct 2014 00:03:58 +0800 Subject: [PATCH 3/3] fix split type --- app/src/org/gnucash/android/db/DatabaseHelper.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/src/org/gnucash/android/db/DatabaseHelper.java b/app/src/org/gnucash/android/db/DatabaseHelper.java index f6107f1fd..90ebb7f41 100644 --- a/app/src/org/gnucash/android/db/DatabaseHelper.java +++ b/app/src/org/gnucash/android/db/DatabaseHelper.java @@ -303,7 +303,7 @@ public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { + TransactionEntry.TABLE_NAME + "_bak." + KEY_DOUBLE_ENTRY_ACCOUNT_UID + " , " + TransactionEntry.TABLE_NAME + "_baK." + TransactionEntry.COLUMN_UID + " FROM " + TransactionEntry.TABLE_NAME + "_bak , " + AccountEntry.TABLE_NAME - + " ON " + TransactionEntry.TABLE_NAME + "_bak." + KEY_DOUBLE_ENTRY_ACCOUNT_UID + " = " + AccountEntry.TABLE_NAME + "." + AccountEntry.COLUMN_UID + + " ON " + TransactionEntry.TABLE_NAME + "_bak.account_uid = " + AccountEntry.TABLE_NAME + "." + AccountEntry.COLUMN_UID + " WHERE " + TransactionEntry.TABLE_NAME + "_bak." + KEY_DOUBLE_ENTRY_ACCOUNT_UID + " IS NOT NULL" ); // drop backup transaction table