GreenDaoUpgradeHelper

repository·master·Indexed 23 days ago

https://github.com/yuweiguocn/greendaoupgradehelper

A utility designed to simplify and automate database schema migrations for greenDAO users. It provides the MigrationHelper class to handle table upgrades with minimal code by extending DaoMaster.OpenHelper and implementing the onUpgrade method. Supports greenDAO 3.0 and above (v2.2.1) as well as versions below 3.0 (v1.0.1).

Tokens
2K
Snippets
6
Records
8
Agent score
31%

What's inside GreenDaoUpgradeHelper

  1. Install GreenDaoUpgradeHelper

    master

    To use GreenDaoUpgradeHelper, you must first add the JitPack repository to your project's root build.gradle file, then add the appropriate dependency based on your version of greenDAO.

    1. Add JitPack Repository

    In your root build.gradle file, add maven { url "https://jitpack.io" } to the allprojects.repositories block.

    2. Add Dependency

    Choose the dependency block that matches your greenDAO version:

    • For greenDAO 3.0 and above: Use io.github.yuweiguocn:GreenDaoUpgradeHelper:v2.2.1.
    • For greenDAO 3.0 and below: Use com.github.yuweiguocn:GreenDaoUpgradeHelper:v1.0.1.
  2. Implement database upgrades with MigrationHelper

    master

    To handle database schema changes, extend DaoMaster.OpenHelper and implement the onUpgrade method. Inside onUpgrade, call MigrationHelper.migrate(...) to automate the migration process.

    Usage Pattern

    1. Create a class extending DaoMaster.OpenHelper.
    2. In onUpgrade, call MigrationHelper.migrate.
    3. Provide a MigrationHelper.ReCreateAllTableListener implementation that calls DaoMaster.createAllTables and DaoMaster.dropAllTables.
    4. Pass all your generated Dao classes as arguments to the migrate method.

    To view migration logs during development, set MigrationHelper.DEBUG = true.

    public class MySQLiteOpenHelper extends DaoMaster.OpenHelper {
        public MySQLiteOpenHelper(Context context, String name, SQLiteDatabase.CursorFactory factory) {
            super(context, name, factory);
        }
    
        @Override
        public void onUpgrade(Database db, int oldVersion, int newVersion) {
            // MigrationHelper.DEBUG = true; // Enable to see log info
            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);
                }
            }, TestDataDao.class, TestData2Dao.class, TestData3Dao.class);
        }
    }
    
    // Initialization
    MySQLiteOpenHelper helper = new MySQLiteOpenHelper(this, "test.db", null);
    daoMaster = new DaoMaster(helper.getWritableDatabase());
  3. Implement database migration with MigrationHelper.migrate()

    master

    To handle database upgrades, create a class that extends DaoMaster.OpenHelper and implement the onUpgrade method. Inside onUpgrade, call MigrationHelper.migrate() using a ReCreateAllTableListener. This listener allows you to define how tables are created or dropped during the migration process. You must pass all your DAO classes as arguments to the migrate method.

    public class MySQLiteOpenHelper extends DaoMaster.OpenHelper {
        public MySQLiteOpenHelper(Context context, String name, SQLiteDatabase.CursorFactory factory) {
            super(context, name, factory);
        }
    
        @Override
        public void onUpgrade(Database db, int oldVersion, int newVersion) {
            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);
                }
            }, TestDataDao.class, TestData2Dao.class, TestData3Dao.class);
        }
    }
  4. Add GreenDaoUpgradeHelper dependencies

    master

    Add the appropriate dependency based on your version of greenDAO.

    For greenDAO 3.0 and above: Use io.github.yuweiguocn:GreenDaoUpgradeHelper:v2.2.1.

    For greenDAO versions prior to 3.0: Use com.github.yuweiguocn:GreenDaoUpgradeHelper:v1.0.1.

    // For greenDAO 3.0+
    dependencies {
        compile 'org.greenrobot:greendao:3.2.0'
        compile 'io.github.yuweiguocn:GreenDaoUpgradeHelper:v2.2.1'
    }
    
    // For greenDAO < 3.0
    dependencies {
        compile 'de.greenrobot:greendao:2.0.0'
        compile 'com.github.yuweiguocn:GreenDaoUpgradeHelper:v1.0.1'
    }
  5. Configure ProGuard rules for GreenDaoUpgradeHelper

    master

    If you use ProGuard or R8 for code obfuscation, you must add the following rule to ensure that the createTable and dropTable methods in your generated DAO classes are not stripped away, as MigrationHelper relies on them via reflection.

    -keepclassmembers class * extends org.greenrobot.greendao.AbstractDao {
        public static void dropTable(org.greenrobot.greendao.database.Database, boolean);
        public static void createTable(org.greenrobot.greendao.database.Database, boolean);
    }
  6. Configure ProGuard rules for GreenDaoUpgradeHelper

    master

    To prevent ProGuard from obfuscating the necessary greenDAO methods used by the migration helper, add the following rule to your ProGuard configuration:

    -keepclassmembers class * extends org.greenrobot.greendao.AbstractDao {
        public static void dropTable(org.greenrobot.greendao.database.Database, boolean);
        public static void createTable(org.greenrobot.greendao.database.Database, boolean);
    }