Skip to content

Commit 1ff6c44

Browse files
committed
Fix migration, release 3.1.1 (29)
1 parent 3514863 commit 1ff6c44

File tree

7 files changed

+165
-8
lines changed

7 files changed

+165
-8
lines changed

farebot-app-persist/build.gradle

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,4 +14,12 @@ dependencies {
1414
implementation project(path: ':farebot-card')
1515
}
1616

17-
android { }
17+
android {
18+
defaultConfig {
19+
kapt {
20+
arguments {
21+
arg("room.schemaLocation", "$projectDir/schemas".toString())
22+
}
23+
}
24+
}
25+
}
Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
{
2+
"formatVersion": 1,
3+
"database": {
4+
"version": 3,
5+
"identityHash": "e0f1c435e9541ad1c6b71d92adbd509d",
6+
"entities": [
7+
{
8+
"tableName": "cards",
9+
"createSql": "CREATE TABLE IF NOT EXISTS `${TABLE_NAME}` (`id` INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, `type` TEXT NOT NULL, `serial` TEXT NOT NULL, `data` TEXT NOT NULL, `scanned_at` INTEGER NOT NULL)",
10+
"fields": [
11+
{
12+
"fieldPath": "id",
13+
"columnName": "id",
14+
"affinity": "INTEGER",
15+
"notNull": true
16+
},
17+
{
18+
"fieldPath": "type",
19+
"columnName": "type",
20+
"affinity": "TEXT",
21+
"notNull": true
22+
},
23+
{
24+
"fieldPath": "serial",
25+
"columnName": "serial",
26+
"affinity": "TEXT",
27+
"notNull": true
28+
},
29+
{
30+
"fieldPath": "data",
31+
"columnName": "data",
32+
"affinity": "TEXT",
33+
"notNull": true
34+
},
35+
{
36+
"fieldPath": "scannedAt",
37+
"columnName": "scanned_at",
38+
"affinity": "INTEGER",
39+
"notNull": true
40+
}
41+
],
42+
"primaryKey": {
43+
"columnNames": [
44+
"id"
45+
],
46+
"autoGenerate": true
47+
},
48+
"indices": [],
49+
"foreignKeys": []
50+
},
51+
{
52+
"tableName": "keys",
53+
"createSql": "CREATE TABLE IF NOT EXISTS `${TABLE_NAME}` (`id` INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, `card_id` TEXT NOT NULL, `card_type` TEXT NOT NULL, `key_data` TEXT NOT NULL, `created_at` INTEGER NOT NULL)",
54+
"fields": [
55+
{
56+
"fieldPath": "id",
57+
"columnName": "id",
58+
"affinity": "INTEGER",
59+
"notNull": true
60+
},
61+
{
62+
"fieldPath": "cardId",
63+
"columnName": "card_id",
64+
"affinity": "TEXT",
65+
"notNull": true
66+
},
67+
{
68+
"fieldPath": "cardType",
69+
"columnName": "card_type",
70+
"affinity": "TEXT",
71+
"notNull": true
72+
},
73+
{
74+
"fieldPath": "keyData",
75+
"columnName": "key_data",
76+
"affinity": "TEXT",
77+
"notNull": true
78+
},
79+
{
80+
"fieldPath": "createdAt",
81+
"columnName": "created_at",
82+
"affinity": "INTEGER",
83+
"notNull": true
84+
}
85+
],
86+
"primaryKey": {
87+
"columnNames": [
88+
"id"
89+
],
90+
"autoGenerate": true
91+
},
92+
"indices": [],
93+
"foreignKeys": []
94+
}
95+
],
96+
"views": [],
97+
"setupQueries": [
98+
"CREATE TABLE IF NOT EXISTS room_master_table (id INTEGER PRIMARY KEY,identity_hash TEXT)",
99+
"INSERT OR REPLACE INTO room_master_table (id,identity_hash) VALUES(42, \"e0f1c435e9541ad1c6b71d92adbd509d\")"
100+
]
101+
}
102+
}

farebot-app-persist/src/main/java/com/codebutler/farebot/persist/db/FareBotDb.kt

Lines changed: 48 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ import com.codebutler.farebot.persist.db.model.SavedKey
1414

1515
private const val DATABASE_NAME = "farebot.db"
1616

