hive icon indicating copy to clipboard operation
hive copied to clipboard

Migration in Hive 2.0.4

Open pl7nkton opened this issue 4 years ago • 4 comments

Question How is a migration performed properly?

Code sample

Let's say we have the following model:

@HiveType(typeId: 1)
class DailyChunk extends HiveObject {
  DailyChunk({
    required this.mood,
    required this.date,
  });

  @HiveField(0)
  double mood;

  @HiveField(1)
  DateTime date;
}

And would like to update it to the following:

@HiveType(typeId: 1)
class DailyChunk extends HiveObject {
  DailyChunk({
    required this.mood,
    required this.date,
    required this.energy,
  });

  @HiveField(0)
  double mood;

  @HiveField(1)
  DateTime date;

  @HiveField(2)
  double energy;
}

How would this be done properly without losing the data or getting null errors.

I read this documentation for Hive 0.1.0: Migration However, I couldn't find anything similar for Hive 2

Updating that model leads to the following error:

Unhandled Exception: type 'Null' is not a subtype of type 'double' in type cast
E/flutter (14764): #0      DailyChunkAdapter.read (package:libitrack/model/entities/daily_chunk.g.dart:22:25)
E/flutter (14764): #1      BinaryReaderImpl.read (package:hive/src/binary/binary_reader_impl.dart:326:33)
E/flutter (14764): #2      BinaryReaderImpl.readFrame (package:hive/src/binary/binary_reader_impl.dart:274:26)
E/flutter (14764): #3      FrameHelper.framesFromBytes (package:hive/src/binary/frame_helper.dart:21:26)
E/flutter (14764): #4      FrameIoHelper.framesFromFile (package:hive/src/io/frame_io_helper.dart:42:12)
E/flutter (14764): <asynchronous suspension>
E/flutter (14764): #5      StorageBackendVm.initialize (package:hive/src/backend/vm/storage_backend_vm.dart:86:11)
E/flutter (14764): <asynchronous suspension>
E/flutter (14764): #6      HiveImpl._openBox (package:hive/src/hive_impl.dart:116:9)
E/flutter (14764): <asynchronous suspension>
E/flutter (14764): #7      HiveImpl.openBox (package:hive/src/hive_impl.dart:145:12)
E/flutter (14764): <asynchronous suspension>
E/flutter (14764): #8      _AddDataScreenState._readData (package:libitrack/ui/add_data_screen.dart:100:15)
E/flutter (14764): <asynchronous suspension>
E/flutter (14764): 
E/flutter (14764): [ERROR:flutter/lib/ui/ui_dart_state.cc(209)] Unhandled Exception: type 'Null' is not a subtype of type 'double' in type cast
E/flutter (14764): #0      DailyChunkAdapter.read (package:libitrack/model/entities/daily_chunk.g.dart:22:25)
E/flutter (14764): #1      BinaryReaderImpl.read (package:hive/src/binary/binary_reader_impl.dart:326:33)
E/flutter (14764): #2      BinaryReaderImpl.readFrame (package:hive/src/binary/binary_reader_impl.dart:274:26)
E/flutter (14764): #3      FrameHelper.framesFromBytes (package:hive/src/binary/frame_helper.dart:21:26)
E/flutter (14764): #4      FrameIoHelper.framesFromFile (package:hive/src/io/frame_io_helper.dart:42:12)
E/flutter (14764): <asynchronous suspension>
E/flutter (14764): #5      StorageBackendVm.initialize (package:hive/src/backend/vm/storage_backend_vm.dart:86:11)
E/flutter (14764): <asynchronous suspension>
E/flutter (14764): #6      HiveImpl._openBox (package:hive/src/hive_impl.dart:116:9)
E/flutter (14764): <asynchronous suspension>
E/flutter (14764): #7      HiveImpl.openBox (package:hive/src/hive_impl.dart:145:12)
E/flutter (14764): <asynchronous suspension>
E/flutter (14764): #8      _AddDataScreenState._readData (package:libitrack/ui/add_data_screen.dart:100:15)
E/flutter (14764): <asynchronous suspension>
E/flutter (14764): 

Version

  • Platform: Mac
  • Flutter version: [e.g. 2.5.2]
  • Hive version: [e.g. 2.0.4]

pl7nkton avatar Oct 29 '21 17:10 pl7nkton

Update: Figured out there is already a defaultValue feature.

@HiveField(2, defaultValue: 0)
double energy;

https://github.com/hivedb/hive/issues/640#issuecomment-822803896

pl7nkton avatar Oct 29 '21 17:10 pl7nkton

Would also love some documentation on migration. I decided that I no longer need one of the boxes and would like to delete it on update.

tonnyavery avatar Mar 18 '22 17:03 tonnyavery

@AAverin

same here also looking for some doc on migration, i have seen a code snippet in stack overflow https://stackoverflow.com/questions/65171780/flutter-delete-hive-database-on-update

Hive.box("myBox", version: 5, migrator: (oldVersion, newVersion, box) async { await box.delete("unusedKey"); await box.put("newKey", 7); });

I'm wondering if just adding @HiveField(2, defaultValue: 0) to new filed is the right way?

ebrahimmansur avatar Mar 19 '24 08:03 ebrahimmansur

Bumping this. I have the problem my data structure changed. Before a value that was just a String is now a new custom object with some Strings etc.. I have no idea how to migrate my boxes if the user still have data in the old format stored.

danielRi avatar Jul 24 '24 05:07 danielRi