0

According to this stack overflow greenDao Schema Upgrade

The guy "DiscDev" answer his own question but I got a question,on the onUpgrade method of these following code :

if(oldVersion == 3 && newVersion == 4){
   boolean ifNotExists = false;

   //Leave old tables alone and only create ones that didn't exist
   //in the previous schema
     NewTable1Dao.createTable(db, ifNotExists);
     NewTable2Dao.createTable(db, ifNotExists);
     NewTable3Dao.createTable(db, ifNotExists);
     NewTable4Dao.createTable(db, ifNotExists);
} else {
     dropAllTables(db, true);
     onCreate(db);
}

the number of old version and new version how did he know the old version is 3 and new version is 4? And also why it needs to be hard code instead of declaration of constant number?

Community
  • 1
  • 1

1 Answers1

0

The oldVersion and newVersion are provided by the OpenHelper onUpgrade method. It provides oldVersion as the current version of the db the user has, and newVersion as the version that they are currently upgrading to.

You can set the current version, which is what newVersion will be, in the green dao generator.

public static void main(String[] args) throws Exception {
    int currentVersion  = 4;
    Schema schema = new Schema(currentVersion, "com.example.app.model.generated");

    new DaoGenerator().generateAll(schema, "app/src/main/java/");
}
Tyler Pfaff
  • 4,900
  • 9
  • 47
  • 62
  • It is not the best answer. because if I just update the current version, all of the db will be get destroyed for example favourite data. (drop all tables) – Andrew Indayang Apr 07 '16 at 23:25
  • What you do in the onUpgrade is up to you, you don't have to drop anything at all. You can handle the migration of the schema from one version to the next. – Tyler Pfaff Apr 08 '16 at 00:47
  • because it is generated by the daogenerator. it will drop all table if you add new entity. If for example you have generate 1 APK and try to mark some thing as your favourite and added that item into your favourite list. and next, try add new table or new entity into your DB and then, generate them and install it without uninstall the old version by downloading the APK. it won't crash the app but it will delete all of your favourite list as well. so to prevent that check on my answer. it won't delete anything just adding new entity that you newly added. – Andrew Indayang Apr 08 '16 at 05:13