Do not rebuild all tables when upgrading GreenDao

Keywords: Database Gradle Java

The configuration of GreenDao is written in build.gradle as follows:

greendao {
    schemaVersion 1    //Version of < -- database, used to make changes during upgrade
    daoPackage 'com.zhuyefeng.green.gen'     //Package for storing auto generated code
    targetGenDir 'src/main/java'                  //...
}

GreenDao will automatically generate two classes: DaoMaster and DaoSession. DaoMaster also contains two internal classes, OpenHelper and DevOpenHelper.

Simple use (not recommended):

public class GreenDaoHelper {

    private static SQLiteDatabase database;
    private static DaoMaster daoMaster;
    private static DaoSession daoSession;
    //Singleton mode
    private static GreenDaoHelper mInstance;

    private GreenDaoHelper() {
        //Initialization suggestions are put in Application
        if (mInstance == null) {
            //Create database "info.db"
            DaoMaster.DevOpenHelper devOpenHelper = new DaoMaster.DevOpenHelper(MyApplication.getContext(), "info.db", null);
            //Get writable database
            database = devOpenHelper.getWritableDatabase();
            //Get database objects
            daoMaster = new DaoMaster(database);
            //Get Dao object manager
            daoSession = daoMaster.newSession();
        }
    }

    public static GreenDaoHelper getInstance() {
        if (mInstance == null) {
            //Secure asynchronous processing
            synchronized (GreenDaoHelper.class) {
                if (mInstance == null) {
                    mInstance = new GreenDaoHelper();
                }
            }
        }
        return mInstance;
    }

    public DaoMaster getDaoMaster() {
        return daoMaster;
    }

    public DaoSession getDaoSession() {
        return daoSession;
    }

}

If you use this directly, GreenDao will rebuild all tables in the database every time you upgrade the version number. The reason can be seen from the onUpgrade method in the DevOpenHelper class. The logic is to rebuild all tables.

If you want to update on demand every time you upgrade, it is recommended to write as follows:

public class GreenDaoHelper {

    private static SQLiteDatabase database;
    private static DaoMaster daoMaster;
    private static DaoSession daoSession;
    //Singleton mode
    private static GreenDaoHelper mInstance;

    private GreenDaoHelper() {
        //Initialization suggestions are put in Application
        if (mInstance == null) {
            //Create database "info.db"
            DaoMaster.OpenHelper openHelper = new DaoMaster.OpenHelper(MyApplication.getContext(), "info.db"){
                @Override
                public void onUpgrade(Database db, int oldVersion, int newVersion) {
                    if(oldVersion < 2) {
                            db.beginTransaction();
                            db.execSQL("...");
                            db.setTransactionSuccessful();
                            db.endTransaction();
                    }
                    if(oldVersion < 3) {
                            db.beginTransaction();
                            db.execSQL("...");
                            db.setTransactionSuccessful();
                            db.endTransaction();
                    }
                }
            };

            //Get writable database
            database = openHelper.getWritableDatabase();
            //Get database objects
            daoMaster = new DaoMaster(database);
            //Get Dao object manager
            daoSession = daoMaster.newSession();
        }
    }

    public static GreenDaoHelper getInstance() {
        if (mInstance == null) {
            //Secure asynchronous processing
            synchronized (GreenDaoHelper.class) {
                if (mInstance == null) {
                    mInstance = new GreenDaoHelper();
                }
            }
        }
        return mInstance;
    }

    public DaoMaster getDaoMaster() {
        return daoMaster;
    }

    public DaoSession getDaoSession() {
        return daoSession;
    }

}

Determine whether the old version is smaller than a certain version. If it is smaller than this version, upgrade the version.

Posted by richtux on Mon, 06 Jan 2020 09:25:30 -0800