Skip to content

Commit

Permalink
fix: Migration issue (#135)
Browse files Browse the repository at this point in the history
* chore: move db creation to AppDatabase

* fix: revert 2.json schema

* test: add single migration tests

* chore: fix detekt issues
  • Loading branch information
mslalith committed Apr 12, 2023
1 parent cf7f96c commit 4947edf
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 137 deletions.
Expand Up @@ -2,7 +2,7 @@
"formatVersion": 1,
"database": {
"version": 2,
"identityHash": "78baa3672dce208785de919cf7f4ef51",
"identityHash": "787cac516d504d296f8716b5ae5b3a8c",
"entities": [
{
"tableName": "apps",
Expand Down Expand Up @@ -212,140 +212,12 @@
},
"indices": [],
"foreignKeys": []
},
{
"tableName": "places",
"createSql": "CREATE TABLE IF NOT EXISTS `${TABLE_NAME}` (`id` INTEGER NOT NULL, `license` TEXT NOT NULL, `osmType` TEXT NOT NULL, `osmId` INTEGER NOT NULL, `latitude` TEXT NOT NULL, `longitude` TEXT NOT NULL, `displayName` TEXT NOT NULL, `boundingBox` TEXT NOT NULL, `houseNumber` TEXT, `road` TEXT, `village` TEXT, `suburb` TEXT, `postCode` TEXT, `county` TEXT, `stateDistrict` TEXT, `state` TEXT, `iso3166` TEXT, `country` TEXT, `countryCode` TEXT, PRIMARY KEY(`id`))",
"fields": [
{
"fieldPath": "id",
"columnName": "id",
"affinity": "INTEGER",
"notNull": true
},
{
"fieldPath": "license",
"columnName": "license",
"affinity": "TEXT",
"notNull": true
},
{
"fieldPath": "osmType",
"columnName": "osmType",
"affinity": "TEXT",
"notNull": true
},
{
"fieldPath": "osmId",
"columnName": "osmId",
"affinity": "INTEGER",
"notNull": true
},
{
"fieldPath": "latitude",
"columnName": "latitude",
"affinity": "TEXT",
"notNull": true
},
{
"fieldPath": "longitude",
"columnName": "longitude",
"affinity": "TEXT",
"notNull": true
},
{
"fieldPath": "displayName",
"columnName": "displayName",
"affinity": "TEXT",
"notNull": true
},
{
"fieldPath": "boundingBox",
"columnName": "boundingBox",
"affinity": "TEXT",
"notNull": true
},
{
"fieldPath": "address.houseNumber",
"columnName": "houseNumber",
"affinity": "TEXT",
"notNull": false
},
{
"fieldPath": "address.road",
"columnName": "road",
"affinity": "TEXT",
"notNull": false
},
{
"fieldPath": "address.village",
"columnName": "village",
"affinity": "TEXT",
"notNull": false
},
{
"fieldPath": "address.suburb",
"columnName": "suburb",
"affinity": "TEXT",
"notNull": false
},
{
"fieldPath": "address.postCode",
"columnName": "postCode",
"affinity": "TEXT",
"notNull": false
},
{
"fieldPath": "address.county",
"columnName": "county",
"affinity": "TEXT",
"notNull": false
},
{
"fieldPath": "address.stateDistrict",
"columnName": "stateDistrict",
"affinity": "TEXT",
"notNull": false
},
{
"fieldPath": "address.state",
"columnName": "state",
"affinity": "TEXT",
"notNull": false
},
{
"fieldPath": "address.iso3166",
"columnName": "iso3166",
"affinity": "TEXT",
"notNull": false
},
{
"fieldPath": "address.country",
"columnName": "country",
"affinity": "TEXT",
"notNull": false
},
{
"fieldPath": "address.countryCode",
"columnName": "countryCode",
"affinity": "TEXT",
"notNull": false
}
],
"primaryKey": {
"autoGenerate": false,
"columnNames": [
"id"
]
},
"indices": [],
"foreignKeys": []
}
],
"views": [],
"setupQueries": [
"CREATE TABLE IF NOT EXISTS room_master_table (id INTEGER PRIMARY KEY,identity_hash TEXT)",
"INSERT OR REPLACE INTO room_master_table (id,identity_hash) VALUES(42, '78baa3672dce208785de919cf7f4ef51')"
"INSERT OR REPLACE INTO room_master_table (id,identity_hash) VALUES(42, '787cac516d504d296f8716b5ae5b3a8c')"
]
}
}
@@ -1,8 +1,10 @@
package dev.mslalith.focuslauncher.core.data.database

