forked from github/wulkanowy-mirror
[DB] Add database migrations (#64)
This commit is contained in:
parent
3799fa910b
commit
a0313827ce
@ -80,6 +80,7 @@ dependencies {
|
|||||||
implementation 'eu.davidea:flexible-adapter-ui:1.0.0-b1'
|
implementation 'eu.davidea:flexible-adapter-ui:1.0.0-b1'
|
||||||
implementation 'org.apache.commons:commons-collections4:4.1'
|
implementation 'org.apache.commons:commons-collections4:4.1'
|
||||||
implementation 'org.greenrobot:greendao:3.2.2'
|
implementation 'org.greenrobot:greendao:3.2.2'
|
||||||
|
implementation 'com.github.yuweiguocn:GreenDaoUpgradeHelper:v2.0.2'
|
||||||
implementation 'com.jakewharton:butterknife:8.8.1'
|
implementation 'com.jakewharton:butterknife:8.8.1'
|
||||||
implementation 'joda-time:joda-time:2.9.9'
|
implementation 'joda-time:joda-time:2.9.9'
|
||||||
implementation 'com.google.dagger:dagger-android:2.14.1'
|
implementation 'com.google.dagger:dagger-android:2.14.1'
|
||||||
|
@ -3,20 +3,26 @@ package io.github.wulkanowy.data.db.dao;
|
|||||||
import android.content.Context;
|
import android.content.Context;
|
||||||
import android.database.sqlite.SQLiteDatabase;
|
import android.database.sqlite.SQLiteDatabase;
|
||||||
|
|
||||||
|
import com.github.yuweiguocn.library.greendao.MigrationHelper;
|
||||||
|
|
||||||
import org.greenrobot.greendao.database.Database;
|
import org.greenrobot.greendao.database.Database;
|
||||||
import org.greenrobot.greendao.database.StandardDatabase;
|
import org.greenrobot.greendao.database.StandardDatabase;
|
||||||
|
|
||||||
import javax.inject.Inject;
|
import javax.inject.Inject;
|
||||||
import javax.inject.Singleton;
|
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.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.data.db.shared.SharedPrefContract;
|
||||||
import io.github.wulkanowy.di.annotations.ApplicationContext;
|
import io.github.wulkanowy.di.annotations.ApplicationContext;
|
||||||
import io.github.wulkanowy.di.annotations.DatabaseInfo;
|
import io.github.wulkanowy.di.annotations.DatabaseInfo;
|
||||||
import io.github.wulkanowy.utils.LogUtils;
|
import io.github.wulkanowy.utils.LogUtils;
|
||||||
|
|
||||||
@Singleton
|
@Singleton
|
||||||
public class DbHelper extends DaoMaster.DevOpenHelper {
|
public class DbHelper extends DaoMaster.OpenHelper {
|
||||||
|
|
||||||
private SharedPrefContract sharedPref;
|
private SharedPrefContract sharedPref;
|
||||||
|
|
||||||
@ -28,19 +34,28 @@ public class DbHelper extends DaoMaster.DevOpenHelper {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
@SuppressWarnings("unchecked")
|
||||||
public void onUpgrade(Database db, int oldVersion, int newVersion) {
|
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
|
@Override
|
||||||
public void onDowngrade(SQLiteDatabase db, int oldVersion, int newVersion) {
|
public void onDowngrade(SQLiteDatabase db, int oldVersion, int newVersion) {
|
||||||
cleanUserData(new StandardDatabase(db), oldVersion, newVersion);
|
Database database = new StandardDatabase(db);
|
||||||
}
|
|
||||||
|
|
||||||
private void cleanUserData(Database database, int oldVersion, int newVersion) {
|
|
||||||
LogUtils.info("Cleaning user data oldVersion=" + oldVersion + " newVersion=" + newVersion);
|
|
||||||
DaoMaster.dropAllTables(database, true);
|
DaoMaster.dropAllTables(database, true);
|
||||||
onCreate(database);
|
onCreate(database);
|
||||||
sharedPref.setCurrentUserId(0);
|
sharedPref.setCurrentUserId(0);
|
||||||
|
|
||||||
|
LogUtils.info("Cleaning user data oldVersion=" + oldVersion + " newVersion=" + newVersion);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -18,7 +18,6 @@ import eu.davidea.flexibleadapter.FlexibleAdapter;
|
|||||||
import eu.davidea.flexibleadapter.items.AbstractExpandableHeaderItem;
|
import eu.davidea.flexibleadapter.items.AbstractExpandableHeaderItem;
|
||||||
import eu.davidea.viewholders.ExpandableViewHolder;
|
import eu.davidea.viewholders.ExpandableViewHolder;
|
||||||
import io.github.wulkanowy.R;
|
import io.github.wulkanowy.R;
|
||||||
import io.github.wulkanowy.data.db.dao.entities.AttendanceLesson;
|
|
||||||
import io.github.wulkanowy.data.db.dao.entities.Day;
|
import io.github.wulkanowy.data.db.dao.entities.Day;
|
||||||
|
|
||||||
public class AttendanceHeaderItem
|
public class AttendanceHeaderItem
|
||||||
|
@ -36,6 +36,7 @@ allprojects {
|
|||||||
jcenter()
|
jcenter()
|
||||||
mavenCentral()
|
mavenCentral()
|
||||||
google()
|
google()
|
||||||
|
maven { url "https://jitpack.io" }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user