hive icon indicating copy to clipboard operation
hive copied to clipboard

We need to support versioning in Hive DB. Please help

Open madhumb opened this issue 3 years ago • 3 comments

Question We had 4 keys in Hive DB and app is live. We changed the requirement and updated Hive DB with 4 more params. But if we run new code over old code. app crashes

Code sample import 'package:hive/hive.dart'; part 'hive_oehub_message.g.dart';

@HiveType(typeId: 3, adapterName: "OEHubMessageAdapter") class HiveOEHubMessage extends HiveObject { @HiveField(0) String expiryDate; @HiveField(1) int id; @HiveField(2) int isRead; @HiveField(3) String message; @HiveField(4) String summary; @HiveField(5) String thumbnail; @HiveField(6) String title; @HiveField(7) String url; @HiveField(8) String notificationType; @HiveField(9) String publishedOn; @HiveField(10) String timstampLastRetrived; @HiveField(11) String urerId; @HiveField(12) String isDelete; @HiveField(13) String validFrom; @HiveField(14) String messageType;

HiveOEHubMessage( {required this.expiryDate, required this.id, required this.isDelete, required this.isRead, required this.message, required this.publishedOn, required this.summary, required this.thumbnail, required this.timstampLastRetrived, required this.title, required this.notificationType, required this.urerId, required this.url, required this.validFrom, required this.messageType}); }

New file is

import 'package:hive/hive.dart'; part 'hive_oehub_message.g.dart';

@HiveType(typeId: 3, adapterName: "OEHubMessageAdapter") class HiveOEHubMessage extends HiveObject { @HiveField(0) String expiryDate; @HiveField(1) int id; @HiveField(2) int isRead; @HiveField(3) String message; @HiveField(4) String summary; @HiveField(5) String thumbnail; @HiveField(6) String title; @HiveField(7) String url; @HiveField(8) String notificationType; @HiveField(9) String publishedOn; @HiveField(10) String timstampLastRetrived; @HiveField(11) String urerId; @HiveField(12) String isDelete; @HiveField(13) String validFrom; @HiveField(14) String messageType; @HiveField(15) int likeCount = 0; @HiveField(16) int commentCount = 0; @HiveField(17) int shareCount = 0; @HiveField(18) int userLiked = 0; @HiveField(19) int mappingId = 0;

HiveOEHubMessage( {required this.expiryDate, required this.id, required this.isDelete, required this.isRead, required this.message, required this.publishedOn, required this.summary, required this.thumbnail, required this.timstampLastRetrived, required this.title, required this.notificationType, required this.urerId, required this.url, required this.validFrom, required this.messageType, required this.likeCount, required this.commentCount, required this.shareCount, required this.userLiked, required this.mappingId}); }

Version

  • Platform: iOS, Android
  • Flutter version: 2.8.1
  • Hive version:
  • hive: ^2.0.4 hive_flutter: ^1.1.0

Let us know how to migrate old data in to new.

madhumb avatar Jan 19 '22 12:01 madhumb

Same challenge to me :D

sgsm74 avatar Jan 25 '22 09:01 sgsm74

https://docs.hivedb.dev/#/custom-objects/generate_adapter?id=updating-a-class Look at this, and this #640

yang-lile avatar Jan 29 '22 14:01 yang-lile

I had this same problem on a personal project, because I needed to make the current model definitions compatible with any future version, in any order, without losing any previous information while loading.

So I implemented this architecture for model versioning using Flutter and Hive. The idea is to read and write data respective to each version model and deliver it transparently using a model interface. That way, if you need to update the current model, you just need to create a new version of it and update the respective adapters. The OOP principles will force the developer to create an adapter for each existing field, automatically.

And this implementation is 100% transparent to any project. And you can use all code generators available with flutter pub run build_runner build (In the example i used JsonSerializable).

image image

Here is my GitHub repository containing a short example of how to implement it: https://github.com/rafaelsetragni/hive_versioning

rafaelsetragni avatar Sep 14 '22 01:09 rafaelsetragni