import android.content.Context
import androidx.room.AutoMigration
import androidx.room.Database
import androidx.room.DeleteTable
import androidx.room.Room
import androidx.room.RoomDatabase
import androidx.room.TypeConverters
import androidx.room.migration.AutoMigrationSpec
Expand All @@ -17,6 +19,7 @@ import dev.mslalith.focuslauncher.core.data.database.entities.HiddenAppRoom
import dev.mslalith.focuslauncher.core.data.database.entities.PlaceRoom
import dev.mslalith.focuslauncher.core.data.database.entities.QuoteRoom
import dev.mslalith.focuslauncher.core.data.database.typeconverter.Converters
import dev.mslalith.focuslauncher.core.data.utils.Constants

@Database(
entities = [
Expand All @@ -41,6 +44,14 @@ internal abstract class AppDatabase : RoomDatabase() {
abstract fun hiddenAppsDao(): HiddenAppsDao
abstract fun quotesDao(): QuotesDao
abstract fun placesDao(): PlacesDao

companion object {
internal fun build(context: Context): AppDatabase = Room.databaseBuilder(
context,
AppDatabase::class.java,
Constants.Database.APP_DB_NAME
).build()
}
}

@DeleteTable(tableName = "cities")
Expand Down
@@ -1,7 +1,6 @@
package dev.mslalith.focuslauncher.core.data.di.modules

import android.content.Context
import androidx.room.Room
import dagger.Module
import dagger.Provides
import dagger.hilt.InstallIn
Expand All @@ -14,7 +13,6 @@ import dev.mslalith.focuslauncher.core.data.database.dao.HiddenAppsDao
import dev.mslalith.focuslauncher.core.data.database.dao.PlacesDao
import dev.mslalith.focuslauncher.core.data.database.dao.QuotesDao
import dev.mslalith.focuslauncher.core.data.database.utils.CloseDatabase
import dev.mslalith.focuslauncher.core.data.utils.Constants.Database.APP_DB_NAME
import javax.inject.Singleton

@Module
Expand All @@ -23,11 +21,7 @@ internal object RoomModule {

@Provides
@Singleton
fun provideRoomDatabase(@ApplicationContext context: Context): AppDatabase = Room.databaseBuilder(
context,
AppDatabase::class.java,
APP_DB_NAME
).build()
fun provideRoomDatabase(@ApplicationContext context: Context): AppDatabase = AppDatabase.build(context = context)

@Provides
@Singleton
Expand Down
Expand Up @@ -2,8 +2,10 @@ package dev.mslalith.focuslauncher.core.data.database

import androidx.room.Room
import androidx.room.testing.MigrationTestHelper
import androidx.sqlite.db.SupportSQLiteDatabase
import androidx.sqlite.db.framework.FrameworkSQLiteOpenHelperFactory
import androidx.test.platform.app.InstrumentationRegistry
import com.google.common.truth.Truth.assertThat
import org.junit.Rule
import org.junit.Test
import org.junit.runner.RunWith
Expand Down Expand Up @@ -37,4 +39,57 @@ class MigrationTest {
openHelper.writableDatabase.close()
}
}

@Test
fun migrateFrom1To2() {
helper.createDatabase(testDbName, 1).apply {
execSQL(
"""INSERT OR REPLACE INTO cities (
id, name, stateId, stateCode, stateName, countryId, countryCode, countryName, latitude, longitude, wikiDataId) VALUES(
42, 'name', 23, 'stateCode', 'stateName', 34, 'countryCode', 'countryName', '10.234', '43.123', 'wikiDataId')
""".trimIndent()
)
close()
}

helper.runMigrationsAndValidate(testDbName, 2, true).use { db ->
db.query("SELECT * FROM cities").use {
assertThat(it.count).isEqualTo(1)
it.moveToFirst()

val id = it.getInt(it.getColumnIndex("id"))
val name = it.getString(it.getColumnIndex("name"))
val countryId = it.getInt(it.getColumnIndex("countryId"))

assertThat(id).isEqualTo(42)
assertThat(name).isEqualTo("name")
assertThat(countryId).isEqualTo(34)
}
}
}

@Test
fun migrateFrom2To3() {
helper.createDatabase(testDbName, 2).apply {
execSQL(
"""INSERT OR REPLACE INTO cities (
id, name, stateId, stateCode, stateName, countryId, countryCode, countryName, latitude, longitude, wikiDataId) VALUES(
42, 'name', 23, 'stateCode', 'stateName', 34, 'countryCode', 'countryName', '10.234', '43.123', 'wikiDataId')
""".trimIndent()
)
close()
}

helper.runMigrationsAndValidate(testDbName, 3, true).use { db ->
assertThat(db.hasTable(tableName = "cities")).isFalse()
assertThat(db.hasTable(tableName = "places")).isTrue()
}
}
}

private fun SupportSQLiteDatabase.hasTable(tableName: String): Boolean = query(
query = "SELECT COUNT(*) FROM sqlite_master WHERE type='table' AND name='$tableName'"
).use { cursor ->
cursor.moveToFirst()
cursor.getInt(0) > 0
}

0 comments on commit 4947edf

Please sign in to comment.