mirror of
https://github.com/commons-app/apps-android-commons.git
synced 2025-10-26 12:23:58 +01:00
Refactor database and add migration
* `AppDatabase`: Updated to use room migration. * `CommonsApplicationModule`: added migration from version 19 to 20 to `appDatabase`
This commit is contained in:
parent
9c352b4ba2
commit
91880c4f5f
2 changed files with 49 additions and 1 deletions
|
|
@ -1,8 +1,12 @@
|
||||||
package fr.free.nrw.commons.db
|
package fr.free.nrw.commons.db
|
||||||
|
|
||||||
|
import android.content.Context
|
||||||
import androidx.room.Database
|
import androidx.room.Database
|
||||||
|
import androidx.room.Room
|
||||||
import androidx.room.RoomDatabase
|
import androidx.room.RoomDatabase
|
||||||
import androidx.room.TypeConverters
|
import androidx.room.TypeConverters
|
||||||
|
import androidx.room.migration.Migration
|
||||||
|
import androidx.sqlite.db.SupportSQLiteDatabase
|
||||||
import fr.free.nrw.commons.bookmarks.category.BookmarkCategoriesDao
|
import fr.free.nrw.commons.bookmarks.category.BookmarkCategoriesDao
|
||||||
import fr.free.nrw.commons.bookmarks.category.BookmarksCategoryModal
|
import fr.free.nrw.commons.bookmarks.category.BookmarksCategoryModal
|
||||||
import fr.free.nrw.commons.bookmarks.locations.BookmarkLocationsDao
|
import fr.free.nrw.commons.bookmarks.locations.BookmarkLocationsDao
|
||||||
|
|
|
||||||
|
|
@ -192,7 +192,10 @@ open class CommonsApplicationModule(private val applicationContext: Context) {
|
||||||
applicationContext,
|
applicationContext,
|
||||||
AppDatabase::class.java,
|
AppDatabase::class.java,
|
||||||
"commons_room.db"
|
"commons_room.db"
|
||||||
).addMigrations(MIGRATION_1_2).fallbackToDestructiveMigration().build()
|
).addMigrations(
|
||||||
|
MIGRATION_1_2,
|
||||||
|
MIGRATION_19_TO_20
|
||||||
|
).fallbackToDestructiveMigration().build()
|
||||||
|
|
||||||
@Provides
|
@Provides
|
||||||
fun providesContributionsDao(appDatabase: AppDatabase): ContributionDao =
|
fun providesContributionsDao(appDatabase: AppDatabase): ContributionDao =
|
||||||
|
|
@ -246,5 +249,46 @@ open class CommonsApplicationModule(private val applicationContext: Context) {
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private val MIGRATION_19_TO_20 = object : Migration(19, 20) {
|
||||||
|
override fun migrate(db: SupportSQLiteDatabase) {
|
||||||
|
db.execSQL(
|
||||||
|
"""
|
||||||
|
CREATE TABLE IF NOT EXISTS bookmarks_locations (
|
||||||
|
location_name TEXT NOT NULL PRIMARY KEY,
|
||||||
|
location_language TEXT NOT NULL,
|
||||||
|
location_description TEXT NOT NULL,
|
||||||
|
location_lat REAL NOT NULL,
|
||||||
|
location_long REAL NOT NULL,
|
||||||
|
location_category TEXT NOT NULL,
|
||||||
|
location_label_text TEXT NOT NULL,
|
||||||
|
location_label_icon INTEGER,
|
||||||
|
location_image_url TEXT NOT NULL,
|
||||||
|
location_wikipedia_link TEXT NOT NULL,
|
||||||
|
location_wikidata_link TEXT NOT NULL,
|
||||||
|
location_commons_link TEXT NOT NULL,
|
||||||
|
location_pic TEXT NOT NULL,
|
||||||
|
location_exists INTEGER NOT NULL CHECK(location_exists IN (0, 1))
|
||||||
|
)
|
||||||
|
"""
|
||||||
|
)
|
||||||
|
db.execSQL("""
|
||||||
|
INSERT 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,
|
||||||
|
location_commons_link, location_pic, location_exists
|
||||||
|
)
|
||||||
|
SELECT
|
||||||
|
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,
|
||||||
|
location_commons_link, location_pic, location_exists
|
||||||
|
FROM bookmarksLocations
|
||||||
|
""")
|
||||||
|
|
||||||
|
db.execSQL("DROP TABLE IF EXISTS bookmarkLocations")
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue