Hi,
first of all I'm really impressed how ObjectBox works (especially threading). But I have a question regarding to migration. I want to change field name and migrate data from old field to new one. Unfortunately data from old field name are lost after migration for objects stored in database before migration. I don't touch default.json file, only fields in class using shortcut in IDE. How to properly perform such a migration?
Before migration:
@Entity
public class Data {
@Id
private long id;
private String name;
@Generated(hash = 1566949835)
public Data(long id, String name) {
this.id = id;
this.name = name;
}
@Generated(hash = 2135787902)
public Data() {
}
}
After migration:
@Entity
public class Data {
@Id
private long id;
private String name2;
@Generated(hash = 284796816)
public Data(long id, String name2) {
this.id = id;
this.name2 = name2;
}
@Generated(hash = 2135787902)
public Data() {
}
}
Best regards
Aleksander Mielczarek
I just added this to the FAQ - more extensive docs will follow. In short put @Property(uid=1234) to the renamed property, where 1234 has to be looked up in objectbox-models/default.json.
Thank you for response. I'm getting now following error:
Error:Execution failed for task ':app:objectbox'.
> Value for uid should be of type long (could not convert from class org.eclipse.jdt.core.dom.NumberLiteral). Note: only inline constants are supported.
I used id from default.json as uid in @Property. I don't have any uid in default.json.
Entity and default.json from my sample project:
@Entity
public class Notification {
@Id
private long id;
@Property(uid = 4365497794674678185L)
private String title;
private String body;
@Convert(converter = LocalDateTimeConverter.class, dbType = String.class)
private LocalDateTime date;
private boolean read;
}
{
"_note1": "KEEP THIS FILE! Check it into a version control system (VCS) like git.",
"_note2": "ObjectBox manages crucial IDs for your object model. See docs for details.",
"_note3": "If you have VCS merge conflicts, you must resolve them according to ObjectBox docs.",
"entities": [
{
"id": "1:3697389786614689332",
"lastPropertyId": "5:7675120440086358920",
"name": "Notification",
"properties": [
{
"id": "1:7960257050845497672",
"name": "id"
},
{
"id": "2:4365497794674678185",
"name": "title"
},
{
"id": "3:5522414989386687706",
"name": "body"
},
{
"id": "4:2283581152000093700",
"name": "date"
},
{
"id": "5:7675120440086358920",
"name": "read"
}
]
}
],
"lastEntityId": "1:3697389786614689332",
"lastIndexId": "0:0",
"lastSequenceId": "0:0",
"modelVersion": 2,
"retiredEntityUids": [],
"retiredIndexUids": [],
"retiredPropertyUids": [],
"version": 1
}
Confirmed.
Use workaround until it is fixed: @Property(nameInDb = "oldName") when renaming properties.
Thank you, I'll check it later.
If you don't mind I have another question about migrations. Let's assume following situation:
Before migration:
@Entity
public class Data {
@Id
private long id;
@Relation
private OtherData otherData;
}
After migration:
@Entity
public class Data {
@Id
private long id;
@Relation(idProperty = "id")
private List<OtherData> othersData;
}
Is it possible that single data before migration appears as as list element after migration? Basically is it possible to add some extra logic to migrations?
Migrating that involve changing the type is not possible in general, this also is true for type of relation. If there are many requests for this feature, we might consider.
Ok, I just see ObjectBox as Realm alternative where such migrations are possible.
You are referring to manual migrations I guess? We have some code for that, but not publicly accessible. First priority is to make automatic migrations smart (enough).
Thank you for answer. Yes, I was refereeing to manual migrations. It would be great to have this feature.
Fixed for upcoming 0.9.10. API changed slightly: now it is @Uid(1234) for both properties and entities.
@greenrobot I wondering if there are a possibility to make custom rules for migrations. I checked documentation at http://objectbox.io/documentation/objectbox-entity-property-migration/ and found that we can use old string field for new one as int. But what if because of some reason String field literally contains string instead of number. Can I make my own rule, to make this field contain the lenght of this string for example? Thank you, looking forward.
I agree with @Yazon2006 . It's will be really handy, if there were a possibility to set custom migration rules. For example: if I had Double before and now it's Integer because I figured out, that fraction is always 0 and I want just to migrate my model from Double to storing Integer without messing with @Uid annotations and I perfectly OK with losing fraction part.
Most helpful comment
@greenrobot I wondering if there are a possibility to make custom rules for migrations. I checked documentation at http://objectbox.io/documentation/objectbox-entity-property-migration/ and found that we can use old string field for new one as int. But what if because of some reason String field literally contains string instead of number. Can I make my own rule, to make this field contain the lenght of this string for example? Thank you, looking forward.