Skip to content

Commit

Permalink
Database migration from 1 to 2
Browse files Browse the repository at this point in the history
  • Loading branch information
ztimms73 committed Apr 17, 2023
1 parent cbc8ce7 commit 5286283
Show file tree
Hide file tree
Showing 9 changed files with 595 additions and 7 deletions.
420 changes: 420 additions & 0 deletions app/schemas/org.xtimms.ridebus.data.database.RideBusDatabase/2.json

Large diffs are not rendered by default.

File renamed without changes.
11 changes: 11 additions & 0 deletions app/src/main/java/org/xtimms/ridebus/Migrations.kt
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,17 @@ object Migrations {
if (oldVersion == 0) {
return false
}

if (oldVersion < 3) {
// Since version 0.3 checks city from database
when (preferences.city().get()) {
"POLOTSK" -> preferences.city().set("0")
"NOVOPOLOTSK" -> preferences.city().set("1")
else -> preferences.city().set("0")
}
preferences.databaseVersion().set("3.0")
}

return true
}
return false
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,39 @@ import android.content.Context
import androidx.room.Database
import androidx.room.Room
import androidx.room.RoomDatabase
import org.xtimms.ridebus.data.database.dao.*
import org.xtimms.ridebus.data.database.entity.*
import androidx.room.migration.Migration
import org.xtimms.ridebus.data.database.dao.CityDao
import org.xtimms.ridebus.data.database.dao.RouteDao
import org.xtimms.ridebus.data.database.dao.RoutesAndStopsDao
import org.xtimms.ridebus.data.database.dao.ScheduleDao
import org.xtimms.ridebus.data.database.dao.StopDao
import org.xtimms.ridebus.data.database.dao.TransportDao
import org.xtimms.ridebus.data.database.entity.City
import org.xtimms.ridebus.data.database.entity.KindOfRoute
import org.xtimms.ridebus.data.database.entity.Route
import org.xtimms.ridebus.data.database.entity.RoutesAndStops
import org.xtimms.ridebus.data.database.entity.Schedule
import org.xtimms.ridebus.data.database.entity.Stop
import org.xtimms.ridebus.data.database.entity.Transport
import org.xtimms.ridebus.data.database.entity.TypeDay
import org.xtimms.ridebus.data.database.migrations.Migration1To2

