forked from github/wulkanowy-mirror
Add cleaning user data on upgrade and downgrade (#60)
This commit is contained in:
parent
c3803b1c96
commit
69fc4bf874
@ -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);
|
||||
}
|
||||
}
|
||||
|
@ -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);
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user