Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Unverified Commit d70471a0 authored by Arnau Mora's avatar Arnau Mora Committed by GitHub
Browse files

Enabled migration on database creation (#121)

parent b9238b20
Loading
Loading
Loading
Loading
+19 −3
Original line number Diff line number Diff line
@@ -34,6 +34,12 @@ class SyncWorker(
         */
        private const val FORCE_RESYNC = "forceResync"

        /**
         * An input data for the Worker that tells if only migration should be performed, without
         * fetching data.
         */
        private const val ONLY_MIGRATE = "onlyMigration"

        /**
         * Enqueues a sync job for immediate execution. If the sync is forced,
         * the "requires network connection" constraint won't be set.
@@ -41,10 +47,16 @@ class SyncWorker(
         * @param context      required for managing work
         * @param force        *true* enqueues the sync regardless of the network state; *false* adds a [NetworkType.CONNECTED] constraint
         * @param forceResync  *true* ignores all locally stored data and fetched everything from the server again
         * @param onlyMigrate  *true* only runs synchronization, without fetching data.
         */
        fun run(context: Context, force: Boolean = false, forceResync: Boolean = false) {
        fun run(context: Context, force: Boolean = false, forceResync: Boolean = false, onlyMigrate: Boolean = false) {
            val request = OneTimeWorkRequestBuilder<SyncWorker>()
                .setInputData(workDataOf(FORCE_RESYNC to forceResync))
                .setInputData(
                    workDataOf(
                        FORCE_RESYNC to forceResync,
                        ONLY_MIGRATE to onlyMigrate,
                    )
                )

            val policy: ExistingWorkPolicy = if (force) {
                Log.i(TAG, "Manual sync, ignoring network condition")
@@ -82,13 +94,17 @@ class SyncWorker(

    override suspend fun doWork(): Result {
        forceReSync = inputData.getBoolean(FORCE_RESYNC, false)
        Log.i(TAG, "Synchronizing (forceReSync=$forceReSync)")
        val onlyMigrate = inputData.getBoolean(ONLY_MIGRATE, false)
        Log.i(TAG, "Synchronizing (forceReSync=$forceReSync,onlyMigrate=$onlyMigrate)")

        provider = LocalCalendar.getCalendarProvider(applicationContext)
        try {
            // migrate old calendar-based subscriptions to database
            migrateLegacyCalendars()

            // Do not synchronize if onlyMigrate is true
            if (onlyMigrate) return Result.success()

            // update local calendars according to the subscriptions
            updateLocalCalendars()

+9 −1
Original line number Diff line number Diff line
@@ -10,6 +10,8 @@ import androidx.room.Database
import androidx.room.Room
import androidx.room.RoomDatabase
import androidx.room.TypeConverters
import androidx.sqlite.db.SupportSQLiteDatabase
import at.bitfire.icsdroid.SyncWorker
import at.bitfire.icsdroid.db.AppDatabase.Companion.getInstance
import at.bitfire.icsdroid.db.dao.CredentialsDao
import at.bitfire.icsdroid.db.dao.SubscriptionsDao
@@ -56,6 +58,12 @@ abstract class AppDatabase : RoomDatabase() {
                // create a new instance and save it
                val db = Room.databaseBuilder(context.applicationContext, AppDatabase::class.java, "icsx5")
                    .fallbackToDestructiveMigration()
                    .addCallback(object : Callback() {
                        override fun onCreate(db: SupportSQLiteDatabase) {
                            super.onCreate(db)
                            SyncWorker.run(context, onlyMigrate = true)
                        }
                    })
                    .build()
                instance = db
                return db