//
// Created by Xtimms on 28.08.2021.
//
@Database(entities = [Route::class, Stop::class, RoutesAndStops::class, City::class, KindOfRoute::class, Transport::class, Schedule::class, TypeDay::class], version = 1)
@Database(
entities = [
Route::class,
Stop::class,
RoutesAndStops::class,
City::class,
KindOfRoute::class,
Transport::class,
Schedule::class,
TypeDay::class
],
version = 2
)
abstract class RideBusDatabase : RoomDatabase() {

abstract fun routeDao(): RouteDao
Expand All @@ -29,16 +55,21 @@ abstract class RideBusDatabase : RoomDatabase() {
@Volatile
private var INSTANCE: RideBusDatabase? = null

private val databaseMigrations: Array<Migration>
get() = arrayOf(
Migration1To2()
)

fun getDatabase(context: Context): RideBusDatabase {
return INSTANCE ?: synchronized(this) {
val instance = Room.databaseBuilder(
context,
RideBusDatabase::class.java,
"ridebus.db"
)
.addMigrations(*databaseMigrations)
.allowMainThreadQueries()
.createFromAsset("database/ridebus.db")
.build()
.createFromAsset("database/ridebus_v3.db").build()
INSTANCE = instance

instance
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
package org.xtimms.ridebus.data.database.migrations

import androidx.room.migration.Migration
import androidx.sqlite.db.SupportSQLiteDatabase

class Migration1To2 : Migration(1, 2) {

override fun migrate(database: SupportSQLiteDatabase) {
database.execSQL("DROP TABLE `route`")
database.execSQL(
"CREATE TABLE IF NOT EXISTS `route`" +
" (`_id` INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL," +
" `city_id` INTEGER NOT NULL," +
" `transport_id` INTEGER NOT NULL," +
" `kindRoute_id` INTEGER NOT NULL," +
" `route_number` TEXT NOT NULL," +
" `route_title` TEXT NOT NULL," +
" `description` TEXT NOT NULL," +
" `fare` TEXT NOT NULL," +
" `weekly_traffic` TEXT NOT NULL," +
" `working_hours` TEXT NOT NULL," +
" `following` TEXT NOT NULL," +
" `carrier_company` TEXT NOT NULL," +
" `tech_info` TEXT NOT NULL," +
" `cash` INTEGER NOT NULL," +
" `qr_code` INTEGER NOT NULL," +
" `is_small` INTEGER NOT NULL," +
" `is_big` INTEGER NOT NULL," +
" `is_very_big` INTEGER NOT NULL," +
" `eco` INTEGER NOT NULL," +
" `wifi` INTEGER NOT NULL," +
" `low_floor` INTEGER NOT NULL)"
)
database.execSQL("DROP TABLE `stop`")
database.execSQL(
"CREATE TABLE IF NOT EXISTS `stop`" +
" (`_id` INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL," +
" `city_id` INTEGER NOT NULL," +
" `transport_id` INTEGER NOT NULL," +
" `kindRoute_id` INTEGER NOT NULL," +
" `name` TEXT NOT NULL," +
" `direction` TEXT NOT NULL," +
" `latitude` TEXT NOT NULL," +
" `longitude` TEXT NOT NULL)"
)
database.execSQL("DROP TABLE `routeStops`")
database.execSQL(
"CREATE TABLE IF NOT EXISTS `routeStops`" +
" (`_id` INTEGER NOT NULL," +
" `route_id` INTEGER NOT NULL," +
" `stop_id` INTEGER NOT NULL," +
" `shift_hour` INTEGER NOT NULL," +
" `shift_minute` INTEGER NOT NULL," +
" PRIMARY KEY(`_id`))"
)
database.execSQL("DROP TABLE `city`")
database.execSQL(
"CREATE TABLE IF NOT EXISTS `city`" +
" (`_id` INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL," +
" `cityName` TEXT NOT NULL," +
" `latitude` TEXT NOT NULL," +
" `longitude` TEXT NOT NULL)"
)
database.execSQL("DROP TABLE `schedule`")
database.execSQL(
"CREATE TABLE IF NOT EXISTS `schedule`" +
" (`_id` INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL," +
" `route_id` INTEGER NOT NULL," +
" `type_day` INTEGER NOT NULL," +
" `hour` INTEGER NOT NULL," +
" `minute` INTEGER NOT NULL)"
)
}
}
12 changes: 11 additions & 1 deletion app/src/main/java/org/xtimms/ridebus/ui/main/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,17 @@ class MainActivity : BaseActivity() {
}
// Show changelog prompt on update
if (didMigration && !BuildConfig.DEBUG) {
WhatsNewDialogController().showDialog(router)
launchNow {
try {
val result = DatabaseUpdateChecker().checkForUpdate(this@MainActivity, true)
if (result is DatabaseUpdateResult.NewUpdate) {
CriticalDatabaseUpdateDialogController(result).showDialog(router)
}
} catch (error: Exception) {
this@MainActivity.toast(error.message)
logcat(LogPriority.ERROR, error)
}
}
}
} else {
// Restore selected nav item
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ class MoreController :
bindTo(preferences.autoUpdateSchedule())
titleRes = R.string.automatic_schedule_updates
summaryRes = R.string.automatic_schedule_updates_summary
iconRes = R.drawable.ic_update
iconRes = R.drawable.ic_database_update
iconTint = tintColor

onChange { newValue ->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -122,3 +122,40 @@ class NewScheduleDialogController(bundle: Bundle? = null) : DialogController(bun
private const val NEW_SCHEDULE_BODY_KEY = "NewScheduleDialogController.body"
private const val NEW_SCHEDULE_DOWNLOAD_URL_KEY = "NewScheduleDialogController.download_url"
private const val NEW_SCHEDULE_VERSION_KEY = "NewScheduleDialogController.version"

class CriticalDatabaseUpdateDialogController(bundle: Bundle? = null) : DialogController(bundle) {

constructor(update: DatabaseUpdateResult.NewUpdate) : this(
bundleOf(
CRITICAL_DATABASE_UPDATE_BODY_KEY to update.update.info,
CRITICAL_DATABASE_UPDATE_VERSION_KEY to update.update.version,
CRITICAL_DATABASE_UPDATE_DOWNLOAD_URL_KEY to update.update.getDownloadLink()
)
)

override fun onCreateDialog(savedViewState: Bundle?): Dialog {
val infoBody = args.getString(CRITICAL_DATABASE_UPDATE_BODY_KEY)!!

return MaterialAlertDialogBuilder(
activity!!,
materialR.style.ThemeOverlay_Material3_MaterialAlertDialog_Centered
)
.setTitle(R.string.critical_database_update_title)
.setMessage(R.string.critical_database_update_message)
.setIcon(R.drawable.ic_database_arrow_up)
.setPositiveButton(R.string.update_check_confirm) { _, _ ->
applicationContext?.let { context ->
// Start download
val url = args.getString(CRITICAL_DATABASE_UPDATE_DOWNLOAD_URL_KEY)!!
val version = args.getString(CRITICAL_DATABASE_UPDATE_VERSION_KEY)!!
DatabaseUpdateService.start(context, url, title = infoBody, version = version)
}
}
.setCancelable(false)
.create()
}
}

private const val CRITICAL_DATABASE_UPDATE_BODY_KEY = "CriticalDatabaseUpdateDialogController.body"
private const val CRITICAL_DATABASE_UPDATE_DOWNLOAD_URL_KEY = "CriticalDatabaseUpdateDialogController.download_url"
private const val CRITICAL_DATABASE_UPDATE_VERSION_KEY = "CriticalDatabaseUpdateDialogController.version"
5 changes: 5 additions & 0 deletions app/src/main/res/drawable/ic_database_arrow_up.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<vector android:height="24dp" android:tint="?attr/colorControlNormal"
android:viewportHeight="24" android:viewportWidth="24"
android:width="24dp" xmlns:android="http://schemas.android.com/apk/res/android">
<path android:fillColor="#FF000000" android:pathData="M20 13.09V7C20 4.79 16.42 3 12 3S4 4.79 4 7V17C4 19.21 7.59 21 12 21C12.46 21 12.9 21 13.33 20.94C13.12 20.33 13 19.68 13 19L13 18.95C12.68 19 12.35 19 12 19C8.13 19 6 17.5 6 17V14.77C7.61 15.55 9.72 16 12 16C12.65 16 13.27 15.96 13.88 15.89C14.93 14.16 16.83 13 19 13C19.34 13 19.67 13.04 20 13.09M18 12.45C16.7 13.4 14.42 14 12 14S7.3 13.4 6 12.45V9.64C7.47 10.47 9.61 11 12 11S16.53 10.47 18 9.64V12.45M12 9C8.13 9 6 7.5 6 7S8.13 5 12 5 18 6.5 18 7 15.87 9 12 9M22 18H20V22H18V18H16L19 15L22 18Z"/>
</vector>

0 comments on commit 5286283

Please sign in to comment.