diff --git a/app/src/main/java/io/github/wulkanowy/data/db/dao/DbHelper.java b/app/src/main/java/io/github/wulkanowy/data/db/dao/DbHelper.java index e4007e29..9392bae5 100644 --- a/app/src/main/java/io/github/wulkanowy/data/db/dao/DbHelper.java +++ b/app/src/main/java/io/github/wulkanowy/data/db/dao/DbHelper.java @@ -1,8 +1,10 @@ package io.github.wulkanowy.data.db.dao; import android.content.Context; +import android.database.sqlite.SQLiteDatabase; import org.greenrobot.greendao.database.Database; +import org.greenrobot.greendao.database.StandardDatabase; import javax.inject.Inject; import javax.inject.Singleton; @@ -11,6 +13,7 @@ import io.github.wulkanowy.data.db.dao.entities.DaoMaster; import io.github.wulkanowy.data.db.shared.SharedPrefContract; import io.github.wulkanowy.di.annotations.ApplicationContext; import io.github.wulkanowy.di.annotations.DatabaseInfo; +import io.github.wulkanowy.utils.LogUtils; @Singleton public class DbHelper extends DaoMaster.DevOpenHelper { @@ -26,7 +29,18 @@ public class DbHelper extends DaoMaster.DevOpenHelper { @Override public void onUpgrade(Database db, int oldVersion, int newVersion) { - super.onUpgrade(db, oldVersion, newVersion); + cleanUserData(db, oldVersion, newVersion); + } + + @Override + public void onDowngrade(SQLiteDatabase db, int oldVersion, int newVersion) { + cleanUserData(new StandardDatabase(db), oldVersion, newVersion); + } + + private void cleanUserData(Database database, int oldVersion, int newVersion) { + LogUtils.info("Cleaning user data oldVersion=" + oldVersion + " newVersion=" + newVersion); + DaoMaster.dropAllTables(database, true); + onCreate(database); sharedPref.setCurrentUserId(0); } } diff --git a/app/src/main/java/io/github/wulkanowy/utils/LogUtils.java b/app/src/main/java/io/github/wulkanowy/utils/LogUtils.java index f3b5b3ec..f59bbf64 100644 --- a/app/src/main/java/io/github/wulkanowy/utils/LogUtils.java +++ b/app/src/main/java/io/github/wulkanowy/utils/LogUtils.java @@ -19,4 +19,8 @@ public final class LogUtils { public static void error(String message) { Log.e(AppConstant.APP_NAME, message); } + + public static void info(String message) { + Log.i(AppConstant.APP_NAME, message); + } }