From 76a4aff37e2ce7a048f9260d44ccc7b351d131b1 Mon Sep 17 00:00:00 2001 From: houxg Date: Mon, 26 Dec 2016 15:04:22 +0800 Subject: [PATCH] add migration procedure --- .../houxg/leamonax/database/AppDataBase.java | 48 ++++++++++++++++++- .../org/houxg/leamonax/model/Account.java | 7 +-- 2 files changed, 51 insertions(+), 4 deletions(-) diff --git a/app/src/main/java/org/houxg/leamonax/database/AppDataBase.java b/app/src/main/java/org/houxg/leamonax/database/AppDataBase.java index 53129b3..10fc0c7 100644 --- a/app/src/main/java/org/houxg/leamonax/database/AppDataBase.java +++ b/app/src/main/java/org/houxg/leamonax/database/AppDataBase.java @@ -5,10 +5,12 @@ import android.text.TextUtils; import com.raizlabs.android.dbflow.annotation.Database; import com.raizlabs.android.dbflow.annotation.Migration; +import com.raizlabs.android.dbflow.sql.SQLiteType; import com.raizlabs.android.dbflow.sql.language.Join; import com.raizlabs.android.dbflow.sql.language.NameAlias; import com.raizlabs.android.dbflow.sql.language.SQLite; import com.raizlabs.android.dbflow.sql.language.property.IProperty; +import com.raizlabs.android.dbflow.sql.migration.AlterTableMigration; import com.raizlabs.android.dbflow.sql.migration.BaseMigration; import com.raizlabs.android.dbflow.structure.database.DatabaseWrapper; @@ -80,7 +82,51 @@ public class AppDataBase { } } - //TODO:copy the value of lastUsn, save it as the noteUsn and notebookUsn + @Migration(version = 3, priority = 1, database = AppDataBase.class) + public static class AddUsnColumn extends AlterTableMigration { + + public AddUsnColumn(Class table) { + super(table); + } + + @Override + public void onPreMigrate() { + super.onPreMigrate(); + addColumn(SQLiteType.INTEGER, "noteUsn"); + addColumn(SQLiteType.INTEGER, "notebookUsn"); + } + } + + @Migration(version = 3, priority = 0, database = AppDataBase.class) + public static class UpdateUsn extends BaseMigration { + + @Override + public void migrate(DatabaseWrapper database) { + Cursor cursor = SQLite.select() + .from(Account.class) + .query(database); + if (cursor == null) { + return; + } + int idIndex = cursor.getColumnIndex("id"); + int usnIndex = cursor.getColumnIndex("lastUsn"); + while (cursor.moveToNext()) { + int lastUsn = cursor.getInt(usnIndex); + int id = cursor.getInt(idIndex); + Account account = SQLite.select() + .from(Account.class) + .where(Account_Table.id.eq(id)) + .querySingle(database); + if (account != null) { + account.setNoteUsn(lastUsn); + account.setNotebookUsn(lastUsn); + account.update(database); + } + } + cursor.close(); + database.execSQL(""); + } + } public static void deleteNoteByLocalId(long localId) { SQLite.delete().from(Note.class) diff --git a/app/src/main/java/org/houxg/leamonax/model/Account.java b/app/src/main/java/org/houxg/leamonax/model/Account.java index bc4ec56..55ce555 100644 --- a/app/src/main/java/org/houxg/leamonax/model/Account.java +++ b/app/src/main/java/org/houxg/leamonax/model/Account.java @@ -43,9 +43,6 @@ public class Account extends BaseModel { String accessToken = ""; @Column(name = "defaultEditor") int defaultEditor = EDITOR_MARKDOWN; - @Column(name = "lastUsn") - @SerializedName("LastSyncUsn") - int lastSyncUsn; @Column(name = "host") @SerializedName("Host") String host = ""; @@ -54,6 +51,10 @@ public class Account extends BaseModel { @Column(name = "notebookUsn") int notebookUsn; + @Deprecated + @Column(name = "lastUsn") + @SerializedName("LastSyncUsn") + int lastSyncUsn; public Account() { }