IO-Design-Team / hive_ce

Hive Community Edition - A spiritual continuation of Hive v2
https://pub.dev/packages/hive_ce
Apache License 2.0
30 stars 2 forks source link

Documentation for adding new fields #19

Closed vizakenjack closed 2 months ago

vizakenjack commented 2 months ago

A friend of mine said that he tried to use Hive but had an issue with migrations.

He was not able to figure out that there is a defaultValue parameter in the @HiveField() annotation. I think it's worth mentioning this issue in the documentation.

Rexios80 commented 2 months ago

Good idea! The documentation for the code generation is surprisingly sparse. I would mention other methods of setting the default value, like with a constructor parameter default.

vizakenjack commented 2 months ago

I would mention other methods of setting the default value, like with a constructor parameter default.

In the previous version of Hive that was not enough, so I had to also specify defaultValue in HiveField()

Rexios80 commented 2 months ago

If that's the case we should probably fix that

Rexios80 commented 2 months ago

@vizakenjack

I just released hive_ce_generator: 1.5.0 which supports constructor parameter defaults. Please see the code here for an example and update the documentation in this PR to include those methods of specifying default values.

vizakenjack commented 2 months ago

@vizakenjack

I just released hive_ce_generator: 1.5.0 which supports constructor parameter defaults. Please see the code here for an example and update the documentation in this PR to include those methods of specifying default values.

Wow, that was fast! Thanks. So, if I understood correctly, if I add a new field to the database with @HiveField(1, defaultValue: '6 * 7') and this.b = '42' in the constructor, there will be '42' for new objects and '6 * 7' for old objects?

If I add d = d ?? DateTime.now();, there won't be any migration for null values and there could be an error, like in the previous version?

Rexios80 commented 2 months ago

if I add a new field to the database with @HiveField(1, defaultValue: '6 7') and this.b = '42' in the constructor, there will be '42' for new objects and '6 7' for old objects?

That is correct

If I add d = d ?? DateTime.now();, there won't be any migration for null values and there could be an error, like in the previous version?

That is not correct. The default value is DateTime.now() in all cases, and that should have been the case before generator version 1.5.0 as well. As we can see in the generated code:

@override
ConstructorDefaults read(BinaryReader reader) {
  final numOfFields = reader.readByte();
  final fields = <int, dynamic>{
    for (int i = 0; i < numOfFields; i++) reader.readByte(): reader.read(),
  };
  return ConstructorDefaults(
    a: fields[0] == null ? 42 : (fields[0] as num).toInt(),
    b: fields[1] == null ? '6 * 7' : fields[1] as String,
    c: fields[2] == null ? true : fields[2] as bool,
    d: fields[3] as DateTime?,
  );
}

If fields[3] is null, the default value of DateTime.now() will be used

vizakenjack commented 2 months ago

@Rexios80 thanks for clarifying this. I have updated the docs

codecov[bot] commented 2 months ago

Codecov Report

All modified and coverable lines are covered by tests :white_check_mark:

Please upload report for BASE (main@139cdae). Learn more about missing BASE report.

Additional details and impacted files ```diff @@ Coverage Diff @@ ## main #19 +/- ## ======================================= Coverage ? 93.55% ======================================= Files ? 43 Lines ? 1972 Branches ? 0 ======================================= Hits ? 1845 Misses ? 127 Partials ? 0 ```

:umbrella: View full report in Codecov by Sentry.
:loudspeaker: Have feedback on the report? Share it here.

Rexios80 commented 2 months ago

Released in version 2.5.0+2

Thank you for the contribution!