Schema changes
ObjectBox manages its data model (schema) mostly automatically. ObjectBox db supports automatic schema migration to make data persistence as easy as possible for application developers.
The data model is defined by the entity structs you define. When you add or remove entities or properties of your entities, ObjectBox takes care of those changes without any further action from you.
For other changes like renaming or changing the type, ObjectBox needs extra information to make things unambiguous. This works using unique identifiers (UIDs) specified by the uid annotation, as we will see below.
Renaming Entities and Properties
So why do we need that UID annotation? If you simply rename an entity struct, ObjectBox only sees that the old entity is gone and a new entity is available. This can be interpreted in two ways:
The old entity is removed and a new entity should be added, the old data is discarded. This is the default behavior of ObjectBox.
The entity was renamed, the old data should be re-used.
So to tell ObjectBox to do a rename instead of discarding your old entity and data, you need to make sure it knows that this is the same entity and not a new one. You do that by attaching the internal UID to the entity.
For properties, the process is the same, but instead of the comment, you just use standard Go tags. We are showing both cases in the following example.
Step 1: Add an empty `objectbox:"uid"` annotation to the entity/property you want to rename:
Step 2: Re-generate ObjectBox code for the project using go generate ./...
in your project directory. The generation will fail with an error message that gives you the current UID of the entity/property:
Note how for a property, the output is slightly different and, besides support for renaming, it provides a newly generated UID you can use to effectively reset (clean) the stored data on the property. See Reset data - new UID on a property for more details.
Step 3: Apply the UID printed in the error message to your entity/property:
Step 4: The last thing to do is the actual rename on the language level:
You can now use your renamed entity/property as expected and all existing data will still be there.
Note: Instead of the above you can also find the UID of the entity/property in the objectbox-model.json
file yourself and add it together with the @Uid annotation before renaming your entity/property.
Changing Property Types
ObjectBox does not support migrating existing property data to a new type. You will have to take care of this yourself, e.g. by keeping the old property and adding some migration logic.
New property, different name
This solution useful if you need data migration or just want to keep the old data around.
Reset data - new UID on a property
This solution useful if you don't care about the original data at all = it will be lost.
Step 1: Add an empty `objectbox:"uid"` annotation to the property you want to reset:
Step 2: Re-generate ObjectBox code for the project using go generate ./...
in your project directory. The generation will fail with an error message that gives you the current UID of the property:
Step 3: Apply the UID printed in the error message to your property (and change its type):
You can now use the property in your entity as if it was a new one.
The original property data isn't really removed right away on old stored objects but will be empty when read from DB and overwritten (thus finally lost) next time an object is written.
Last updated