From a7bc2ab64c4a73c7d757288d97b3288b8d856daf Mon Sep 17 00:00:00 2001 From: bowyer-app Date: Tue, 22 Oct 2024 04:36:57 +0900 Subject: [PATCH] Fix migration (#949) --- .../hunghd/flutterdownloader/TaskDbHelper.kt | 31 ++++++++++++++----- 1 file changed, 24 insertions(+), 7 deletions(-) diff --git a/android/src/main/kotlin/vn/hunghd/flutterdownloader/TaskDbHelper.kt b/android/src/main/kotlin/vn/hunghd/flutterdownloader/TaskDbHelper.kt index 51849631..07737109 100644 --- a/android/src/main/kotlin/vn/hunghd/flutterdownloader/TaskDbHelper.kt +++ b/android/src/main/kotlin/vn/hunghd/flutterdownloader/TaskDbHelper.kt @@ -12,14 +12,31 @@ class TaskDbHelper private constructor(context: Context) : } override fun onUpgrade(db: SQLiteDatabase, oldVersion: Int, newVersion: Int) { - if (newVersion == 4) { - db.execSQL("ALTER TABLE ${TaskEntry.TABLE_NAME} ADD COLUMN ${TaskEntry.COLUMN_ALLOW_CELLULAR} TINYINT DEFAULT 1") - } else if (oldVersion == 2 && newVersion == 3) { - db.execSQL("ALTER TABLE " + TaskEntry.TABLE_NAME + " ADD COLUMN " + TaskEntry.COLUMN_SAVE_IN_PUBLIC_STORAGE + " TINYINT DEFAULT 0") - } else { - db.execSQL(SQL_DELETE_ENTRIES) - onCreate(db) + update1to2(db, oldVersion) + update2to3(db, oldVersion) + update3to4(db, oldVersion) + } + + private fun update1to2(db: SQLiteDatabase, oldVersion: Int) { + if (oldVersion > 1) { + return + } + db.execSQL(SQL_DELETE_ENTRIES) + onCreate(db) + } + + private fun update2to3(db: SQLiteDatabase, oldVersion: Int) { + if (oldVersion > 2) { + return + } + db.execSQL("ALTER TABLE " + TaskEntry.TABLE_NAME + " ADD COLUMN " + TaskEntry.COLUMN_SAVE_IN_PUBLIC_STORAGE + " TINYINT DEFAULT 0") + } + + private fun update3to4(db: SQLiteDatabase, oldVersion: Int) { + if (oldVersion > 3) { + return } + db.execSQL("ALTER TABLE ${TaskEntry.TABLE_NAME} ADD COLUMN ${TaskEntry.COLUMN_ALLOW_CELLULAR} TINYINT DEFAULT 1") } override fun onDowngrade(db: SQLiteDatabase, oldVersion: Int, newVersion: Int) {