- 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
This commit is contained in:
Nelson R. Perez 2019-02-06 17:38:01 -05:00
parent 0b287ca714
commit cf8771ac5e
2 changed files with 14 additions and 4 deletions

View file

@ -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))
}
}

View file

@ -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`))")