Skip to content

Commit

Permalink
Fix schemas and migrations
Browse files Browse the repository at this point in the history
Unfortunately, we manually changed the schema JSON files manually so
there was an issue with the old schemas since the identity hashes were
different and Room was unable to apply migrations correctly.

Now everything should be fine, it was tested via migration tests and
also manually to ensure everything works now
  • Loading branch information
diego-francisco-jt committed Feb 18, 2024
1 parent 8f41cb0 commit 4373148
Show file tree
Hide file tree
Showing 6 changed files with 33 additions and 116 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@
"formatVersion": 1,
"database": {
"version": 6,
"identityHash": "b7edbbddb7b6074b82058cc10fc85bbb",
"identityHash": "0fb719da15b9a11591a0ec26f11628e6",
"entities": [
{
"tableName": "tasks",
"createSql": "CREATE TABLE IF NOT EXISTS `${TABLE_NAME}` (`id` INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, `created_at` TEXT NOT NULL, `name` TEXT NOT NULL, `date` TEXT NOT NULL, `is_done` INTEGER NOT NULL, `position` INTEGER NOT NULL DEFAULT 0, `is_recurring` INTEGER NOT NULL DEFAULT 0, `recurrence_type` TEXT, `recurrence_end_date` TEXT, `parent_id` INTEGER)",
"createSql": "CREATE TABLE IF NOT EXISTS `${TABLE_NAME}` (`id` INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, `created_at` TEXT NOT NULL, `name` TEXT NOT NULL, `date` TEXT NOT NULL, `is_done` INTEGER NOT NULL, `position` INTEGER NOT NULL DEFAULT 0)",
"fields": [
{
"fieldPath": "id",
Expand Down Expand Up @@ -44,31 +44,6 @@
"affinity": "INTEGER",
"notNull": true,
"defaultValue": "0"
},
{
"fieldPath": "isRecurring",
"columnName": "is_recurring",
"affinity": "INTEGER",
"notNull": true,
"defaultValue": "0"
},
{
"fieldPath": "recurrenceType",
"columnName": "recurrence_type",
"affinity": "TEXT",
"notNull": false
},
{
"fieldPath": "recurrenceEndDate",
"columnName": "recurrence_end_date",
"affinity": "TEXT",
"notNull": false
},
{
"fieldPath": "parentId",
"columnName": "parent_id",
"affinity": "INTEGER",
"notNull": false
}
],
"primaryKey": {
Expand Down Expand Up @@ -96,24 +71,6 @@
],
"orders": [],
"createSql": "CREATE UNIQUE INDEX IF NOT EXISTS `index_tasks_position_date` ON `${TABLE_NAME}` (`position`, `date`)"
},
{
"name": "index_tasks_date",
"unique": false,
"columnNames": [
"date"
],
"orders": [],
"createSql": "CREATE INDEX IF NOT EXISTS `index_tasks_date` ON `${TABLE_NAME}` (`date`)"
},
{
"name": "index_tasks_parent_id",
"unique": false,
"columnNames": [
"parent_id"
],
"orders": [],
"createSql": "CREATE INDEX IF NOT EXISTS `index_tasks_parent_id` ON `${TABLE_NAME}` (`parent_id`)"
}
],
"foreignKeys": []
Expand Down Expand Up @@ -160,7 +117,7 @@
"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, 'b7edbbddb7b6074b82058cc10fc85bbb')"
"INSERT OR REPLACE INTO room_master_table (id,identity_hash) VALUES(42, '0fb719da15b9a11591a0ec26f11628e6')"
]
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@
"formatVersion": 1,
"database": {
"version": 6,
"identityHash": "b7edbbddb7b6074b82058cc10fc85bbb",
"identityHash": "0fb719da15b9a11591a0ec26f11628e6",
"entities": [
{
"tableName": "tasks",
"createSql": "CREATE TABLE IF NOT EXISTS `${TABLE_NAME}` (`id` INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, `created_at` TEXT NOT NULL, `name` TEXT NOT NULL, `date` TEXT NOT NULL, `is_done` INTEGER NOT NULL, `position` INTEGER NOT NULL DEFAULT 0, `is_recurring` INTEGER NOT NULL DEFAULT 0, `recurrence_type` TEXT, `recurrence_end_date` TEXT, `parent_id` INTEGER)",
"createSql": "CREATE TABLE IF NOT EXISTS `${TABLE_NAME}` (`id` INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, `created_at` TEXT NOT NULL, `name` TEXT NOT NULL, `date` TEXT NOT NULL, `is_done` INTEGER NOT NULL, `position` INTEGER NOT NULL DEFAULT 0)",
"fields": [
{
"fieldPath": "id",
Expand Down Expand Up @@ -44,31 +44,6 @@
"affinity": "INTEGER",
"notNull": true,
"defaultValue": "0"
},
{
"fieldPath": "isRecurring",
"columnName": "is_recurring",
"affinity": "INTEGER",
"notNull": true,
"defaultValue": "0"
},
{
"fieldPath": "recurrenceType",
"columnName": "recurrence_type",
"affinity": "TEXT",
"notNull": false
},
{
"fieldPath": "recurrenceEndDate",
"columnName": "recurrence_end_date",
"affinity": "TEXT",
"notNull": false
},
{
"fieldPath": "parentId",
"columnName": "parent_id",
"affinity": "INTEGER",
"notNull": false
}
],
"primaryKey": {
Expand Down Expand Up @@ -96,24 +71,6 @@
],
"orders": [],
"createSql": "CREATE UNIQUE INDEX IF NOT EXISTS `index_tasks_position_date` ON `${TABLE_NAME}` (`position`, `date`)"
},
{
"name": "index_tasks_date",
"unique": false,
"columnNames": [
"date"
],
"orders": [],
"createSql": "CREATE INDEX IF NOT EXISTS `index_tasks_date` ON `${TABLE_NAME}` (`date`)"
},
{
"name": "index_tasks_parent_id",
"unique": false,
"columnNames": [
"parent_id"
],
"orders": [],
"createSql": "CREATE INDEX IF NOT EXISTS `index_tasks_parent_id` ON `${TABLE_NAME}` (`parent_id`)"
}
],
"foreignKeys": []
Expand Down Expand Up @@ -160,7 +117,7 @@
"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, 'b7edbbddb7b6074b82058cc10fc85bbb')"
"INSERT OR REPLACE INTO room_master_table (id,identity_hash) VALUES(42, '0fb719da15b9a11591a0ec26f11628e6')"
]
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import org.junit.runner.RunWith

