GreenDaoUpgradeHelper
GreenDaoUpgradeHelper copied to clipboard
Please wrap migrate() in a transaction and throws a SQLException
Could you please wrap migrate() in a transaction and throws a SQLException? The current code destroys the database when a SQLException is thrown.
I meet the same problem...
What I want is this. https://github.com/yukoba/GreenDaoUpgradeHelper/commit/431d00eacdb1a8e571074acc7256fac360ab95ac
I think it can help you. Good luck!!!
http://stackoverflow.com/questions/16154113/greendao-schema-upgrade
thanks ☺ @yukoba
@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); } } ....