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

[DB] Add database migrations (#64)

This commit is contained in:
Mikołaj Pich 2018-03-11 18:18:33 +01:00 committed by Rafał Borcz
parent 3799fa910b
commit a0313827ce
4 changed files with 24 additions and 8 deletions

View File

@ -80,6 +80,7 @@ dependencies {
implementation 'eu.davidea:flexible-adapter-ui:1.0.0-b1'
implementation 'org.apache.commons:commons-collections4:4.1'
implementation 'org.greenrobot:greendao:3.2.2'
implementation 'com.github.yuweiguocn:GreenDaoUpgradeHelper:v2.0.2'
implementation 'com.jakewharton:butterknife:8.8.1'
implementation 'joda-time:joda-time:2.9.9'
implementation 'com.google.dagger:dagger-android:2.14.1'

View File

@ -3,20 +3,26 @@ package io.github.wulkanowy.data.db.dao;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import com.github.yuweiguocn.library.greendao.MigrationHelper;
import org.greenrobot.greendao.database.Database;
import org.greenrobot.greendao.database.StandardDatabase;
import javax.inject.Inject;
import javax.inject.Singleton;
import io.github.wulkanowy.BuildConfig;
import io.github.wulkanowy.data.db.dao.entities.AccountDao;
import io.github.wulkanowy.data.db.dao.entities.DaoMaster;
import io.github.wulkanowy.data.db.dao.entities.GradeDao;
import io.github.wulkanowy.data.db.dao.entities.SubjectDao;
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 {
public class DbHelper extends DaoMaster.OpenHelper {
private SharedPrefContract sharedPref;
@ -28,19 +34,28 @@ public class DbHelper extends DaoMaster.DevOpenHelper {
}
@Override
@SuppressWarnings("unchecked")
public void onUpgrade(Database db, int oldVersion, int newVersion) {
cleanUserData(db, oldVersion, newVersion);
MigrationHelper.DEBUG = BuildConfig.DEBUG;
MigrationHelper.migrate(db, new MigrationHelper.ReCreateAllTableListener() {
@Override
public void onCreateAllTables(Database db, boolean ifNotExists) {
DaoMaster.createAllTables(db, ifNotExists);
}
@Override
public void onDropAllTables(Database db, boolean ifExists) {
DaoMaster.dropAllTables(db, ifExists);
}
}, AccountDao.class, SubjectDao.class, GradeDao.class);
}
@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);
Database database = new StandardDatabase(db);
DaoMaster.dropAllTables(database, true);
onCreate(database);
sharedPref.setCurrentUserId(0);
LogUtils.info("Cleaning user data oldVersion=" + oldVersion + " newVersion=" + newVersion);
}
}

View File

@ -18,7 +18,6 @@ import eu.davidea.flexibleadapter.FlexibleAdapter;
import eu.davidea.flexibleadapter.items.AbstractExpandableHeaderItem;
import eu.davidea.viewholders.ExpandableViewHolder;
import io.github.wulkanowy.R;
import io.github.wulkanowy.data.db.dao.entities.AttendanceLesson;
import io.github.wulkanowy.data.db.dao.entities.Day;
public class AttendanceHeaderItem

View File

@ -36,6 +36,7 @@ allprojects {
jcenter()
mavenCentral()
google()
maven { url "https://jitpack.io" }
}
}