0

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;
Farid
  • 2,317
  • 1
  • 20
  • 34

3 Answers3

1

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.

Source: https://docs.objectbox.io/advanced/data-model-updates#changing-property-types

Uwe - ObjectBox
  • 1,012
  • 1
  • 7
  • 11
  • `You will have to take care of this yourself, e.g. by keeping the old property and adding some migration logic.` but how? None of the methods described there gives any hint on how to do it without losing old data – Farid Mar 30 '21 at 06:33
1

@Farid a manual migration could look somewhat like this:

  1. add a new field to the model, with the new type you want to use, e.g. newField
  2. add code that updates all objects, reading oldField and writing the appropriate value to newField
  3. remove oldField from the model, now that all the data is migrated
  4. optionally, you can follow the docs on how to rename newField to anything you want
vaind
  • 1,642
  • 10
  • 19
  • What I want to do is type migration that will keep previous data which, apparently, is not supported in ObjectBox mentioned in this issue: https://github.com/objectbox/objectbox-java/issues/971. "optionally, you can follow the docs on how to rename newField to anything you want" I want to do *type-migration* not *property-renaming* – Farid Apr 09 '21 at 13:39
0

Unfortunately, type migration where old data is kept is not supported in ObjectBox.

Reference: https://github.com/objectbox/objectbox-java/issues/971

Farid
  • 2,317
  • 1
  • 20
  • 34