Refactor database schema to delete bookmarksLocations table and migrate data

*   Delete `bookmarksLocations` table from the database during schema upgrade
*   Migrate data from `bookmarksLocations` to `bookmarks_locations` in new schema
*   Add `INSERT OR REPLACE` to prevent duplication during migration
*   Delete `CONTRIBUTIONS_TABLE` and `BOOKMARKS_LOCATIONS` in the application start-up to have a fresh DB for first start-up after update
This commit is contained in:
Saifuddin 2025-02-23 13:33:34 +05:30
parent b45d4c30ad
commit 25db06c551
3 changed files with 8 additions and 7 deletions

View file

@ -247,6 +247,11 @@ class CommonsApplication : MultiDexApplication() {
DBOpenHelper.CONTRIBUTIONS_TABLE
) //Delete the contributions table in the existing db on older versions
dbOpenHelper.deleteTable(
db,
DBOpenHelper.BOOKMARKS_LOCATIONS
)
try {
contributionDao.deleteAll()
} catch (e: SQLiteException) {

View file

@ -20,6 +20,7 @@ class DBOpenHelper(
private const val DATABASE_NAME = "commons.db"
private const val DATABASE_VERSION = 21
const val CONTRIBUTIONS_TABLE = "contributions"
const val BOOKMARKS_LOCATIONS = "bookmarksLocations"
private const val DROP_TABLE_STATEMENT = "DROP TABLE IF EXISTS %s"
}
@ -42,6 +43,7 @@ class DBOpenHelper(
RecentSearchesDao.Table.onUpdate(db, from, to)
RecentLanguagesDao.Table.onUpdate(db, from, to)
deleteTable(db, CONTRIBUTIONS_TABLE)
deleteTable(db, BOOKMARKS_LOCATIONS)
}
/**

View file

@ -276,11 +276,9 @@ open class CommonsApplicationModule(private val applicationContext: Context) {
"""
)
// Open old database manually
val oldDbPath = "/data/user/0/fr.free.nrw.commons.beta/databases/commons.db"
val oldDb = SQLiteDatabase.openDatabase(oldDbPath, null, SQLiteDatabase.OPEN_READONLY)
// Fetch data from old table
val cursor = oldDb.rawQuery("SELECT * FROM bookmarksLocations", null)
while (cursor.moveToNext()) {
@ -301,10 +299,9 @@ open class CommonsApplicationModule(private val applicationContext: Context) {
val locationPic = cursor.getString(cursor.getColumnIndexOrThrow("location_pic")) ?: ""
val locationExists = cursor.getInt(cursor.getColumnIndexOrThrow("location_exists"))
// Insert data into new table
db.execSQL(
"""
INSERT INTO bookmarks_locations (
INSERT OR REPLACE INTO bookmarks_locations (
location_name, location_language, location_description, location_category,
location_label_text, location_label_icon, location_lat, location_long,
location_image_url, location_wikipedia_link, location_wikidata_link,
@ -322,9 +319,6 @@ open class CommonsApplicationModule(private val applicationContext: Context) {
cursor.close()
oldDb.close()
// Drop the old table
db.execSQL("DROP TABLE IF EXISTS bookmarksLocations")
}
}
}