17-
@Database(entities = [SavedCard::class, SavedKey::class], version = 2, exportSchema = false)
17+
@Database(entities = [SavedCard::class, SavedKey::class], version = 3, exportSchema = true)
1818
@TypeConverters(FareBotDbConverters::class)
1919
abstract class FareBotDb : RoomDatabase() {
2020
abstract fun savedCardDao(): SavedCardDao
@@ -34,6 +34,53 @@ abstract class FareBotDb : RoomDatabase() {
3434
// Migration from Sqldelight to Room. Nothing to change.
3535
}
3636
})
37+
.addMigrations(object : Migration(2, 3) {
38+
override fun migrate(database: SupportSQLiteDatabase) {
39+
// Re-create tables with new NOT NULL `id` column.
40+
database.beginTransaction()
41+
try {
42+
database.execSQL("""
43+
CREATE TABLE `cards_new` (
44+
`id` INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,
45+
`type` TEXT NOT NULL,
46+
`serial` TEXT NOT NULL,
47+
`data` TEXT NOT NULL,
48+
`scanned_at` INTEGER NOT NULL
49+
);
50+
""".trimIndent())
51+
52+
database.execSQL("""
53+
CREATE TABLE `keys_new` (
54+
`id` INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,
55+
`card_id` TEXT NOT NULL,
56+
`card_type` TEXT NOT NULL,
57+
`key_data` TEXT NOT NULL,
58+
`created_at` INTEGER NOT NULL
59+
);
60+
""".trimIndent())
61+
62+
database.execSQL("""
63+
INSERT INTO `cards_new` (type, serial, data, scanned_at)
64+
SELECT type, serial, data, scanned_at FROM cards;
65+
""".trimIndent())
66+
67+
database.execSQL("""
68+
INSERT INTO `keys_new` (card_id, card_type, key_data, created_at)
69+
SELECT card_id, card_type, key_data, created_at FROM keys;
70+
""".trimIndent())
71+
72+
database.execSQL("DROP TABLE `cards`;")
73+
database.execSQL("DROP TABLE `keys`;")
74+
75+
database.execSQL("ALTER TABLE `cards_new` RENAME TO `cards`;")
76+
database.execSQL("ALTER TABLE `keys_new` RENAME TO `keys`;")
77+
78+
database.setTransactionSuccessful()
79+
} finally {
80+
database.endTransaction()
81+
}
82+
}
83+
})
3784
.build()
3885
}
3986
}

farebot-app-persist/src/main/java/com/codebutler/farebot/persist/db/dao/SavedCardDao.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ interface SavedCardDao {
1111
@Query("SELECT * FROM cards ORDER BY scanned_at DESC")
1212
fun selectAll(): List<SavedCard>
1313

14-
@Query("SELECT * FROM cards WHERE _id = :id")
14+
@Query("SELECT * FROM cards WHERE id = :id")
1515
fun selectById(id: Long): SavedCard?
1616

1717
@Insert

farebot-app-persist/src/main/java/com/codebutler/farebot/persist/db/model/SavedCard.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,9 @@ import java.util.Date
88

99
@Entity(tableName = "cards")
1010
data class SavedCard(
11-
@PrimaryKey(autoGenerate = true) @ColumnInfo(name = "_id") val id: Long = 0,
11+
@PrimaryKey(autoGenerate = true) @ColumnInfo(name = "id") val id: Long = 0,
1212
@ColumnInfo(name = "type") val type: CardType,
1313
@ColumnInfo(name = "serial") val serial: String,
1414
@ColumnInfo(name = "data") val data: String,
15-
@ColumnInfo(name = "scanned_at") val scannedAt: Date? = Date()
15+
@ColumnInfo(name = "scanned_at") val scannedAt: Date = Date()
1616
)

farebot-app-persist/src/main/java/com/codebutler/farebot/persist/db/model/SavedKey.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import java.util.Date
88

99
@Entity(tableName = "keys")
1010
data class SavedKey(
11-
@PrimaryKey(autoGenerate = true) @ColumnInfo(name = "_id") val id: Long = 0,
11+
@PrimaryKey(autoGenerate = true) @ColumnInfo(name = "id") val id: Long = 0,
1212
@ColumnInfo(name = "card_id") val cardId: String,
1313
@ColumnInfo(name = "card_type") val cardType: CardType,
1414
@ColumnInfo(name = "key_data") val keyData: String,

farebot-app/build.gradle

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -99,8 +99,8 @@ kapt {
9999

100100
android {
101101
defaultConfig {
102-
versionCode 28
103-
versionName '3.1.0'
102+
versionCode 29
103+
versionName '3.1.1'
104104
multiDexEnabled true
105105
}
106106

0 commit comments

Comments
 (0)