diff --git a/data/schemas/com.costular.atomtasks.data.database.AtomTasksDatabase/6.json b/data/schemas/com.costular.atomtasks.data.database.AtomTasksDatabase/6.json index 50c8d126..d74d7ec7 100644 --- a/data/schemas/com.costular.atomtasks.data.database.AtomTasksDatabase/6.json +++ b/data/schemas/com.costular.atomtasks.data.database.AtomTasksDatabase/6.json @@ -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", @@ -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": { @@ -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": [] @@ -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')" ] } -} \ No newline at end of file +} diff --git a/data/src/androidTest/assets/com.costular.atomtasks.data.database.AtomTasksDatabase/6.json b/data/src/androidTest/assets/com.costular.atomtasks.data.database.AtomTasksDatabase/6.json index 0af51bc3..d74d7ec7 100644 --- a/data/src/androidTest/assets/com.costular.atomtasks.data.database.AtomTasksDatabase/6.json +++ b/data/src/androidTest/assets/com.costular.atomtasks.data.database.AtomTasksDatabase/6.json @@ -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", @@ -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": { @@ -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": [] @@ -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')" ] } } diff --git a/data/src/androidTest/java/com/costular/atomtasks/data/database/MigrationsTest.kt b/data/src/androidTest/java/com/costular/atomtasks/data/database/MigrationsTest.kt index 45047c26..74caa57d 100644 --- a/data/src/androidTest/java/com/costular/atomtasks/data/database/MigrationsTest.kt +++ b/data/src/androidTest/java/com/costular/atomtasks/data/database/MigrationsTest.kt @@ -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 @@ -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 " + @@ -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()) { @@ -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); " @@ -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 -> diff --git a/data/src/main/java/com/costular/atomtasks/data/database/AtomTasksDatabase.kt b/data/src/main/java/com/costular/atomtasks/data/database/AtomTasksDatabase.kt index 85548a9b..f680506b 100644 --- a/data/src/main/java/com/costular/atomtasks/data/database/AtomTasksDatabase.kt +++ b/data/src/main/java/com/costular/atomtasks/data/database/AtomTasksDatabase.kt @@ -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 ), ] ) diff --git a/data/src/main/java/com/costular/atomtasks/data/database/DatabaseMigrations.kt b/data/src/main/java/com/costular/atomtasks/data/database/DatabaseMigrations.kt index 2fc9b810..8a29dbe2 100644 --- a/data/src/main/java/com/costular/atomtasks/data/database/DatabaseMigrations.kt +++ b/data/src/main/java/com/costular/atomtasks/data/database/DatabaseMigrations.kt @@ -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`;") + } +} diff --git a/data/src/main/java/com/costular/atomtasks/data/database/DatabaseModule.kt b/data/src/main/java/com/costular/atomtasks/data/database/DatabaseModule.kt index 6167e827..1fda013d 100644 --- a/data/src/main/java/com/costular/atomtasks/data/database/DatabaseModule.kt +++ b/data/src/main/java/com/costular/atomtasks/data/database/DatabaseModule.kt @@ -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() }