Skip to content

Commit d391782

Browse files
authored
Merge pull request #116 from gatheringhallstudios/migrations
Fix missing database migrations!
2 parents 46f08b2 + 040091c commit d391782

File tree

16 files changed

+380
-70
lines changed

16 files changed

+380
-70
lines changed

app/build.gradle

Lines changed: 22 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,18 @@ android {
2929

3030
// We set this to false so vectors get rastered at buildtime.
3131
vectorDrawables.useSupportLibrary = false
32+
33+
// Export schemas for databases (important for testing)
34+
javaCompileOptions {
35+
annotationProcessorOptions {
36+
arguments = ["room.schemaLocation": "$projectDir/schemas".toString()]
37+
}
38+
}
39+
40+
// Import exported schemas into tests
41+
sourceSets {
42+
androidTest.assets.srcDirs += files("$projectDir/schemas".toString())
43+
}
3244
}
3345
buildTypes {
3446
release {
@@ -85,6 +97,15 @@ dependencies {
8597
implementation "org.jetbrains.kotlinx:kotlinx-coroutines-core:1.1.1"
8698
implementation "org.jetbrains.kotlinx:kotlinx-coroutines-android:1.1.1"
8799

100+
/*
101+
* Testing
102+
*/
103+
androidTestImplementation 'androidx.test:core:1.2.0'
104+
androidTestImplementation "androidx.test:runner:1.2.0"
105+
androidTestImplementation 'androidx.test:rules:1.3.0-alpha02'
106+
androidTestImplementation "androidx.test.espresso:espresso-core:3.2.0"
107+
androidTestImplementation "androidx.test.ext:junit:1.1.1"
108+
88109
/*
89110
* Flexbox layout
90111
*/
@@ -114,6 +135,7 @@ dependencies {
114135
kapt 'androidx.room:room-compiler:2.2.0'
115136
testImplementation 'androidx.arch.core:core-testing:2.1.0'
116137
testImplementation 'androidx.room:room-testing:2.2.0'
138+
androidTestImplementation 'android.arch.persistence.room:testing:2.2.0'
117139

118140
/*
119141
* Android Navigation Component
@@ -151,14 +173,6 @@ dependencies {
151173
*/
152174
implementation 'com.sdsmdg.harjot:vectormaster:1.1.3'
153175

154-
/*
155-
* Testing
156-
*/
157-
testImplementation "junit:junit:4.12"
158-
androidTestImplementation "androidx.test:runner:1.3.0-alpha02"
159-
androidTestImplementation 'androidx.test:rules:1.3.0-alpha02'
160-
androidTestImplementation "androidx.test.espresso:espresso-core:3.3.0-alpha02"
161-
162176
/*
163177
Change log extension
164178
*/
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
{
2+
"formatVersion": 1,
3+
"database": {
4+
"version": 1,
5+
"identityHash": "b4bb8414ec55fe8289decd430bb020c6",
6+
"entities": [
7+
{
8+
"tableName": "bookmarks",
9+
"createSql": "CREATE TABLE IF NOT EXISTS `${TABLE_NAME}` (`dataId` INTEGER NOT NULL, `dataType` TEXT NOT NULL, `dateAdded` TEXT NOT NULL, PRIMARY KEY(`dataId`, `dataType`))",
10+
"fields": [
11+
{
12+
"fieldPath": "dataId",
13+
"columnName": "dataId",
14+
"affinity": "INTEGER",
15+
"notNull": true
16+
},
17+
{
18+
"fieldPath": "dataType",
19+
"columnName": "dataType",
20+
"affinity": "TEXT",
21+
"notNull": true
22+
},
23+
{
24+
"fieldPath": "dateAdded",
25+
"columnName": "dateAdded",
26+
"affinity": "TEXT",
27+
"notNull": true
28+
}
29+
],
30+
"primaryKey": {
31+
"columnNames": [
32+
"dataId",
33+
"dataType"
34+
],
35+
"autoGenerate": false
36+
},
37+
"indices": [],
38+
"foreignKeys": []
39+
}
40+
],
41+
"views": [],
42+
"setupQueries": [
43+
"CREATE TABLE IF NOT EXISTS room_master_table (id INTEGER PRIMARY KEY,identity_hash TEXT)",
44+
"INSERT OR REPLACE INTO room_master_table (id,identity_hash) VALUES(42, 'b4bb8414ec55fe8289decd430bb020c6')"
45+
]
46+
}
47+
}
Lines changed: 161 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,161 @@
1+
{
2+
"formatVersion": 1,
3+
"database": {
4+
"version": 2,
5+
"identityHash": "341a8d5be3506c4abe29d7e2231b4078",
6+
"entities": [
7+
{
8+
"tableName": "bookmarks",
9+
"createSql": "CREATE TABLE IF NOT EXISTS `${TABLE_NAME}` (`dataId` INTEGER NOT NULL, `dataType` TEXT NOT NULL, `dateAdded` TEXT NOT NULL, PRIMARY KEY(`dataId`, `dataType`))",
10+
"fields": [
11+
{
12+
"fieldPath": "dataId",
13+
"columnName": "dataId",
14+
"affinity": "INTEGER",
15+
"notNull": true
16+
},
17+
{
18+
"fieldPath": "dataType",
19+
"columnName": "dataType",
20+
"affinity": "TEXT",
21+
"notNull": true
22+
},
23+
{
24+
"fieldPath": "dateAdded",
25+
"columnName": "dateAdded",
26+
"affinity": "TEXT",
27+
"notNull": true
28+
}
29+
],
30+
"primaryKey": {
31+
"columnNames": [
32+
"dataId",
33+
"dataType"
34+
],
35+
"autoGenerate": false
36+
},
37+
"indices": [],
38+
"foreignKeys": []
39+
},
40+
{
41+
"tableName": "user_equipment_set_equipment",
42+
"createSql": "CREATE TABLE IF NOT EXISTS `${TABLE_NAME}` (`id` INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, `dataId` INTEGER NOT NULL, `dataType` TEXT NOT NULL, `equipmentSetId` INTEGER NOT NULL)",
43+
"fields": [
44+
{
45+
"fieldPath": "id",
46+
"columnName": "id",
47+
"affinity": "INTEGER",
48+
"notNull": true
49+
},
50+
{
51+
"fieldPath": "dataId",
52+
"columnName": "dataId",
53+
"affinity": "INTEGER",
54+
"notNull": true
55+
},
56+
{
57+
"fieldPath": "dataType",
58+
"columnName": "dataType",
59+
"affinity": "TEXT",
60+
"notNull": true
61+
},
62+
{
63+
"fieldPath": "equipmentSetId",
64+
"columnName": "equipmentSetId",
65+
"affinity": "INTEGER",
66+
"notNull": true
67+
}
68+
],
69+
"primaryKey": {
70+
"columnNames": [
71+
"id"
72+
],
73+
"autoGenerate": true
74+
},
75+
"indices": [],
76+
"foreignKeys": []
77+
},
78+
{
79+
"tableName": "user_equipment_sets",
80+
"createSql": "CREATE TABLE IF NOT EXISTS `${TABLE_NAME}` (`id` INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, `name` TEXT NOT NULL)",
81+
"fields": [
82+
{
83+
"fieldPath": "id",
84+
"columnName": "id",
85+
"affinity": "INTEGER",
86+
"notNull": true
87+
},
88+
{
89+
"fieldPath": "name",
90+
"columnName": "name",
91+
"affinity": "TEXT",
92+
"notNull": true
93+
}
94+
],
95+
"primaryKey": {
96+
"columnNames": [
97+
"id"
98+
],
99+
"autoGenerate": true
100+
},
101+
"indices": [],
102+
"foreignKeys": []
103+
},
104+
{
105+
"tableName": "user_equipment_set_decorations",
106+
"createSql": "CREATE TABLE IF NOT EXISTS `${TABLE_NAME}` (`id` INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, `equipmentSetId` INTEGER NOT NULL, `dataId` INTEGER NOT NULL, `dataType` TEXT NOT NULL, `decorationId` INTEGER NOT NULL, `slotNumber` INTEGER NOT NULL)",
107+
"fields": [
108+
{
109+
"fieldPath": "id",
110+
"columnName": "id",
111+
"affinity": "INTEGER",
112+
"notNull": true
113+
},
114+
{
115+
"fieldPath": "equipmentSetId",
116+
"columnName": "equipmentSetId",
117+
"affinity": "INTEGER",
118+
"notNull": true
119+
},
120+
{
121+
"fieldPath": "dataId",
122+
"columnName": "dataId",
123+
"affinity": "INTEGER",
124+
"notNull": true
125+
},
126+
{
127+
"fieldPath": "dataType",
128+
"columnName": "dataType",
129+
"affinity": "TEXT",
130+
"notNull": true
131+
},
132+
{
133+
"fieldPath": "decorationId",
134+
"columnName": "decorationId",
135+
"affinity": "INTEGER",
136+
"notNull": true
137+
},
138+
{
139+
"fieldPath": "slotNumber",
140+
"columnName": "slotNumber",
141+
"affinity": "INTEGER",
142+
"notNull": true
143+
}
144+
],
145+
"primaryKey": {
146+
"columnNames": [
147+
"id"
148+
],
149+
"autoGenerate": true
150+
},
151+
"indices": [],
152+
"foreignKeys": []
153+
}
154+
],
155+
"views": [],
156+
"setupQueries": [
157+
"CREATE TABLE IF NOT EXISTS room_master_table (id INTEGER PRIMARY KEY,identity_hash TEXT)",
158+
"INSERT OR REPLACE INTO room_master_table (id,identity_hash) VALUES(42, '341a8d5be3506c4abe29d7e2231b4078')"
159+
]
160+
}
161+
}

app/src/androidTest/java/com/gatheringhallstudios/mhworlddatabase/MHExtensionsTest.kt

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,7 @@ package com.gatheringhallstudios.mhworlddatabase
33
import android.content.Context
44
import androidx.appcompat.content.res.AppCompatResources
55
import androidx.test.rule.ActivityTestRule
6-
import androidx.test.runner.AndroidJUnit4
7-
import androidx.core.content.ContextCompat
6+
import androidx.test.ext.junit.runners.AndroidJUnit4
87
import com.gatheringhallstudios.mhworlddatabase.assets.getAssetDrawable
98
import com.gatheringhallstudios.mhworlddatabase.assets.getVectorDrawable
109
import org.junit.Rule
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package com.gatheringhallstudios.mhworlddatabase
2+
3+
import androidx.room.Room
4+
import androidx.test.ext.junit.runners.AndroidJUnit4
5+
import androidx.test.platform.app.InstrumentationRegistry
6+
import com.gatheringhallstudios.mhworlddatabase.data.AppDatabase
7+
import org.junit.Test
8+
import org.junit.runner.RunWith
9+
import androidx.room.testing.MigrationTestHelper
10+
import androidx.sqlite.db.framework.FrameworkSQLiteOpenHelperFactory
11+
import com.gatheringhallstudios.mhworlddatabase.data.AppDatabase_MIGRATIONS
12+
import org.junit.Rule
13+
14+
/**
15+
* Class used to test if database migrations are functional
16+
*/
17+
@RunWith(AndroidJUnit4::class)
18+
class MigrationTest {
19+
val helper = MigrationTestHelper(
20+
InstrumentationRegistry.getInstrumentation(),
21+
AppDatabase::class.java.canonicalName,
22+
FrameworkSQLiteOpenHelperFactory())
23+
24+
@Test
25+
fun migrateAll() {
26+
val dbName = "migration_test"
27+
28+
// create initial DB version
29+
val db = helper.createDatabase(dbName, 1)
30+
db.close()
31+
32+
val ctx = InstrumentationRegistry.getInstrumentation().targetContext
33+
val appDB = Room.databaseBuilder(
34+
ctx, AppDatabase::class.java, dbName
35+
).addMigrations(*AppDatabase_MIGRATIONS).build()
36+
37+
appDB.openHelper.writableDatabase
38+
appDB.close()
39+
}
40+
}

app/src/androidTest/java/com/gatheringhallstudios/mhworlddatabase/TestUtils.java

Lines changed: 0 additions & 22 deletions
This file was deleted.
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package com.gatheringhallstudios.mhworlddatabase
2+
3+
import androidx.test.platform.app.InstrumentationRegistry
4+
import com.gatheringhallstudios.mhworlddatabase.data.MHWDatabase
5+
6+
fun initMHWDatabase(): MHWDatabase {
7+
val ctx = InstrumentationRegistry.getInstrumentation().targetContext
8+
val db = MHWDatabase.getDatabase(ctx)
9+
return db
10+
}

app/src/androidTest/java/com/gatheringhallstudios/mhworlddatabase/dao/ArmorDaoTest.kt

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
package com.gatheringhallstudios.mhworlddatabase.dao
22

3-
import androidx.test.InstrumentationRegistry
4-
import androidx.test.runner.AndroidJUnit4
5-
3+
import androidx.test.ext.junit.runners.AndroidJUnit4
4+
import androidx.test.internal.runner.junit4.AndroidJUnit4ClassRunner
65
import com.gatheringhallstudios.mhworlddatabase.data.MHWDatabase
76
import com.gatheringhallstudios.mhworlddatabase.data.dao.ArmorDao
87
import com.gatheringhallstudios.mhworlddatabase.data.types.ArmorType
@@ -14,9 +13,9 @@ import org.junit.Test
1413
import org.junit.runner.RunWith
1514

1615
import com.gatheringhallstudios.mhworlddatabase.getResult
16+
import com.gatheringhallstudios.mhworlddatabase.initMHWDatabase
1717
import org.junit.Assert.assertEquals
1818
import org.junit.Assert.assertFalse
19-
import org.junit.Assert.assertTrue
2019

2120
@RunWith(AndroidJUnit4::class)
2221
class ArmorDaoTest {
@@ -27,8 +26,7 @@ class ArmorDaoTest {
2726
@BeforeClass @JvmStatic
2827
fun initDatabase() {
2928
// this is read only, so its ok to use the actual database
30-
val ctx = InstrumentationRegistry.getTargetContext()
31-
db = MHWDatabase.getDatabase(ctx)
29+
db = initMHWDatabase()
3230
dao = db.armorDao()
3331
}
3432

0 commit comments

Comments
 (0)