Schema Changes
The ObjectBox C / C++ database comes with automatic schema migration. Learn all about it here.
ObjectBox manages its data model (schema) mostly automatically. The data model is defined by the Entities you define in FlatBuffer schema files. When you add or remove entities or properties of your entities, ObjectBox takes care of those changes when you launch objectbox-generator
on the changed .fbs
file without any further action needed 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, 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. The same applies when renaming properties. We'll show an example for both an entity and a property rename.
Step 1: Add an empty objectbox:uid
annotation to the entity/property you want to rename:
Step 2: Re-generate ObjectBox code for the file using objectbox-generator [-c|-cpp] schema.fbs
. 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. It's because you have two options, either renaming the property or resetting (clearing) it's stored data. 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 as a value of the uid
annotation when renaming your entity/property to achieve the same effect.
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
It's just adding an additional property - this solution useful if you need to do the data migration manually 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 (no data stored)
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