We had to migrate one of the field-params in an entity from long
to BigDecimal
. Migration is quite smooth but there is a problem; we want to keep previous values to be set to the migrated field. But as soon as ObjectBox
is initialized it defaults migrated field to the default value of the current type, in our case, to null
.
Say we had:
Id (long) | Name |
---|---|
123 | Random Name |
After migration we got:
Id (String) | Name |
---|---|
null | Random Name |
Is there any possible way to migrate without losing values on migrated fields?
A side note: I have used a converter to keep the BigDecimal
values since ObjectBox
doesn't support BigDecimal
Converter class:
public class BigIntegerStringConverter implements PropertyConverter<BigInteger, String> {
@Override
public BigInteger convertToEntityProperty(String databaseValue) {
return databaseValue == null ? null : new BigInteger(databaseValue);
}
@Override
public String convertToDatabaseValue(BigInteger entityProperty) {
return String.valueOf(entityProperty);
}
}
Usage:
@Convert(converter = BigIntegerStringConverter.class, dbType = String.class)
@Uid(XXXXXXXX)
BigInteger tigerId;
Unfortunately, type migration where old data is kept is not supported in ObjectBox.
Reference: https://github.com/objectbox/objectbox-java/issues/971