Fix database upgrade
This commit is contained in:
parent
ac71ad887d
commit
f1a9c1383c
@ -95,21 +95,23 @@ class VolumeDatabase(private val context: Context): SQLiteOpenHelper(context, Co
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (oldVersion < 6) {
|
if (oldVersion < 6) {
|
||||||
val volumeCount = db.rawQuery("SELECT COUNT(*) FROM $TABLE_NAME", null).let { cursor ->
|
val cursor = db.rawQuery("SELECT $COLUMN_NAME FROM $TABLE_NAME;", null)
|
||||||
cursor.moveToNext()
|
val volumeNames = arrayOfNulls<String>(cursor.count)
|
||||||
cursor.getInt(0).also {
|
var i = 0
|
||||||
|
while (cursor.moveToNext()) {
|
||||||
|
volumeNames[i++] = cursor.getString(0)
|
||||||
|
}
|
||||||
cursor.close()
|
cursor.close()
|
||||||
}
|
|
||||||
}
|
|
||||||
db.execSQL("ALTER TABLE $TABLE_NAME RENAME TO OLD;")
|
db.execSQL("ALTER TABLE $TABLE_NAME RENAME TO OLD;")
|
||||||
createTable(db)
|
createTable(db)
|
||||||
val uuids = (0 until volumeCount).joinToString(", ") { "('${VolumeData.newUuid()}')" }
|
val uuidsValues = volumeNames.indices.joinToString(", ") { "('${VolumeData.newUuid()}', ?)" }
|
||||||
val baseColumns = "$COLUMN_NAME, $COLUMN_HIDDEN, $COLUMN_TYPE, $COLUMN_HASH, $COLUMN_IV"
|
|
||||||
// add uuids to old data
|
// add uuids to old data
|
||||||
db.execSQL("INSERT INTO $TABLE_NAME " +
|
db.execSQL(
|
||||||
"SELECT uuid, $baseColumns FROM " +
|
"INSERT INTO $TABLE_NAME " +
|
||||||
"(SELECT $baseColumns, ROW_NUMBER() OVER () i FROM OLD) NATURAL JOIN " +
|
"WITH uuids($COLUMN_UUID, $COLUMN_NAME) AS (VALUES $uuidsValues) " +
|
||||||
"(SELECT column1 uuid, ROW_NUMBER() OVER () i FROM (VALUES $uuids));")
|
"SELECT * FROM OLD NATURAL JOIN uuids;",
|
||||||
|
volumeNames
|
||||||
|
)
|
||||||
db.execSQL("DROP TABLE OLD;")
|
db.execSQL("DROP TABLE OLD;")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user