From a4387c12b90ad325f298d0133ebdd0d8cdb22ab5 Mon Sep 17 00:00:00 2001 From: Robin Munn Date: Thu, 12 Nov 2026 16:12:47 +0700 Subject: [PATCH 1/2] Send a never-synced project's missing entries only to an empty FieldWorks #361 made LfMerge send every entry a never-synced project's FieldWorks lacks, so that an LF project's first Send/Receive -- as when one is migrated -- would not lose the entries a LIFT import into LF left without user references. But it did that for every never-synced project, and the ones whose FieldWorks already has entries were cloned from it: the entries it lacks were since deleted in FLEx, or dropped when its repository was begun afresh. Their next sync would bring those entries back, undoing what #362 was for. Three never-synced projects on the live server are like that, with 664 such entries between them. So the missing entries are now sent only when FieldWorks had no entries at all before the conversion began, which is how a migration's new LexBox project starts. Every other never-synced project is back to how #362 left it. Co-Authored-By: Claude Opus 5.5 --- .../Lcm/TransferMongoToLcmActionTests.cs | 58 +++++++++++++++---- .../ConvertMongoToLcmLexicon.cs | 34 +++++++---- 2 files changed, 69 insertions(+), 23 deletions(-) diff --git a/src/LfMerge.Core.Tests/Lcm/TransferMongoToLcmActionTests.cs b/src/LfMerge.Core.Tests/Lcm/TransferMongoToLcmActionTests.cs index 86dc7aa1..477a9e59 100644 --- a/src/LfMerge.Core.Tests/Lcm/TransferMongoToLcmActionTests.cs +++ b/src/LfMerge.Core.Tests/Lcm/TransferMongoToLcmActionTests.cs @@ -745,25 +745,34 @@ public void Action_NeverSyncedProjectWhoseEntriesLfMergeWrote_KeepsFieldWorksVer Assert.That(_counts.Modified, Is.EqualTo(0)); } - [Test] - public void Action_NeverSyncedProjectWithEntryImportedIntoLf_SendsItToFieldWorks() + // An entry as a LIFT import into LF leaves it: no user references, the same as LfMerge's own + // writes, so it is stored with UpdateRecord rather than as an LF user's edit. + private LfLexEntry StoreImportedLfEntry(ILfProject lfProj, string lexeme) { - // Setup: an LF project's first Send/Receive, as when one is migrated. A LIFT import into - // LF leaves no user references on its entries, the same as LfMerge's own writes do, so - // the entry is stored with UpdateRecord rather than as an LF user's edit. - var lfProj = _lfProj; - _conn.SetLastSyncedDate(lfProj, MagicValues.UnixEpoch); - FwProject fwProject = lfProj.FieldWorksProject; - string vernacularWS = fwProject.Cache.LanguageProject.DefaultVernacularWritingSystem.Id; - Guid importedGuid = Guid.NewGuid(); + string vernacularWS = lfProj.FieldWorksProject.Cache.LanguageProject.DefaultVernacularWritingSystem.Id; var imported = new LfLexEntry(); - imported.Guid = importedGuid; - imported.Lexeme = LfMultiText.FromSingleStringMapping(vernacularWS, "imported into LF"); + imported.Guid = Guid.NewGuid(); + imported.Lexeme = LfMultiText.FromSingleStringMapping(vernacularWS, lexeme); imported.AuthorInfo = new LfAuthorInfo(); imported.AuthorInfo.CreatedDate = new DateTime(2016, 4, 10, 0, 0, 0, DateTimeKind.Utc); imported.AuthorInfo.ModifiedDate = imported.AuthorInfo.CreatedDate; imported.DateModified = new DateTime(2016, 7, 25, 0, 0, 0, DateTimeKind.Utc); _conn.UpdateRecord(lfProj, imported); + return imported; + } + + [Test] + public void Action_NeverSyncedProjectIntoEmptyFieldWorks_SendsEntriesImportedIntoLf() + { + // Setup: an LF project's first Send/Receive into a new, empty FieldWorks project, as when + // one is migrated + var lfProj = _lfProj; + _conn.SetLastSyncedDate(lfProj, MagicValues.UnixEpoch); + FwProject fwProject = lfProj.FieldWorksProject; + foreach (Guid guid in LcmTestHelper.GetEntries(fwProject).Select(entry => entry.Guid).ToList()) + LcmTestHelper.DeleteEntry(fwProject, guid); + Assert.That(LcmTestHelper.CountEntries(fwProject), Is.EqualTo(0)); + Guid importedGuid = StoreImportedLfEntry(lfProj, "imported into LF").Guid.Value; // Exercise SutMongoToLcm.Run(lfProj); @@ -778,6 +787,31 @@ public void Action_NeverSyncedProjectWithEntryImportedIntoLf_SendsItToFieldWorks Assert.That(_counts.Deleted, Is.EqualTo(0)); } + [Test] + public void Action_NeverSyncedProjectWhoseFieldWorksHasEntries_DoesNotRecreateEntryDeletedInFieldWorks() + { + // Setup: an initial clone whose transfer to Mongo never got as far as recording the sync, + // after which FLEx deleted one of the entries LfMerge had written to Mongo + var lfProj = _lfProj; + SutLcmToMongo.Run(lfProj); + _conn.SetLastSyncedDate(lfProj, MagicValues.UnixEpoch); + FwProject fwProject = lfProj.FieldWorksProject; + Guid kenGuid = Guid.Parse(KenEntryGuidStr); + LcmTestHelper.DeleteEntry(fwProject, kenGuid); + Assert.That(LcmTestHelper.CountEntries(fwProject), Is.GreaterThan(0)); + + // Exercise + SutMongoToLcm.Run(lfProj); + + // Verify + ILexEntry ken; + Assert.That(fwProject.ServiceLocator.GetInstance().TryGetObject(kenGuid, out ken), Is.False, + "an entry FLEx deleted must stay deleted, though LF still has it"); + Assert.That(_counts.Added, Is.EqualTo(0)); + Assert.That(_counts.Modified, Is.EqualTo(0)); + Assert.That(_counts.Deleted, Is.EqualTo(0)); + } + // TODO: Move custom field tests to their own test class, and move these helper functions with them public void AddCustomFieldToSense(LfSense sense, string fieldName, BsonValue fieldValue) { diff --git a/src/LfMerge.Core/DataConverters/ConvertMongoToLcmLexicon.cs b/src/LfMerge.Core/DataConverters/ConvertMongoToLcmLexicon.cs index c86a1065..054ea525 100644 --- a/src/LfMerge.Core/DataConverters/ConvertMongoToLcmLexicon.cs +++ b/src/LfMerge.Core/DataConverters/ConvertMongoToLcmLexicon.cs @@ -45,11 +45,16 @@ public class ConvertMongoToLcmLexicon // Entries where LF's copy differs from FieldWorks, or FieldWorks lacks it, but no LF user has // touched it since the last sync, so FieldWorks holds the newer version (see - // EditedInLfSinceLastSync). An entry FieldWorks lacks in a project that has never synced is - // not counted: it is LF's own, and is sent (see LfLexEntryToLcmLexEntry). + // EditedInLfSinceLastSync). Not counted are the entries of a never-synced project whose + // FieldWorks lexicon started out empty: those are LF's own, and are sent (see + // LfLexEntryToLcmLexEntry). private int _changedOnlyInFieldWorks; private int _absentOnlyFromFieldWorks; + // Whether this is an LF project's first Send/Receive into an empty FieldWorks project, as when + // one is migrated: never synced, and FieldWorks had no entries before this conversion began. + private bool _firstSyncIntoEmptyFieldWorks; + // Shorter names to use in this class since MagicStrings.LfOptionListCodeForGrammaticalInfo // (etc.) are real mouthfuls private const string GrammarListCode = MagicStrings.LfOptionListCodeForGrammaticalInfo; @@ -159,6 +164,8 @@ public ConversionError RunConversion() IEnumerable lexicon = GetLexicon(LfProject); _changedOnlyInFieldWorks = 0; _absentOnlyFromFieldWorks = 0; + // Decided once, before any entry is created: it is about where the project started. + _firstSyncIntoEmptyFieldWorks = LastSyncedDate == null && GetInstance().Count == 0; UndoableUnitOfWorkHelper.DoUsingNewOrCurrentUOW("undo", "redo", Cache.ActionHandlerAccessor, () => { #if false // Once we allow LanguageForge to create optionlist items with "canonical" values (parts of speech, semantic domains, etc.), uncomment this block @@ -1061,8 +1068,8 @@ private DateTime? LastSyncedDate /// sync. LfMerge clears the user references on every entry it writes, and LF sets one on every /// edit and creation; a deletion sets none, but deleting what FieldWorks lacks is harmless. /// A LIFT import into LF sets none either, so this says "not edited" of every imported entry - /// in a never-synced LF project; LfLexEntryToLcmLexEntry sends such an entry anyway when - /// FieldWorks does not have it. + /// in a never-synced LF project; LfLexEntryToLcmLexEntry sends such entries anyway when + /// FieldWorks started out with no entries at all. /// private bool EditedInLfSinceLastSync(LfLexEntry lfEntry) { @@ -1092,17 +1099,22 @@ private void LfLexEntryToLcmLexEntry(LfLexEntry lfEntry) _changedOnlyInFieldWorks++; return; } - if (LastSyncedDate != null) + if (!_firstSyncIntoEmptyFieldWorks) { + // FLEx deleted it, or the repository was begun afresh without it: either way + // FieldWorks is right not to have it. _absentOnlyFromFieldWorks++; return; } - // Never synced, and FieldWorks does not have it: an LF project's own entry, going to - // FieldWorks for the first time. A LIFT import into LF sets no user references either, - // so EditedInLfSinceLastSync cannot tell it from one LfMerge wrote; but FieldWorks - // cannot hold a newer version of an entry it does not have, so there is nothing to - // keep. Leaving it out would also lose it: the transfer back to Mongo marks every - // entry FieldWorks lacks as deleted. + // An LF project's own entry, on its first Send/Receive into an empty FieldWorks + // project: a migration. A LIFT import into LF sets no user references, so + // EditedInLfSinceLastSync cannot tell it from one LfMerge wrote, but an empty + // FieldWorks project never had any entry to delete. Leaving it out would lose it, as + // the transfer back to Mongo marks every entry FieldWorks lacks as deleted. + // + // A never-synced project whose FieldWorks has entries is not sent anything this way, + // though FieldWorks lacks some of LF's: there, LF's entries came from an earlier + // FieldWorks, and the ones it no longer has were deleted in FLEx since. } bool createdEntry = false; bool wantCreation = !lfEntry.IsDeleted; From fd79190636aca158c3ddc145c749b446a0644a71 Mon Sep 17 00:00:00 2001 From: Robin Munn Date: Thu, 12 Nov 2026 16:50:29 +0700 Subject: [PATCH 2/2] Log each never-synced entry taken to be deleted in FLEx In a never-synced project whose FieldWorks has entries, an LF entry with no user references that FieldWorks lacks is taken to have been deleted in FLEx since the initial clone, and the transfer back to Mongo marks it deleted. The data cannot check that: an entry imported into LF from LIFT after the clone looks just the same, and would be marked deleted too. That is deliberate, but it should not pass unnoticed. Each such entry is now logged by GUID and name as it is left out, so that one which turns out to be LF's own can be found and restored -- marking it deleted leaves it in Mongo. The comment there now says this is an assumption, and names the case it gets wrong. After a sync nothing is logged: an entry FieldWorks lacks then was certainly deleted in FLEx. Co-Authored-By: Claude Opus 5.5 --- .../Lcm/TransferMongoToLcmActionTests.cs | 7 +++++++ .../ConvertMongoToLcmLexicon.cs | 21 +++++++++++++------ 2 files changed, 22 insertions(+), 6 deletions(-) diff --git a/src/LfMerge.Core.Tests/Lcm/TransferMongoToLcmActionTests.cs b/src/LfMerge.Core.Tests/Lcm/TransferMongoToLcmActionTests.cs index 477a9e59..5cd65ced 100644 --- a/src/LfMerge.Core.Tests/Lcm/TransferMongoToLcmActionTests.cs +++ b/src/LfMerge.Core.Tests/Lcm/TransferMongoToLcmActionTests.cs @@ -702,6 +702,8 @@ public void Action_EntryDeletedInFieldWorksSinceLastSync_IsNotRecreated() Assert.That(_counts.Added, Is.EqualTo(0)); Assert.That(_counts.Modified, Is.EqualTo(0)); Assert.That(_counts.Deleted, Is.EqualTo(0)); + Assert.That(_env.Logger.GetMessages(), Does.Not.Contain("has never synced"), + "after a sync, an entry FieldWorks lacks was certainly deleted in FLEx, so it is not reported one by one"); } [Test] @@ -810,6 +812,11 @@ public void Action_NeverSyncedProjectWhoseFieldWorksHasEntries_DoesNotRecreateEn Assert.That(_counts.Added, Is.EqualTo(0)); Assert.That(_counts.Modified, Is.EqualTo(0)); Assert.That(_counts.Deleted, Is.EqualTo(0)); + // The data cannot tell this from an entry imported into LF after the clone, which would be + // marked deleted the same way, so each one is logged where it can be found again + string messages = _env.Logger.GetMessages(); + Assert.That(messages, Does.Contain($"MongoToLcm: entry {kenGuid} (")); + Assert.That(messages, Does.Contain("has never synced")); } // TODO: Move custom field tests to their own test class, and move these helper functions with them diff --git a/src/LfMerge.Core/DataConverters/ConvertMongoToLcmLexicon.cs b/src/LfMerge.Core/DataConverters/ConvertMongoToLcmLexicon.cs index 054ea525..9045bf95 100644 --- a/src/LfMerge.Core/DataConverters/ConvertMongoToLcmLexicon.cs +++ b/src/LfMerge.Core/DataConverters/ConvertMongoToLcmLexicon.cs @@ -1101,8 +1101,21 @@ private void LfLexEntryToLcmLexEntry(LfLexEntry lfEntry) } if (!_firstSyncIntoEmptyFieldWorks) { - // FLEx deleted it, or the repository was begun afresh without it: either way - // FieldWorks is right not to have it. + // FieldWorks is taken to be right not to have it. After a sync that is certain: LF's + // copy is what FieldWorks had then, untouched since, so FLEx has deleted it. + // + // In a never-synced project whose FieldWorks has entries it is an assumption, which + // the data cannot check. Such a project's entries normally came from its FieldWorks + // at the initial clone, so one FieldWorks now lacks was deleted in FLEx since, or + // dropped when the repository was begun afresh. But an entry imported into LF from + // LIFT after the clone has no user references either, and looks just the same: it + // is deliberately treated as deleted in FLEx too, and the transfer back to Mongo + // marks it deleted. Marked, not removed -- it stays in Mongo, and clearing isDeleted + // restores it -- and logged here, so that it can be found if it was LF's own. + if (LastSyncedDate == null) + Logger.Info("MongoToLcm: entry {0} ({1}) is in LF but not in FieldWorks, and the project has never synced; " + + "taking it to have been deleted in FLEx, so the transfer back to Mongo will mark it deleted", + guid, ConvertUtilities.EntryNameForDebugging(lfEntry)); _absentOnlyFromFieldWorks++; return; } @@ -1111,10 +1124,6 @@ private void LfLexEntryToLcmLexEntry(LfLexEntry lfEntry) // EditedInLfSinceLastSync cannot tell it from one LfMerge wrote, but an empty // FieldWorks project never had any entry to delete. Leaving it out would lose it, as // the transfer back to Mongo marks every entry FieldWorks lacks as deleted. - // - // A never-synced project whose FieldWorks has entries is not sent anything this way, - // though FieldWorks lacks some of LF's: there, LF's entries came from an earlier - // FieldWorks, and the ones it no longer has were deleted in FLEx since. } bool createdEntry = false; bool wantCreation = !lfEntry.IsDeleted;