add migration procedure

This commit is contained in:
houxg
2016-12-26 15:04:22 +08:00
parent 52939699d9
commit 76a4aff37e
2 changed files with 51 additions and 4 deletions

View File

@@ -5,10 +5,12 @@ import android.text.TextUtils;
import com.raizlabs.android.dbflow.annotation.Database; import com.raizlabs.android.dbflow.annotation.Database;
import com.raizlabs.android.dbflow.annotation.Migration; 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.Join;
import com.raizlabs.android.dbflow.sql.language.NameAlias; import com.raizlabs.android.dbflow.sql.language.NameAlias;
import com.raizlabs.android.dbflow.sql.language.SQLite; import com.raizlabs.android.dbflow.sql.language.SQLite;
import com.raizlabs.android.dbflow.sql.language.property.IProperty; 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.sql.migration.BaseMigration;
import com.raizlabs.android.dbflow.structure.database.DatabaseWrapper; 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<Account> {
public AddUsnColumn(Class<Account> 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) { public static void deleteNoteByLocalId(long localId) {
SQLite.delete().from(Note.class) SQLite.delete().from(Note.class)

View File

@@ -43,9 +43,6 @@ public class Account extends BaseModel {
String accessToken = ""; String accessToken = "";
@Column(name = "defaultEditor") @Column(name = "defaultEditor")
int defaultEditor = EDITOR_MARKDOWN; int defaultEditor = EDITOR_MARKDOWN;
@Column(name = "lastUsn")
@SerializedName("LastSyncUsn")
int lastSyncUsn;
@Column(name = "host") @Column(name = "host")
@SerializedName("Host") @SerializedName("Host")
String host = ""; String host = "";
@@ -54,6 +51,10 @@ public class Account extends BaseModel {
@Column(name = "notebookUsn") @Column(name = "notebookUsn")
int notebookUsn; int notebookUsn;
@Deprecated
@Column(name = "lastUsn")
@SerializedName("LastSyncUsn")
int lastSyncUsn;
public Account() { public Account() {
} }