From cf8771ac5eac021b486a2ea6dc729a78edcf6652 Mon Sep 17 00:00:00 2001 From: "Nelson R. Perez" Date: Wed, 6 Feb 2019 17:38:01 -0500 Subject: [PATCH] - Introduced a small modification to the db migration to version 3 that will take care of filling up the 'bts_value' of pre-existing entries in the 'transfer' table made with BTS - Extended the unit test for the 2-to-3 database migration to take in consideration said change --- .../bitsybitshareswallet/MigrationTest.kt | 16 +++++++++++++--- .../database/BitsyDatabase.kt | 2 +- 2 files changed, 14 insertions(+), 4 deletions(-) diff --git a/app/src/androidTest/java/cy/agorise/bitsybitshareswallet/MigrationTest.kt b/app/src/androidTest/java/cy/agorise/bitsybitshareswallet/MigrationTest.kt index 822547d..fbd7639 100644 --- a/app/src/androidTest/java/cy/agorise/bitsybitshareswallet/MigrationTest.kt +++ b/app/src/androidTest/java/cy/agorise/bitsybitshareswallet/MigrationTest.kt @@ -5,6 +5,7 @@ import androidx.sqlite.db.framework.FrameworkSQLiteOpenHelperFactory import androidx.test.ext.junit.runners.AndroidJUnit4 import androidx.test.platform.app.InstrumentationRegistry import cy.agorise.bitsybitshareswallet.database.BitsyDatabase +import org.junit.Assert import org.junit.Rule import org.junit.Test import org.junit.runner.RunWith @@ -24,17 +25,26 @@ class MigrationTest { @Test @Throws(IOException::class) fun migrate2To3() { - var db = helper.createDatabase(TEST_DB, 2).apply { + helper.createDatabase(TEST_DB, 2).apply { // db has schema version 1. insert some data using SQL queries. // You cannot use DAO classes because they expect the latest schema. execSQL("INSERT INTO assets(id, symbol, precision, description, bit_asset_id) VALUES('1.3.0','BTS', 5, '', '')") - execSQL("INSERT INTO transfers(id, block_number, timestamp, fee_amount, fee_asset_id, source, destination, transfer_amount, transfer_asset_id, memo) values(1,0,1500000000,120,'1.3.0','1.2.100','1.2.101',1000,'1.3.121','')") + // Inserting two entries in the 'transfers' table, without any 'bts_value' field. + execSQL("INSERT INTO transfers(id, block_number, timestamp, fee_amount, fee_asset_id, source, destination, transfer_amount, transfer_asset_id, memo) values('1',0,1500000000,120,'1.3.0','1.2.100','1.2.101',1000,'1.3.121','')") + execSQL("INSERT INTO transfers(id, block_number, timestamp, fee_amount, fee_asset_id, source, destination, transfer_amount, transfer_asset_id, memo) values('2',1,1500000300,120,'1.3.0','1.2.100','1.2.101',1000,'1.3.0','')") execSQL("INSERT INTO equivalent_values(id, transfer_id, value, asset_id) values(1, 1, 100, '1.3.0')") // Prepare for the next version. close() } // Re-open the database with version 2 and provide // MIGRATION_1_2 as the migration process. - db = helper.runMigrationsAndValidate(TEST_DB, 3, true, BitsyDatabase.MIGRATION_2_3) + val db = helper.runMigrationsAndValidate(TEST_DB, 3, true, BitsyDatabase.MIGRATION_2_3) + val cursor = db.query("SELECT bts_value FROM transfers WHERE id = '2'") + cursor.moveToFirst() + // Checking that the 'bts_value' of the transfer with id '2', which was a BTS transfer, now has the 'bts_value' + // column with the same value as the 'transfer_amount'. + Assert.assertEquals(1, cursor.count) + Assert.assertEquals(1, cursor.columnCount) + Assert.assertEquals(1000, cursor.getLong(0)) } } diff --git a/app/src/main/java/cy/agorise/bitsybitshareswallet/database/BitsyDatabase.kt b/app/src/main/java/cy/agorise/bitsybitshareswallet/database/BitsyDatabase.kt index e62c07e..fb12aa5 100644 --- a/app/src/main/java/cy/agorise/bitsybitshareswallet/database/BitsyDatabase.kt +++ b/app/src/main/java/cy/agorise/bitsybitshareswallet/database/BitsyDatabase.kt @@ -24,7 +24,6 @@ import cy.agorise.bitsybitshareswallet.database.joins.TransferDetailDao version = 3, exportSchema = true) abstract class BitsyDatabase : RoomDatabase() { - abstract fun assetDao(): AssetDao abstract fun authorityDao(): AuthorityDao abstract fun balanceDao(): BalanceDao @@ -70,6 +69,7 @@ abstract class BitsyDatabase : RoomDatabase() { database.execSQL("CREATE TABLE IF NOT EXISTS 'equivalent_values' ('transfer_id' TEXT NOT NULL, 'value' INTEGER NOT NULL, 'symbol' TEXT NOT NULL, PRIMARY KEY(transfer_id, symbol), FOREIGN KEY (transfer_id) REFERENCES transfers(id))") database.execSQL("ALTER TABLE transfers ADD bts_value INTEGER") + database.execSQL("UPDATE transfers SET bts_value = transfer_amount WHERE transfer_asset_id = '1.3.0'") database.execSQL("DROP TABLE assets") database.execSQL("CREATE TABLE IF NOT EXISTS assets (`id` TEXT NOT NULL, `symbol` TEXT NOT NULL, `precision` INTEGER NOT NULL, `description` TEXT NOT NULL, `issuer` TEXT NOT NULL, PRIMARY KEY(`id`))")