Skip to content

Instantly share code, notes, and snippets.

@brucetoo
Created August 19, 2015 03:27
Show Gist options
  • Save brucetoo/84805360be0082e9e064 to your computer and use it in GitHub Desktop.
Save brucetoo/84805360be0082e9e064 to your computer and use it in GitHub Desktop.
Realm数据库迁移实例
package com.brucetoo.realmdemo.realmmigration;
import android.app.Application;
import android.util.Log;
import io.realm.Realm;
import io.realm.RealmConfiguration;
import io.realm.RealmMigration;
import io.realm.RealmObject;
import io.realm.internal.ColumnType;
import io.realm.internal.Table;
/**
* Created by Bruce Too
* On 8/19/15.
* At 10:47
* Realm数据库迁移实例
*/
public class RealmMigrationDemo extends Application{
@Override
public void onCreate() {
super.onCreate();
//首先在初次数据库配置中声明 version,当然这个操作通常是在Application中完成
RealmConfiguration config = new RealmConfiguration.Builder(this)
.name("database")
.schemaVersion(0) //初始化版本的version = 0
.build();
//然后设置对person设置值
Realm realm = Realm.getInstance(config);
realm.beginTransaction();
Person person = realm.createObject(Person.class);
person.setFullName("fullname");
person.setAge(15);
realm.commitTransaction(); //提交事务后数据库就存在了保存的值
//假设现在数据库中Person字段需要添加一个参数 time,就需要做数据库的迁移操作了
/**
* 1.需要将默认配置中的schemaVersion在原来的基础上+1,在此是 0+1=1
* 2.配置迁移的Migration
*/
}
//假设在第一次Person的中的参数如下:
public class Person extends RealmObject {
private String fullName;
private int age;
private long time; //第二个版本添加的字段
public long getTime() {
return time;
}
public void setTime(long time) {
this.time = time;
}
public String getFullName() {
return fullName;
}
public void setFullName(String fullName) {
this.fullName = fullName;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}
public class Migration implements RealmMigration {
@Override
public long execute(Realm realm, long version) {
Log.e("version:",String.valueOf(version));
//从版本 0 升级到 1
/*
version 0
Person
private String fullName;
private int age;
version 1
private String fullName;
private int age;
private long time;
*/
if(version == 0){
//获取person类保存的table
Table personTable = realm.getTable(Person.class);
//添加一个字段 time
long timeIndex = personTable.addColumn(ColumnType.INTEGER,"time");
for(int i = 0; i < personTable.size(); i++){
//为每个time字段赋值
personTable.setString(timeIndex,i,"11111");
}
//版本号加1
version++;
}
/*********************如果还有版本迭代了很多次,则需要依次对每个版本的迁移做操作*******************************************/
/*
// Version 2
class Pet // add a new model class
String name;
String type;
class Person
String fullName;
int age;
RealmList<Pet> pets; // add an array property
*/
// Migrate from version 1 to version 2
if (version == 1) {
// Table personTable = realm.getTable(Person.class);
// Table petTable = realm.getTable(Pet.class);
// petTable.addColumn(ColumnType.STRING, "name");
// petTable.addColumn(ColumnType.STRING, "type");
// long petsIndex = personTable.addColumnLink(ColumnType.LINK_LIST, "pets", petTable);
// long fullNameIndex = getIndexForProperty(personTable, "fullName");
//
// for (int i = 0; i < personTable.size(); i++) {
// if (personTable.getString(fullNameIndex, i).equals("JP McDonald")) {
// personTable.getUncheckedRow(i).getLinkList(petsIndex).add(petTable.add("Jimbo", "dog"));
// }
// }
// version++;
}
/*
// Version 3
class Pet
String name;
int type; // type becomes int
class Person
String fullName;
RealmList<Pet> pets; // age and pets re-ordered
int age;
*/
// Migrate from version 2 to version 3
// if (version == 2) {
// Table petTable = realm.getTable(Pet.class);
// long oldTypeIndex = getIndexForProperty(petTable, "type");
// long typeIndex = petTable.addColumn(ColumnType.INTEGER, "type");
// for (int i = 0; i < petTable.size(); i++) {
// String type = petTable.getString(oldTypeIndex, i);
// if (type.equals("dog")) {
// petTable.setLong(typeIndex, i, 1);
// }
// else if (type.equals("cat")) {
// petTable.setLong(typeIndex, i, 2);
// }
// else if (type.equals("hamster")) {
// petTable.setLong(typeIndex, i, 3);
// }
// }
// petTable.removeColumn(oldTypeIndex);
// version++;
// }
// if(version == 3){
// Table personTable = realm.getTable(Person.class);
// long timeIndex = personTable.addColumn(ColumnType.STRING,"time");
// Log.e("version:",version+"");
// for(int i = 0; i < personTable.size(); i++){
// personTable.setString(timeIndex,i,"11111111");
// }
//// version++;
// }
return version;
}
private long getIndexForProperty(Table table, String name) {
for (int i = 0; i < table.getColumnCount(); i++) {
if (table.getColumnName(i).equals(name)) {
return i;
}
}
return -1;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment