1
0
mirror of https://github.com/wulkanowy/wulkanowy.git synced 2024-09-19 23:29:08 -05:00

Add cleaning user data on upgrade and downgrade (#60)

This commit is contained in:
Rafał Borcz 2018-03-06 20:14:54 +01:00 committed by Mikołaj Pich
parent c3803b1c96
commit 69fc4bf874
2 changed files with 19 additions and 1 deletions

View File

@ -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);
}
}

View File

@ -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);
}
}