fix: Author column values written by set-based updates (#147)

This commit is contained in:
Marcelo Mendonça Soares 2026-09-17 16:18:18 -03:00 • committed by GitHub
commit 811145eda6
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 195 additions and 12 deletions

View file

@ -463,6 +463,84 @@ void main() {
});
});
group('Given a merged town with a withheld mayor,', () {
late SyncNode node;
late Person mayor;
late Town town;
setUpAll(() async {
node = await syncNode(await createAdditionalTestSession(), testSyncTables);
mayor = Person(id: const Uuid().v7obj(), name: 'mayor');
town = Town(id: const Uuid().v7obj(), name: 'town', mayorId: mayor.id);
final hlc = Hlc(DateTime.now().toUtc(), 0, const Uuid().v7obj());
await node.offlineSync.db.mergeChanges([
CrdtMergeInsert(
uuidSpaceId: testCrdtUserId,
tableName: Town.t.tableName,
uuidRowId: town.id!,
uuidNodeId: hlc.nodeId,
hlcDatetime: hlc.datetime,
hlcCounter: hlc.counter,
data: town,
),
], spaceId: testCrdtUserId);
});
group('when a set-based write clears the mayor,', () {
setUpAll(() async {
// A narrowed write authors the column it names, so it replaces the claim
// the projection is holding rather than passing over it. The column
// already displays null, which is exactly when a write that did not
// author would leave the withheld mayor to reappear later.
await node.offlineSync.db.transactionForUser(
testCrdtUserId,
(tx) => Town.db.updateWhere(
node.offlineSync,
columnValues: (t) => [t.mayorId(null)],
where: (t) => t.id.equals(town.id),
transaction: tx,
),
);
});
test('then the town no longer holds a withheld mayor.', () async {
expect(
await attemptedValue(
rowId: town.id!,
columnName: 'mayorId',
databaseSession: node.offlineSync,
),
isNull,
);
});
});
group(
'when a set-based write clears the mayor and the missing person is inserted locally,',
() {
setUpAll(() async {
await node.offlineSync.db.transactionForUser(
testCrdtUserId,
(tx) => Town.db.updateWhere(
node.offlineSync,
columnValues: (t) => [t.mayorId(null)],
where: (t) => t.id.equals(town.id),
transaction: tx,
),
);
await node.offlineSync.db.transactionForUser(
testCrdtUserId,
(tx) => Person.db.insertRow(node.offlineSync, mayor, transaction: tx),
);
});
test('then the cleared mayor stays cleared.', () async {
expect((await Town.db.findById(node.offlineSync, town.id!))!.mayorId, isNull);
});
},
);
});
group(
'Given two merged unique children waiting for unavailable parents and a missing fallback,',
() {

View file

@ -231,6 +231,91 @@ void main() {
expect((await Unique.db.findById(session, winner.id!))!.name, 'shared-name');
},
);
test(
'when a set-based write gives the loser a free name, '
'then the write is authored and the released claim is not restored.',
() async {
await session.db.transactionForUser(
testCrdtUserId,
(tx) => Unique.db.updateWhere(
session,
columnValues: (t) => [t.name('renamed')],
where: (t) => t.id.equals(loser.id),
transaction: tx,
),
);
expect((await Unique.db.findById(session, loser.id!))!.name, 'renamed');
expect(
await attemptedValue(
rowId: loser.id!,
columnName: Unique.t.name.columnName,
),
isNull,
);
expect((await Unique.db.findById(session, winner.id!))!.name, 'shared-name');
},
);
},
);
group(
'Given a nullable unique loser whose claim was released to null, ',
() {
late UniqueNullable winner;
late UniqueNullable loser;
setUp(() async {
winner = await session.db.transactionForUser(
testCrdtUserId,
(tx) => UniqueNullable.db.insertRow(
session,
UniqueNullable(id: const Uuid().v7obj(), value: 1),
transaction: tx,
),
);
loser = UniqueNullable(id: const Uuid().v7obj(), value: 1);
await mergeIndependentInsert(
session,
loser,
space: testCrdtUserId,
tables: [UniqueNullable.t],
);
});
test(
'when a set-based write authors null on both rows, '
'then the loser keeps the written null rather than reclaiming its value.',
() async {
// The loser displays the null its release wrote, so writing null is
// an unchanged domain value. A narrowed write authors it anyway:
// otherwise the freed claim behind it would win the reprojection the
// winner's own null triggers, and reappear as a value nobody wrote.
await session.db.transactionForUser(
testCrdtUserId,
(tx) => UniqueNullable.db.updateWhere(
session,
columnValues: (t) => [t.value(null)],
where: (t) => t.id.inSet(<UuidValue>{winner.id!, loser.id!}),
transaction: tx,
),
);
expect((await UniqueNullable.db.findById(session, loser.id!))!.value, isNull);
expect(
await attemptedValue(
rowId: loser.id!,
columnName: UniqueNullable.t.value.columnName,
),
isNull,
);
expect(
(await UniqueNullable.db.findById(session, winner.id!))!.value,
isNull,
);
},
);
},
);