Skip to content

Latest commodities #739

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 9 commits into from
Nov 9, 2019
4 changes: 1 addition & 3 deletions app/src/main/java/org/gnucash/android/db/DatabaseHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -278,9 +278,7 @@ public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion){
while(oldVersion < newVersion){
try {
Method method = MigrationHelper.class.getDeclaredMethod("upgradeDbToVersion" + (oldVersion+1), SQLiteDatabase.class);
Object result = method.invoke(null, db);
oldVersion = Integer.parseInt(result.toString());

oldVersion = (int) method.invoke(null, db);
} catch (NoSuchMethodException e) {
String msg = String.format("Database upgrade method upgradeToVersion%d(SQLiteDatabase) definition not found ", newVersion);
Log.e(LOG_TAG, msg, e);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public class DatabaseSchema {
* Version number of database containing accounts and transactions info.
* With any change to the database schema, this number must increase
*/
public static final int DATABASE_VERSION = 15;
public static final int DATABASE_VERSION = 16;

/**
* Name of the database
Expand Down
65 changes: 45 additions & 20 deletions app/src/main/java/org/gnucash/android/db/MigrationHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -264,7 +264,7 @@ public static int upgradeDbToVersion2(SQLiteDatabase db) {
" ADD COLUMN double_account_uid varchar(255)";

//introducing sub accounts
Log.i(DatabaseHelper.LOG_TAG, "Adding column for parent accounts");
Log.i(LOG_TAG, "Adding column for parent accounts");
String addParentAccountSql = "ALTER TABLE " + AccountEntry.TABLE_NAME +
" ADD COLUMN " + AccountEntry.COLUMN_PARENT_ACCOUNT_UID + " varchar(255)";

Expand All @@ -273,7 +273,7 @@ public static int upgradeDbToVersion2(SQLiteDatabase db) {

//update account types to GnuCash account types
//since all were previously CHECKING, now all will be CASH
Log.i(DatabaseHelper.LOG_TAG, "Converting account types to GnuCash compatible types");
Log.i(LOG_TAG, "Converting account types to GnuCash compatible types");
ContentValues cv = new ContentValues();
cv.put(SplitEntry.COLUMN_TYPE, AccountType.CASH.toString());
db.update(AccountEntry.TABLE_NAME, cv, null, null);
Expand Down Expand Up @@ -495,7 +495,7 @@ static int upgradeDbToVersion7(SQLiteDatabase db) {
* @return New database version (8) if upgrade successful, old version (7) if unsuccessful
*/
static int upgradeDbToVersion8(SQLiteDatabase db) {
Log.i(DatabaseHelper.LOG_TAG, "Upgrading database to version 8");
Log.i(LOG_TAG, "Upgrading database to version 8");
int oldVersion = 7;
new File(Exporter.LEGACY_BASE_FOLDER_PATH + "/backups/").mkdirs();
new File(Exporter.LEGACY_BASE_FOLDER_PATH + "/exports/").mkdirs();
Expand All @@ -505,7 +505,7 @@ static int upgradeDbToVersion8(SQLiteDatabase db) {
db.beginTransaction();
try {

Log.i(DatabaseHelper.LOG_TAG, "Creating scheduled actions table");
Log.i(LOG_TAG, "Creating scheduled actions table");
db.execSQL("CREATE TABLE " + ScheduledActionEntry.TABLE_NAME + " ("
+ ScheduledActionEntry._ID + " integer primary key autoincrement, "
+ ScheduledActionEntry.COLUMN_UID + " varchar(255) not null UNIQUE, "
Expand All @@ -525,7 +525,7 @@ static int upgradeDbToVersion8(SQLiteDatabase db) {


//==============================BEGIN TABLE MIGRATIONS ========================================
Log.i(DatabaseHelper.LOG_TAG, "Migrating accounts table");
Log.i(LOG_TAG, "Migrating accounts table");
// backup transaction table
db.execSQL("ALTER TABLE " + AccountEntry.TABLE_NAME + " RENAME TO " + AccountEntry.TABLE_NAME + "_bak");
// create new transaction table
Expand Down Expand Up @@ -577,7 +577,7 @@ static int upgradeDbToVersion8(SQLiteDatabase db) {
+ " FROM " + AccountEntry.TABLE_NAME + "_bak;"
);

Log.i(DatabaseHelper.LOG_TAG, "Migrating transactions table");
Log.i(LOG_TAG, "Migrating transactions table");
// backup transaction table
db.execSQL("ALTER TABLE " + TransactionEntry.TABLE_NAME + " RENAME TO " + TransactionEntry.TABLE_NAME + "_bak");
// create new transaction table
Expand Down Expand Up @@ -618,7 +618,7 @@ static int upgradeDbToVersion8(SQLiteDatabase db) {
+ " FROM " + TransactionEntry.TABLE_NAME + "_bak;"
);

Log.i(DatabaseHelper.LOG_TAG, "Migrating splits table");
Log.i(LOG_TAG, "Migrating splits table");
// backup split table
db.execSQL("ALTER TABLE " + SplitEntry.TABLE_NAME + " RENAME TO " + SplitEntry.TABLE_NAME + "_bak");
// create new split table
Expand Down Expand Up @@ -668,7 +668,7 @@ static int upgradeDbToVersion8(SQLiteDatabase db) {
//TransactionsDbAdapter transactionsDbAdapter = new TransactionsDbAdapter(db, splitsDbAdapter);
//AccountsDbAdapter accountsDbAdapter = new AccountsDbAdapter(db,transactionsDbAdapter);

Log.i(DatabaseHelper.LOG_TAG, "Creating default root account if none exists");
Log.i(LOG_TAG, "Creating default root account if none exists");
ContentValues contentValues = new ContentValues();
//assign a root account to all accounts which had null as parent except ROOT (top-level accounts)
String rootAccountUID;
Expand Down Expand Up @@ -706,7 +706,7 @@ static int upgradeDbToVersion8(SQLiteDatabase db) {
contentValues.put(AccountEntry.COLUMN_PARENT_ACCOUNT_UID, rootAccountUID);
db.update(AccountEntry.TABLE_NAME, contentValues, AccountEntry.COLUMN_PARENT_ACCOUNT_UID + " IS NULL AND " + AccountEntry.COLUMN_TYPE + " != ?", new String[]{"ROOT"});

Log.i(DatabaseHelper.LOG_TAG, "Migrating existing recurring transactions");
Log.i(LOG_TAG, "Migrating existing recurring transactions");
cursor = db.query(TransactionEntry.TABLE_NAME + "_bak", null, "recurrence_period > 0", null, null, null, null);
long lastRun = System.currentTimeMillis();
while (cursor.moveToNext()){
Expand Down Expand Up @@ -753,7 +753,7 @@ static int upgradeDbToVersion8(SQLiteDatabase db) {
cursor.close();

//auto-balance existing splits
Log.i(DatabaseHelper.LOG_TAG, "Auto-balancing existing transaction splits");
Log.i(LOG_TAG, "Auto-balancing existing transaction splits");
cursor = db.query(
TransactionEntry.TABLE_NAME + " , " + SplitEntry.TABLE_NAME + " ON "
+ TransactionEntry.TABLE_NAME + "." + TransactionEntry.COLUMN_UID + "=" + SplitEntry.TABLE_NAME + "." + SplitEntry.COLUMN_TRANSACTION_UID
Expand Down Expand Up @@ -830,7 +830,7 @@ static int upgradeDbToVersion8(SQLiteDatabase db) {
cursor.close();
}

Log.i(DatabaseHelper.LOG_TAG, "Dropping temporary migration tables");
Log.i(LOG_TAG, "Dropping temporary migration tables");
db.execSQL("DROP TABLE " + SplitEntry.TABLE_NAME + "_bak");
db.execSQL("DROP TABLE " + AccountEntry.TABLE_NAME + "_bak");
db.execSQL("DROP TABLE " + TransactionEntry.TABLE_NAME + "_bak");
Expand Down Expand Up @@ -861,7 +861,7 @@ static int upgradeDbToVersion8(SQLiteDatabase db) {
* @throws RuntimeException if the default commodities could not be imported
*/
static int upgradeDbToVersion9(SQLiteDatabase db){
Log.i(DatabaseHelper.LOG_TAG, "Upgrading database to version 9");
Log.i(LOG_TAG, "Upgrading database to version 9");
int oldVersion = 8;

db.beginTransaction();
Expand All @@ -885,7 +885,7 @@ static int upgradeDbToVersion9(SQLiteDatabase db){
try {
importCommodities(db);
} catch (SAXException | ParserConfigurationException | IOException e) {
Log.e(DatabaseHelper.LOG_TAG, "Error loading currencies into the database", e);
Log.e(LOG_TAG, "Error loading currencies into the database", e);
Crashlytics.logException(e);
throw new RuntimeException(e);
}
Expand Down Expand Up @@ -1092,7 +1092,7 @@ static int upgradeDbToVersion9(SQLiteDatabase db){
* @return 10 if upgrade was successful, 9 otherwise
*/
static int upgradeDbToVersion10(SQLiteDatabase db){
Log.i(DatabaseHelper.LOG_TAG, "Upgrading database to version 9");
Log.i(LOG_TAG, "Upgrading database to version 9");
int oldVersion = 9;

db.beginTransaction();
Expand Down Expand Up @@ -1145,7 +1145,7 @@ static int upgradeDbToVersion10(SQLiteDatabase db){
* @return 11 if upgrade was successful, 10 otherwise
*/
static int upgradeDbToVersion11(SQLiteDatabase db){
Log.i(DatabaseHelper.LOG_TAG, "Upgrading database to version 9");
Log.i(LOG_TAG, "Upgrading database to version 9");
int oldVersion = 10;

db.beginTransaction();
Expand Down Expand Up @@ -1241,7 +1241,7 @@ static int upgradeDbToVersion12(SQLiteDatabase db){
* @return New database version, 13 if migration succeeds, 11 otherwise
*/
static int upgradeDbToVersion13(SQLiteDatabase db){
Log.i(DatabaseHelper.LOG_TAG, "Upgrading database to version 13");
Log.i(LOG_TAG, "Upgrading database to version 13");
int oldVersion = 12;

db.beginTransaction();
Expand Down Expand Up @@ -1552,7 +1552,7 @@ private static void moveDirectory(File srcDir, File dstDir) throws IOException {
* @return New database version
*/
public static int upgradeDbToVersion14(SQLiteDatabase db){
Log.i(DatabaseHelper.LOG_TAG, "Upgrading database to version 14");
Log.i(LOG_TAG, "Upgrading database to version 14");
int oldDbVersion = 13;
File backupFolder = new File(Exporter.BASE_FOLDER_PATH);
backupFolder.mkdir();
Expand Down Expand Up @@ -1585,18 +1585,18 @@ public void run() {
}

/**
* Upgrades the database to version 14.
* Upgrades the database to version 15.
* <p>This migration makes the following changes to the database:
* <ul>
* <li>Fixes accounts referencing a default transfer account that no longer
* exists (see #654)</li>
* </ul>
* </p>
* @param db SQLite database to be upgraded
* @return New database version, 14 if migration succeeds, 13 otherwise
* @return New database version, 15 if migration succeeds, 14 otherwise
*/
static int upgradeDbToVersion15(SQLiteDatabase db) {
Log.i(DatabaseHelper.LOG_TAG, "Upgrading database to version 15");
Log.i(LOG_TAG, "Upgrading database to version 15");
int dbVersion = 14;

db.beginTransaction();
Expand Down Expand Up @@ -1628,4 +1628,29 @@ static int upgradeDbToVersion15(SQLiteDatabase db) {
rescheduleServiceAlarm();
return dbVersion;
}

/**
* Upgrades the database to version 16.
* <p>This migration makes the following changes to the database:
* <ul>
* <li>Re-populate the commodities table (see #731)</li>
* </ul>
* </p>
* @param db SQLite database to be upgraded
* @return New database version, 16 if migration succeeds, 15 otherwise
*/
static int upgradeDbToVersion16(SQLiteDatabase db) {
Log.i(LOG_TAG, "Upgrading database to version 16");
int dbVersion = 15;

try {
importCommodities(db);
dbVersion = 16;
} catch (SAXException | ParserConfigurationException | IOException e) {
Log.e(LOG_TAG, "Error loading currencies into the database", e);
Crashlytics.logException(e);
}

return dbVersion;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ public void startElement(String uri, String localName, String qName, Attributes

@Override
public void endDocument() throws SAXException {
mCommoditiesDbAdapter.deleteAllRecords();
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What is the purpose of this call? Aren't we always importing into a new book anyway? (which means a fresh database)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

no, an existing book that will have its database "upgraded" means that the table is already populated with commodities, so we need to delete the old ones and populate from the latest xml.

mCommoditiesDbAdapter.bulkAddRecords(mCommodities, DatabaseAdapter.UpdateMethod.insert);
}
}
23 changes: 19 additions & 4 deletions app/src/main/res/raw/iso_4217_currencies.xml
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
Sort order by ISO codes for simpler maintainance


File source: https://github.com/Gnucash/gnucash/blob/master/src/engine/iso-4217-currencies.xml
File source: https://github.com/Gnucash/gnucash/blob/master/libgnucash/engine/iso-4217-currencies.xml
-->


Expand Down Expand Up @@ -1774,7 +1774,7 @@
exchange-code="480"
parts-per-unit="100"
smallest-fraction="100"
local-symbol="R"
local-symbol=""
/>
<!-- "MVR" - "Rufiyaa"
-->
Expand Down Expand Up @@ -2161,7 +2161,7 @@
exchange-code="643"
parts-per-unit="100"
smallest-fraction="100"
local-symbol="руб"
local-symbol=""
/>
<!-- "RWF" - "Rwanda Franc"
-->
Expand Down Expand Up @@ -2981,7 +2981,7 @@
exchange-code="nil"
parts-per-unit="1000000"
smallest-fraction="1000000"
local-symbol="XBT"
local-symbol=""
/>

<!-- precious metals -->
Expand Down Expand Up @@ -3037,4 +3037,19 @@
smallest-fraction="1000000"
local-symbol=""
/>
<!-- "XSU" - "SUCRE"
Alianza Bolivariana para los Pueblos de Nuestra América - Tratado de Comercio de los Pueblos
http://www.currency-iso.org/dam/isocy/downloads/dl_currency_iso_amendment_148.pdf
-->
<currency
isocode="XSU"
fullname="Sistema Unitario de Compensación Regional"
unitname="SUCRE"
partname=""
namespace="ISO4217"
exchange-code="994"
parts-per-unit="1"
smallest-fraction="1"
local-symbol=""
/>
</currencylist>