@@ -240,6 +240,15 @@ class SiteRepository {
240240 if (media.siteId != null ) media.id: media.siteId! ,
241241 };
242242
243+ // Capture raw timestamps for deleted sites (domain entity lacks these)
244+ final rawSiteRows = await (_db.select (
245+ _db.diveSites,
246+ )..where ((t) => t.id.isIn (orderedIds))).get ();
247+ final siteTimestamps = {
248+ for (final row in rawSiteRows)
249+ row.id: (createdAt: row.createdAt, updatedAt: row.updatedAt),
250+ };
251+
243252 final allSpeciesRows = await (_db.select (
244253 _db.siteSpecies,
245254 )..where ((t) => t.siteId.isIn (orderedIds))).get ();
@@ -250,6 +259,7 @@ class SiteRepository {
250259 siteId: row.siteId,
251260 speciesId: row.speciesId,
252261 notes: row.notes,
262+ createdAt: row.createdAt,
253263 ),
254264 )
255265 .toList (growable: false );
@@ -309,6 +319,11 @@ class SiteRepository {
309319 mediaOriginalSiteIds: mediaOriginalSiteIds,
310320 deletedSpeciesEntries: deletedSpecies,
311321 modifiedSpeciesEntries: modifiedSpecies,
322+ deletedSiteTimestamps: {
323+ for (final id in duplicateIds)
324+ if (siteTimestamps.containsKey (id)) id: siteTimestamps[id]! ,
325+ },
326+ survivorTimestamps: siteTimestamps[survivorId],
312327 );
313328 } catch (e, stackTrace) {
314329 _log.error ('Failed to merge sites: $siteIds ' , e, stackTrace);
@@ -340,6 +355,7 @@ class SiteRepository {
340355
341356 // 2. Re-create deleted sites
342357 for (final site in snapshot.deletedSites) {
358+ final ts = snapshot.deletedSiteTimestamps[site.id];
343359 await _db
344360 .into (_db.diveSites)
345361 .insert (
@@ -362,8 +378,8 @@ class SiteRepository {
362378 mooringNumber: Value (site.mooringNumber),
363379 parkingInfo: Value (site.parkingInfo),
364380 altitude: Value (site.altitude),
365- createdAt: Value (now),
366- updatedAt: Value (now),
381+ createdAt: Value (ts ? .createdAt ?? now),
382+ updatedAt: Value (ts ? .updatedAt ?? now),
367383 ),
368384 );
369385 await _syncRepository.markRecordPending (
@@ -411,7 +427,7 @@ class SiteRepository {
411427 siteId: Value (entry.siteId),
412428 speciesId: Value (entry.speciesId),
413429 notes: Value (entry.notes),
414- createdAt: Value (now ),
430+ createdAt: Value (entry.createdAt ),
415431 ),
416432 );
417433 await _syncRepository.markRecordPending (
@@ -714,13 +730,23 @@ class MergeSnapshot {
714730 final List <SiteSpeciesSnapshot > deletedSpeciesEntries;
715731 final List <SiteSpeciesSnapshot > modifiedSpeciesEntries;
716732
733+ /// Original createdAt/updatedAt for each deleted site, keyed by site ID.
734+ /// DiveSite domain entity does not carry timestamps, so they are captured
735+ /// separately from the database row before deletion.
736+ final Map <String , ({int createdAt, int updatedAt})> deletedSiteTimestamps;
737+
738+ /// Original createdAt/updatedAt for the survivor site before merge.
739+ final ({int createdAt, int updatedAt})? survivorTimestamps;
740+
717741 const MergeSnapshot ({
718742 required this .originalSurvivor,
719743 required this .deletedSites,
720744 required this .diveOriginalSiteIds,
721745 required this .mediaOriginalSiteIds,
722746 required this .deletedSpeciesEntries,
723747 required this .modifiedSpeciesEntries,
748+ this .deletedSiteTimestamps = const {},
749+ this .survivorTimestamps,
724750 });
725751}
726752
@@ -730,11 +756,13 @@ class SiteSpeciesSnapshot {
730756 final String siteId;
731757 final String speciesId;
732758 final String notes;
759+ final int createdAt;
733760
734761 const SiteSpeciesSnapshot ({
735762 required this .id,
736763 required this .siteId,
737764 required this .speciesId,
738765 required this .notes,
766+ required this .createdAt,
739767 });
740768}
0 commit comments