GreenDaoUpgradeHelper icon indicating copy to clipboard operation
GreenDaoUpgradeHelper copied to clipboard

Please wrap migrate() in a transaction and throws a SQLException

Open yukoba opened this issue 8 years ago • 5 comments

Could you please wrap migrate() in a transaction and throws a SQLException? The current code destroys the database when a SQLException is thrown.

yukoba avatar Dec 11 '16 11:12 yukoba

I meet the same problem...

Bertkiing avatar Jan 11 '17 09:01 Bertkiing

What I want is this. https://github.com/yukoba/GreenDaoUpgradeHelper/commit/431d00eacdb1a8e571074acc7256fac360ab95ac

yukoba avatar Jan 11 '17 09:01 yukoba

I think it can help you. Good luck!!!

http://stackoverflow.com/questions/16154113/greendao-schema-upgrade

Bertkiing avatar Jan 11 '17 10:01 Bertkiing

thanks ☺ @yukoba

xuzc avatar Mar 17 '17 08:03 xuzc

@yukoba i find that OpenHelper.onUpgrade() will be execute in transaction by default.

@Override
public void onUpgrade(Database db, int oldVersion, int newVersion) {
    boolean isInTransaction = db.inTransaction();//Here isInTransaction=true
}

My Conclusion is: 1.There is no need to begin a transaction 2.Throw all exception and catch it in onUpgrade()(Don't catch SQLException in MigrationHelper).

//MigrationHelper.java .... public static void migrate(Database database, Class extends AbstractDao, ?>>... daoClasses) throws NoSuchMethodException, IllegalAccessException, InvocationTargetException { .... } ....

//MyOpenHelper.java ...... @Override public void onUpgrade(Database db, int oldVersion, int newVersion) { Log.i(TAG, "Upgrading schema from version " + oldVersion + " to " + newVersion); try { MigrationHelper.migrate(db,XXDAO.class); Log.i(TAG, "Upgrading success!!!"); } catch (Exception e) { e.printStackTrace(); Log.e(TAG, "Upgrading failed, drop all tables!!!"); DaoMaster.dropAllTables(db, true); onCreate(db); } } ....

zzzhouzhong avatar Jul 03 '17 02:07 zzzhouzhong