Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
369 changes: 360 additions & 9 deletions core/database/schemas/com.awan.app.core.database.AwanDatabase/1.json

Large diffs are not rendered by default.

748 changes: 0 additions & 748 deletions core/database/schemas/com.awan.app.core.database.AwanDatabase/2.json

This file was deleted.

779 changes: 0 additions & 779 deletions core/database/schemas/com.awan.app.core.database.AwanDatabase/3.json

This file was deleted.

790 changes: 0 additions & 790 deletions core/database/schemas/com.awan.app.core.database.AwanDatabase/4.json

This file was deleted.

927 changes: 0 additions & 927 deletions core/database/schemas/com.awan.app.core.database.AwanDatabase/5.json

This file was deleted.

882 changes: 0 additions & 882 deletions core/database/schemas/com.awan.app.core.database.AwanDatabase/6.json

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@ package com.awan.app.core.database

import androidx.room.Database
import androidx.room.RoomDatabase
import androidx.room.migration.Migration
import androidx.sqlite.db.SupportSQLiteDatabase
import com.awan.app.core.database.dao.CachedScheduleDateDao
import com.awan.app.core.database.dao.CategoryDao
import com.awan.app.core.database.dao.GoalDao
Expand Down Expand Up @@ -55,7 +53,7 @@ import com.awan.app.core.database.model.ZoneEntity
OwnedItemEntity::class,
EquippedItemEntity::class,
],
version = 5,
version = 1,
exportSchema = true,
)
abstract class AwanDatabase : RoomDatabase() {
Expand All @@ -79,255 +77,4 @@ abstract class AwanDatabase : RoomDatabase() {
abstract fun cachedScheduleDateDao(): CachedScheduleDateDao

abstract fun storeDao(): StoreDao

companion object {
val MIGRATION_1_2 = object : Migration(1, 2) {
override fun migrate(db: SupportSQLiteDatabase) {
// 1. Create categories table
db.execSQL(
"""
CREATE TABLE IF NOT EXISTS `categories` (
`id` TEXT NOT NULL,
`name` TEXT NOT NULL,
`colorHex` TEXT,
`icon` TEXT,
PRIMARY KEY(`id`)
)
""".trimIndent()
)

// 2. Recreate tasks table with nullable goalId and optional categoryId
db.execSQL(
"""
CREATE TABLE IF NOT EXISTS `tasks_new` (
`id` TEXT NOT NULL,
`title` TEXT NOT NULL,
`description` TEXT,
`estimatedDuration` INTEGER NOT NULL,
`status` TEXT NOT NULL,
`mandatory` INTEGER NOT NULL,
`estimatedPoints` INTEGER NOT NULL,
`allowTaskSplitting` INTEGER NOT NULL,
`goalId` TEXT,
`categoryId` TEXT,
PRIMARY KEY(`id`),
FOREIGN KEY(`goalId`) REFERENCES `goals`(`id`) ON UPDATE NO ACTION ON DELETE NO ACTION,
FOREIGN KEY(`categoryId`) REFERENCES `categories`(`id`) ON UPDATE NO ACTION ON DELETE NO ACTION
)
""".trimIndent()
)

db.execSQL(
"""
INSERT INTO `tasks_new` (
`id`, `title`, `description`, `estimatedDuration`, `status`,
`mandatory`, `estimatedPoints`, `allowTaskSplitting`, `goalId`, `categoryId`
)
SELECT
`id`, `title`, `description`, `estimatedDuration`, `status`,
`mandatory`, `estimatedPoints`, `allowTaskSplitting`, `goalId`, NULL
FROM `tasks`
""".trimIndent()
)

db.execSQL("DROP TABLE `tasks`")
db.execSQL("ALTER TABLE `tasks_new` RENAME TO `tasks`")

db.execSQL("CREATE INDEX IF NOT EXISTS `index_tasks_goalId` ON `tasks` (`goalId`)")
db.execSQL("CREATE INDEX IF NOT EXISTS `index_tasks_categoryId` ON `tasks` (`categoryId`)")

// 3. Create sessions table
db.execSQL(
"""
CREATE TABLE IF NOT EXISTS `sessions` (
`id` TEXT NOT NULL,
`taskId` TEXT NOT NULL,
`zoneId` TEXT,
`date` TEXT NOT NULL,
`startTime` TEXT NOT NULL,
`endTime` TEXT NOT NULL,
`status` TEXT NOT NULL,
`locked` INTEGER NOT NULL,
PRIMARY KEY(`id`),
FOREIGN KEY(`taskId`) REFERENCES `tasks`(`id`) ON UPDATE NO ACTION ON DELETE CASCADE
)
""".trimIndent()
)

db.execSQL("CREATE INDEX IF NOT EXISTS `index_sessions_taskId` ON `sessions` (`taskId`)")
db.execSQL("CREATE INDEX IF NOT EXISTS `index_sessions_zoneId` ON `sessions` (`zoneId`)")
db.execSQL("CREATE INDEX IF NOT EXISTS `index_sessions_date` ON `sessions` (`date`)")

// 4. Create cached_schedule_dates table
db.execSQL(
"""
CREATE TABLE IF NOT EXISTS `cached_schedule_dates` (
`date` TEXT NOT NULL,
`lastSyncedAt` TEXT NOT NULL,
PRIMARY KEY(`date`)
)
""".trimIndent()
)
}
}

val MIGRATION_2_3 = object : Migration(2, 3) {
override fun migrate(db: SupportSQLiteDatabase) {
val existingColumns = mutableSetOf<String>()
try {
val cursor = db.query("PRAGMA table_info(`users`)")
cursor.use {
val nameIndex = it.getColumnIndex("name")
while (it.moveToNext()) {
if (nameIndex != -1) {
existingColumns.add(it.getString(nameIndex))
}
}
}
} catch (_: Exception) {
}
if (!existingColumns.contains("expiryTime")) {
db.execSQL("ALTER TABLE `users` ADD COLUMN `expiryTime` INTEGER NOT NULL DEFAULT 0")
}
if (!existingColumns.contains("profilePictureUrl")) {
db.execSQL("ALTER TABLE `users` ADD COLUMN `profilePictureUrl` TEXT")
}
if (!existingColumns.contains("isNew")) {
db.execSQL("ALTER TABLE `users` ADD COLUMN `isNew` INTEGER NOT NULL DEFAULT 0")
}
db.execSQL("ALTER TABLE `categories` ADD COLUMN `expiryTime` INTEGER NOT NULL DEFAULT 0")
db.execSQL("ALTER TABLE `goals` ADD COLUMN `expiryTime` INTEGER NOT NULL DEFAULT 0")
db.execSQL("ALTER TABLE `tasks` ADD COLUMN `expiryTime` INTEGER NOT NULL DEFAULT 0")
db.execSQL("ALTER TABLE `sessions` ADD COLUMN `expiryTime` INTEGER NOT NULL DEFAULT 0")
db.execSQL("ALTER TABLE `templates` ADD COLUMN `expiryTime` INTEGER NOT NULL DEFAULT 0")
db.execSQL("ALTER TABLE `cached_schedule_dates` ADD COLUMN `expiryTime` INTEGER NOT NULL DEFAULT 0")
}
}

val MIGRATION_3_4 = object : Migration(3, 4) {
override fun migrate(db: SupportSQLiteDatabase) {
// 1. Ensure users table columns exist
val existingColumns = mutableSetOf<String>()
try {
val cursor = db.query("PRAGMA table_info(`users`)")
cursor.use {
val nameIndex = it.getColumnIndex("name")
while (it.moveToNext()) {
if (nameIndex != -1) {
existingColumns.add(it.getString(nameIndex))
}
}
}
} catch (_: Exception) {
}
if (!existingColumns.contains("expiryTime")) {
db.execSQL("ALTER TABLE `users` ADD COLUMN `expiryTime` INTEGER NOT NULL DEFAULT 0")
}
if (!existingColumns.contains("profilePictureUrl")) {
db.execSQL("ALTER TABLE `users` ADD COLUMN `profilePictureUrl` TEXT")
}
if (!existingColumns.contains("isNew")) {
db.execSQL("ALTER TABLE `users` ADD COLUMN `isNew` INTEGER NOT NULL DEFAULT 0")
}

// 2. Recreate tasks table without goalId FK
db.execSQL(
"""
CREATE TABLE IF NOT EXISTS `tasks_v4` (
`id` TEXT NOT NULL,
`title` TEXT NOT NULL,
`description` TEXT,
`estimatedDuration` INTEGER NOT NULL,
`status` TEXT NOT NULL,
`mandatory` INTEGER NOT NULL,
`estimatedPoints` INTEGER NOT NULL,
`allowTaskSplitting` INTEGER NOT NULL,
`goalId` TEXT,
`categoryId` TEXT,
`expiryTime` INTEGER NOT NULL DEFAULT 0,
PRIMARY KEY(`id`),
FOREIGN KEY(`categoryId`) REFERENCES `categories`(`id`) ON UPDATE NO ACTION ON DELETE NO ACTION
)
""".trimIndent()
)

val taskColumns = mutableSetOf<String>()
try {
val cursor = db.query("PRAGMA table_info(`tasks`)")
cursor.use {
val nameIndex = it.getColumnIndex("name")
while (it.moveToNext()) {
if (nameIndex != -1) {
taskColumns.add(it.getString(nameIndex))
}
}
}
} catch (_: Exception) {
}

val selectExpiry = if (taskColumns.contains("expiryTime")) "`expiryTime`" else "0 AS `expiryTime`"

db.execSQL(
"""
INSERT INTO `tasks_v4` (
`id`, `title`, `description`, `estimatedDuration`, `status`,
`mandatory`, `estimatedPoints`, `allowTaskSplitting`, `goalId`, `categoryId`, `expiryTime`
)
SELECT
`id`, `title`, `description`, `estimatedDuration`, `status`,
`mandatory`, `estimatedPoints`, `allowTaskSplitting`, `goalId`, `categoryId`, $selectExpiry
FROM `tasks`
""".trimIndent()
)

db.execSQL("DROP TABLE `tasks`")
db.execSQL("ALTER TABLE `tasks_v4` RENAME TO `tasks`")
db.execSQL("CREATE INDEX IF NOT EXISTS `index_tasks_goalId` ON `tasks` (`goalId`)")
db.execSQL("CREATE INDEX IF NOT EXISTS `index_tasks_categoryId` ON `tasks` (`categoryId`)")
}
}

val MIGRATION_4_5 = object : Migration(4, 5) {
override fun migrate(db: SupportSQLiteDatabase) {
db.execSQL(
"""
CREATE TABLE IF NOT EXISTS `store_items` (
`id` TEXT NOT NULL,
`name` TEXT NOT NULL,
`description` TEXT NOT NULL,
`image` TEXT NOT NULL,
`info` TEXT,
`price` INTEGER NOT NULL,
`version` TEXT NOT NULL,
`type` TEXT NOT NULL,
`expiryTime` INTEGER NOT NULL DEFAULT 0,
PRIMARY KEY(`id`)
)
""".trimIndent()
)
db.execSQL(
"""
CREATE TABLE IF NOT EXISTS `owned_items` (
`id` TEXT NOT NULL,
`itemId` TEXT NOT NULL,
`boughtAt` TEXT NOT NULL,
`expiryTime` INTEGER NOT NULL DEFAULT 0,
PRIMARY KEY(`id`)
)
""".trimIndent()
)
db.execSQL(
"""
CREATE TABLE IF NOT EXISTS `equipped_items` (
`type` TEXT NOT NULL,
`itemId` TEXT NOT NULL,
`equippedAt` TEXT NOT NULL,
`expiryTime` INTEGER NOT NULL DEFAULT 0,
PRIMARY KEY(`type`)
)
""".trimIndent()
)
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,6 @@ object DatabaseModule {
AwanDatabase::class.java,
"awan-database",
)
.addMigrations(AwanDatabase.MIGRATION_1_2)
.addMigrations(AwanDatabase.MIGRATION_2_3)
.addMigrations(AwanDatabase.MIGRATION_3_4)
.addMigrations(AwanDatabase.MIGRATION_4_5)
.fallbackToDestructiveMigration(dropAllTables = true)
.build()

Expand Down

This file was deleted.

This file was deleted.

Loading
Loading