Skip to content

Commit 35a3b3a

Browse files
committed
Fix migration
1 parent c82260b commit 35a3b3a

File tree

1 file changed

+24
-7
lines changed

1 file changed

+24
-7
lines changed

android/src/main/kotlin/vn/hunghd/flutterdownloader/TaskDbHelper.kt

Lines changed: 24 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,31 @@ class TaskDbHelper private constructor(context: Context) :
1212
}
1313

1414
override fun onUpgrade(db: SQLiteDatabase, oldVersion: Int, newVersion: Int) {
15-
if (newVersion == 4) {
16-
db.execSQL("ALTER TABLE ${TaskEntry.TABLE_NAME} ADD COLUMN ${TaskEntry.COLUMN_ALLOW_CELLULAR} TINYINT DEFAULT 1")
17-
} else if (oldVersion == 2 && newVersion == 3) {
18-
db.execSQL("ALTER TABLE " + TaskEntry.TABLE_NAME + " ADD COLUMN " + TaskEntry.COLUMN_SAVE_IN_PUBLIC_STORAGE + " TINYINT DEFAULT 0")
19-
} else {
20-
db.execSQL(SQL_DELETE_ENTRIES)
21-
onCreate(db)
15+
update1to2(db, oldVersion)
16+
update2to3(db, oldVersion)
17+
update3to4(db, oldVersion)
18+
}
19+
20+
private fun update1to2(db: SQLiteDatabase, oldVersion: Int) {
21+
if (oldVersion > 1) {
22+
return
23+
}
24+
db.execSQL(SQL_DELETE_ENTRIES)
25+
onCreate(db)
26+
}
27+
28+
private fun update2to3(db: SQLiteDatabase, oldVersion: Int) {
29+
if (oldVersion > 2) {
30+
return
31+
}
32+
db.execSQL("ALTER TABLE " + TaskEntry.TABLE_NAME + " ADD COLUMN " + TaskEntry.COLUMN_SAVE_IN_PUBLIC_STORAGE + " TINYINT DEFAULT 0")
33+
}
34+
35+
private fun update3to4(db: SQLiteDatabase, oldVersion: Int) {
36+
if (oldVersion > 3) {
37+
return
2238
}
39+
db.execSQL("ALTER TABLE ${TaskEntry.TABLE_NAME} ADD COLUMN ${TaskEntry.COLUMN_ALLOW_CELLULAR} TINYINT DEFAULT 1")
2340
}
2441

2542
override fun onDowngrade(db: SQLiteDatabase, oldVersion: Int, newVersion: Int) {

0 commit comments

Comments
 (0)