@RunWith(AndroidJUnit4::class)
class MigrationsTest {
private val ALL_MIGRATIONS = arrayOf(MIGRATION_4_5, MIGRATION_6_7)
private val ALL_MIGRATIONS = arrayOf(MIGRATION_4_5, MIGRATION_5_6)
private val TEST_DB = "migration-test"
private val LATEST_VERSION = 7

Expand Down Expand Up @@ -81,7 +81,7 @@ class MigrationsTest {

@Test
@Throws(IOException::class)
fun testMigration5To6_reminders() {
fun testMigration5to6() {
helper.createDatabase(TEST_DB, 5).use { db ->
db.execSQL(
"INSERT INTO tasks (created_at, name, date, is_done) VALUES " +
Expand All @@ -96,7 +96,8 @@ class MigrationsTest {
helper.runMigrationsAndValidate(
name = TEST_DB,
version = 6,
validateDroppedTables = true
validateDroppedTables = true,
migrations = arrayOf(MIGRATION_5_6),
).use { db ->
db.query("SELECT * FROM reminders", arrayOf()).use { cursor ->
if (cursor.moveToFirst()) {
Expand All @@ -119,8 +120,8 @@ class MigrationsTest {

@Test
@Throws(IOException::class)
fun testMigration5To6_tasks() {
helper.createDatabase(TEST_DB, 5).use { db ->
fun testMigration6to7() {
helper.createDatabase(TEST_DB, 6).use { db ->
db.execSQL(
"INSERT INTO tasks (created_at, name, date, is_done) VALUES " +
"('2023-08-23', 'This is a test', '2023-08-23', 0); "
Expand All @@ -129,7 +130,7 @@ class MigrationsTest {

helper.runMigrationsAndValidate(
name = TEST_DB,
version = 6,
version = 7,
validateDroppedTables = true
).use { db ->
db.query("SELECT * FROM tasks", arrayOf()).use { cursor ->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@ import com.costular.atomtasks.data.tasks.TasksDao
exportSchema = true,
autoMigrations = [
AutoMigration(
from = 5,
to = 6,
spec = MigrationDeleteIsEnabledReminder::class,
from = 6,
to = 7,
spec = Migration6To7Spec::class
),
]
)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,44 +1,46 @@
package com.costular.atomtasks.data.database

import androidx.room.DeleteColumn
import androidx.room.migration.AutoMigrationSpec
import androidx.room.migration.Migration
import androidx.sqlite.db.SupportSQLiteDatabase

val MIGRATION_4_5 = object : Migration(4, 5) {
override fun migrate(database: SupportSQLiteDatabase) {
database.execSQL(
override fun migrate(db: SupportSQLiteDatabase) {
db.execSQL(
"CREATE TABLE IF NOT EXISTS `_new_tasks` (`id` INTEGER PRIMARY KEY" +
" AUTOINCREMENT NOT NULL, `created_at` TEXT NOT NULL, `name` TEXT NOT NULL," +
" `date` TEXT NOT NULL, `is_done` INTEGER NOT NULL, `position`" +
" INTEGER NOT NULL DEFAULT 0)",
)
database.execSQL(
db.execSQL(
"INSERT INTO `_new_tasks` (`id`,`created_at`,`name`,`date`," +
"`is_done`) SELECT `id`,`created_at`,`name`,`date`,`is_done` FROM `tasks`",
)
database.execSQL("DROP TABLE `tasks`")
database.execSQL("ALTER TABLE `_new_tasks` RENAME TO `tasks`")
database.execSQL(
db.execSQL("DROP TABLE `tasks`")
db.execSQL("ALTER TABLE `_new_tasks` RENAME TO `tasks`")
db.execSQL(
"UPDATE tasks SET position = (SELECT COUNT ( *) FROM tasks " +
"AS t WHERE t.ID <= tasks.ID);",
)
database.execSQL(
db.execSQL(
"CREATE INDEX IF NOT EXISTS `index_tasks_created_at` " +
"ON `tasks` (`created_at`)",
)
database.execSQL(
db.execSQL(
"CREATE UNIQUE INDEX IF NOT EXISTS `index_tasks_position_date` " +
"ON `tasks` (`position`, `date`)",
)
}
}

val MIGRATION_6_7 = object : Migration(6, 7) {
val MIGRATION_5_6 = object : Migration(5, 6) {
override fun migrate(db: SupportSQLiteDatabase) {
db.execSQL("DROP INDEX IF EXISTS `index_tasks_created_at`")
db.execSQL("ALTER TABLE reminders DROP COLUMN is_enabled;")
}
}

@DeleteColumn(tableName = "reminders", columnName = "is_enabled")
internal class MigrationDeleteIsEnabledReminder : AutoMigrationSpec
class Migration6To7Spec : AutoMigrationSpec {
override fun onPostMigrate(db: SupportSQLiteDatabase) {
db.execSQL("DROP INDEX IF EXISTS `index_tasks_created_at`;")
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ abstract class DatabaseModule {
Room.databaseBuilder(context, AtomTasksDatabase::class.java, "atomtasks.db")
.addMigrations(
MIGRATION_4_5,
MIGRATION_6_7,
MIGRATION_5_6,
)
.build()
}
Expand Down

0 comments on commit 4373148

Please sign in to comment.