Skip to content

Commit 7f0804d

Browse files
authored
refactor(persistence): Store DateTime as ISO-8601 text in DB to preserve sub-seconds precision (#2694)
* refactor(persistence): Store DateTime as ISO-8601 text in DB to preserve sub-second precision. * test(persistence): Tighten isSameDateAs to also enforce millisecond precision.
1 parent d85ff66 commit 7f0804d

4 files changed

Lines changed: 26 additions & 2 deletions

File tree

packages/stream_chat_persistence/CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,10 @@
44

55
- Reduce the number of DB reads in the `ChatPersistenceClient.getChannelStates` method.
66

7+
🔄 Changed
8+
9+
- Changed how dates are stored in the local cache, from integer seconds to ISO-8601 strings, in order to preserve millisecond precision.
10+
711
## 9.24.0
812

913
- Updated `stream_chat` dependency to [`9.24.0`](https://pub.dev/packages/stream_chat/changelog).

packages/stream_chat_persistence/lib/src/db/drift_chat_database.dart

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,12 @@ class DriftChatDatabase extends _$DriftChatDatabase {
5555

5656
// you should bump this number whenever you change or add a table definition.
5757
@override
58-
int get schemaVersion => 27;
58+
int get schemaVersion => 28;
59+
60+
// Store DateTime as ISO-8601 text to preserve sub-second precision.
61+
@override
62+
DriftDatabaseOptions get options =>
63+
const DriftDatabaseOptions(storeDateTimeAsText: true);
5964

6065
@override
6166
MigrationStrategy get migration => MigrationStrategy(

packages/stream_chat_persistence/test/src/dao/connection_event_dao_test.dart

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,20 @@ void main() {
112112
expect(updatedLastSyncAt, isSameDateAs(now));
113113
});
114114

115+
test('updateLastSyncAt preserves millisecond precision', () async {
116+
// A connection event must exist before lastSyncAt can be stored.
117+
await eventDao.updateConnectionEvent(
118+
Event(createdAt: DateTime.now(), me: OwnUser(id: 'testUserId')),
119+
);
120+
121+
final preciseDate = DateTime.utc(2026, 5, 28, 12, 34, 56, 123);
122+
await eventDao.updateLastSyncAt(preciseDate);
123+
124+
final readBack = await eventDao.lastSyncAt;
125+
expect(readBack, equals(preciseDate));
126+
expect(readBack!.millisecond, equals(123));
127+
});
128+
115129
tearDown(() async {
116130
await database.disconnect();
117131
});

packages/stream_chat_persistence/test/src/utils/date_matcher.dart

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,8 @@ class _IsSameDateAs extends Matcher {
1515
date.day == targetDate?.day &&
1616
date.hour == targetDate?.hour &&
1717
date.minute == targetDate?.minute &&
18-
date.second == targetDate?.second;
18+
date.second == targetDate?.second &&
19+
date.millisecond == targetDate?.millisecond;
1920
}
2021

2122
@override

0 commit comments

Comments
 (0)