feat(db): lot presentation + attachment sort order (schema v4→v5)

Expand LotType from {seed, plant} to six append-only forms (seed,
seedling, plant, tree, bulb, cutting) and add an optional Presentation
attribute (pot/tray/plug/bareRoot/rootBall) kept separate from quantity
so 'how many' and 'in what packaging' never conflate.

Add attachments.sortOrder so the cover photo is the lowest-ordered one,
enabling reordering from the full-screen viewer. Expand QuantityKind
with the new plant-aware forms.

Migrations are additive (v1→v5 all covered by migration_test); enum
values are stored by name and appended only, keeping CRDT sync safe.
This commit is contained in:
vjrj 2026-07-09 11:51:21 +02:00
parent 06aed2a586
commit 3954c62aa6
16 changed files with 6174 additions and 32 deletions

View file

@ -201,6 +201,43 @@ void main() {
]);
});
test('setPreferredPhoto moves a photo to the front (cover)', () async {
final id = await repo.addQuickVariety(label: 'Maize');
await repo.addPhoto(id, Uint8List.fromList([1]));
await repo.addPhoto(id, Uint8List.fromList([2]));
final p3 = await repo.addPhoto(id, Uint8List.fromList([3]));
// Insertion order until a cover is chosen.
expect(
(await repo.watchVariety(id).first)!.photos.map((p) => p.bytes.toList()),
[
[1],
[2],
[3],
],
);
await repo.setPreferredPhoto(id, p3);
final photos = (await repo.watchVariety(id).first)!.photos;
expect(photos.map((p) => p.bytes.toList()), [
[3],
[1],
[2],
]);
});
test('setPreferredPhoto changes the inventory avatar to the cover', () async {
final id = await repo.addQuickVariety(label: 'Maize');
await repo.addPhoto(id, Uint8List.fromList([1]));
final p2 = await repo.addPhoto(id, Uint8List.fromList([2]));
expect((await repo.watchInventory().first).single.photo!.toList(), [1]);
await repo.setPreferredPhoto(id, p2);
expect((await repo.watchInventory().first).single.photo!.toList(), [2]);
});
test('watchInventory avatar refreshes when a photo is added later', () async {
final id = await repo.addQuickVariety(label: 'Maize');
final queue = StreamQueue(repo.watchInventory());

View file

@ -11,28 +11,40 @@ void main() {
verifier = SchemaVerifier(GeneratedHelper());
});
test('freshly created database matches the exported schema v3', () async {
final schema = await verifier.schemaAt(3);
test('freshly created database matches the exported schema v5', () async {
final schema = await verifier.schemaAt(5);
final db = AppDatabase(schema.newConnection());
await verifier.migrateAndValidate(db, 3);
await verifier.migrateAndValidate(db, 5);
await db.close();
});
test('upgrades v1 → v3 (adds Lot.type, then harvestMonth) and matches the '
'fresh schema', () async {
test('upgrades v1 → v5 (Lot.type, harvestMonth, presentation, '
'Attachment.sortOrder) and matches the fresh schema', () async {
final connection = await verifier.startAt(1);
final db = AppDatabase(connection);
await verifier.migrateAndValidate(db, 3);
await verifier.migrateAndValidate(db, 5);
await db.close();
});
test(
'upgrades v2 → v3 (adds Lot.harvestMonth) and matches the fresh schema',
() async {
final connection = await verifier.startAt(2);
final db = AppDatabase(connection);
await verifier.migrateAndValidate(db, 3);
await db.close();
},
);
test('upgrades v2 → v5 and matches the fresh schema', () async {
final connection = await verifier.startAt(2);
final db = AppDatabase(connection);
await verifier.migrateAndValidate(db, 5);
await db.close();
});
test('upgrades v3 → v5 and matches the fresh schema', () async {
final connection = await verifier.startAt(3);
final db = AppDatabase(connection);
await verifier.migrateAndValidate(db, 5);
await db.close();
});
test('upgrades v4 → v5 (adds Attachment.sortOrder) and matches the fresh '
'schema', () async {
final connection = await verifier.startAt(4);
final db = AppDatabase(connection);
await verifier.migrateAndValidate(db, 5);
await db.close();
});
}

View file

@ -7,6 +7,8 @@ import 'package:drift/internal/migrations.dart';
import 'schema_v1.dart' as v1;
import 'schema_v2.dart' as v2;
import 'schema_v3.dart' as v3;
import 'schema_v4.dart' as v4;
import 'schema_v5.dart' as v5;
class GeneratedHelper implements SchemaInstantiationHelper {
@override
@ -18,10 +20,14 @@ class GeneratedHelper implements SchemaInstantiationHelper {
return v2.DatabaseAtV2(db);
case 3:
return v3.DatabaseAtV3(db);
case 4:
return v4.DatabaseAtV4(db);
case 5:
return v5.DatabaseAtV5(db);
default:
throw MissingSchemaException(version, versions);
}
}
static const versions = const [1, 2, 3];
static const versions = const [1, 2, 3, 4, 5];
}

File diff suppressed because it is too large Load diff

File diff suppressed because it is too large Load diff