GreenDAO schema update and data migration?

后端 未结 7 2120
借酒劲吻你
借酒劲吻你 2020-12-12 10:14

I\'m evaluating GreenDAO for consideration in a commercial Android app I will be working on and wanted to determine the migration path for schema updates.

Am I corre

7条回答
  •  北海茫月
    2020-12-12 10:31

    I think that my answer to a similiar question can help with this approach. If you really need to migrate data I suggest you that if you for example need to deal with some constrains changes or things that are not supported in SQLite you write the migration yourself. For instance an example of a migrator helper (following the approach I did on the linked response) could be:

    public class DBMigrationHelper6 extends AbstractMigratorHelper {
    
    /* Upgrade from DB schema 6 to schema 7 , version numbers are just examples*/
    
    public void onUpgrade(SQLiteDatabase db) {
    
        /* Create a temporal table where you will copy all the data from the previous table that you need to modify with a non supported sqlite operation */
        db.execSQL("CREATE TABLE " + "'post2' (" + //
                "'_id' INTEGER PRIMARY KEY ," + // 0: id
                "'POST_ID' INTEGER UNIQUE ," + // 1: postId
                "'USER_ID' INTEGER," + // 2: userId
                "'VERSION' INTEGER," + // 3: version
                "'TYPE' TEXT," + // 4: type
                "'MAGAZINE_ID' TEXT NOT NULL ," + // 5: magazineId
                "'SERVER_TIMESTAMP' INTEGER," + // 6: serverTimestamp
                "'CLIENT_TIMESTAMP' INTEGER," + // 7: clientTimestamp
                "'MAGAZINE_REFERENCE' TEXT NOT NULL ," + // 8: magazineReference
                "'POST_CONTENT' TEXT);"); // 9: postContent
    
        /* Copy the data from one table to the new one */
        db.execSQL("INSERT INTO post2 (_id, POST_ID, USER_ID, VERSION, TYPE,  MAGAZINE_ID, SERVER_TIMESTAMP, CLIENT_TIMESTAMP, MAGAZINE_REFERENCE, POST_CONTENT)" +
                "   SELECT _id, POST_ID, USER_ID, VERSION, TYPE,  MAGAZINE_ID, SERVER_TIMESTAMP, CLIENT_TIMESTAMP, MAGAZINE_REFERENCE, POST_CONTENT FROM post;");
    
        /* Delete the previous table */
        db.execSQL("DROP TABLE post");
        /* Rename the just created table to the one that I have just deleted */
        db.execSQL("ALTER TABLE post2 RENAME TO post");
    
        /* Add Index/es if you want them */
        db.execSQL("CREATE INDEX " + "IDX_post_USER_ID ON post" +
                " (USER_ID);");
    
       }
    }
    

提交回复
热议问题