Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions docker-compose.mongo.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ services:
# then hang forever so the container does not exit
command:
[
'/bin/sh',
'-c',
'mongod --profile=2 --replSet myRS --bind_ip 0.0.0.0 & sleep 2s; mongosh --host localhost:27017 --eval '' config = { "_id" : "myRS", "members" : [{"_id" : 0,"host" : "mongo:27017"}] }; rs.initiate(config, { force: true }); '' ; sleep infinity'
"/bin/sh",
"-c",
'mongod --profile=2 --replSet myRS --bind_ip 0.0.0.0 & sleep 2s; mongosh --host localhost:27017 --eval '' config = { "_id" : "myRS", "members" : [{"_id" : 0,"host" : "localhost:27017"}] }; rs.initiate(config, { force: true }); '' ; sleep infinity',
]
Original file line number Diff line number Diff line change
Expand Up @@ -61,21 +61,7 @@ await c.Aggregate()
.AppendStage<BsonDocument>(new BsonDocument("$unset", "engine"))
.Merge(c, new MergeStageOptions<Build> { WhenMatched = MergeStageWhenMatched.Replace })
.ToListAsync();

//migrate by adding TargetQuoteConvention field populated from analysis field
await c.Aggregate()
.Match(Builders<Build>.Filter.Exists(b => b.TargetQuoteConvention, false))
.Match(Builders<Build>.Filter.Exists("analysis"))
.Unwind("analysis")
.Match(Builders<BsonDocument>.Filter.Ne("analysis.targetQuoteConvention", ""))
.AppendStage<BsonDocument>(
new BsonDocument(
"$set",
new BsonDocument("targetQuoteConvention", "$analysis.targetQuoteConvention")
)
)
.Merge(c, new MergeStageOptions<Build> { WhenMatched = MergeStageWhenMatched.Replace })
.ToListAsync();
await MongoMigrations.MigrateTargetQuoteConvention(c);
}
);
configurator.AddRepository<Pretranslation>(
Expand Down
61 changes: 61 additions & 0 deletions src/Serval/src/Serval.Translation/Configuration/MongoMigrations.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
using MongoDB.Bson;
using MongoDB.Driver;

namespace Serval.Translation.Configuration;

public class MongoMigrations
{
public static async Task MigrateTargetQuoteConvention(IMongoCollection<Build> c)
{
// migrate by adding TargetQuoteConvention field populated from analysis field
await c.Aggregate()
.Match(Builders<Build>.Filter.Exists(b => b.TargetQuoteConvention, false))
.Match(Builders<Build>.Filter.Exists("analysis"))
.AppendStage<BsonDocument>(
new BsonDocument(
"$set",
new BsonDocument(
"targetQuoteConvention",
new BsonDocument(
"$ifNull",
new BsonArray()
{
new BsonDocument(
"$first",
new BsonDocument(
"$map",
new BsonDocument
{
{
"input",
new BsonDocument(
"$filter",
new BsonDocument
{
{ "input", "$analysis" },
{ "as", "a" },
{
"cond",
new BsonDocument(
"$ne",
new BsonArray { "$$a.targetQuoteConvention", "" }
)
}
}
)
},
{ "as", "a" },
{ "in", "$$a.targetQuoteConvention" }
}
)
),
""
}
)
)
)
)
.Merge(c, new MergeStageOptions<Build> { WhenMatched = MergeStageWhenMatched.Replace })
.ToListAsync();
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Google.Protobuf.WellKnownTypes;
using Serval.Translation.Configuration;
using Serval.Translation.Models;
using Serval.Translation.V1;
using SIL.ServiceToolkit.Services;
Expand Down Expand Up @@ -2459,6 +2460,52 @@ await corporaClient.UpdateAsync(
Assert.That(newEngine2.ParallelCorpora[0].TargetCorpora[0].Files.Count, Is.EqualTo(1));
}

[Test]
public async Task MongoMigration_TargetQuoteConvention()
{
await _env.Builds.InsertAsync(
new Build()
{
Id = "111111111111111111111111",
EngineRef = NMT_ENGINE1_ID,
Owner = "client1",
Analysis =
[
new Shared.Models.ParallelCorpusAnalysis()
{
ParallelCorpusRef = "111111111111111111111112",
TargetQuoteConvention = ""
},
new Shared.Models.ParallelCorpusAnalysis()
{
ParallelCorpusRef = "111111111111111111111113",
TargetQuoteConvention = "standard_english"
}
]
}
);

Build? unmigratedBuild = await _env.Builds.GetAsync(b => b.Id == "111111111111111111111111");
Assert.That(unmigratedBuild, Is.Not.Null);
Assert.Multiple(() =>
{
Assert.That(unmigratedBuild.Analysis, Has.Count.EqualTo(2));
Assert.That(unmigratedBuild.TargetQuoteConvention, Is.Null);
});

await MongoMigrations.MigrateTargetQuoteConvention(
_env.MongoClient.GetDatabase("serval_test").GetCollection<Build>("translation.builds")
);

Build? migratedBuild = await _env.Builds.GetAsync(b => b.Id == "111111111111111111111111");
Assert.That(migratedBuild, Is.Not.Null);
Assert.Multiple(() =>
{
Assert.That(migratedBuild.Analysis, Has.Count.EqualTo(2));
Assert.That(migratedBuild.TargetQuoteConvention, Is.EqualTo("standard_english"));
});
}

[TearDown]
public void TearDown()
{
Expand All @@ -2468,11 +2515,11 @@ public void TearDown()
private class TestEnvironment : DisposableBase
{
private readonly IServiceScope _scope;
private readonly MongoClient _mongoClient;
public readonly MongoClient MongoClient;

public TestEnvironment()
{
_mongoClient = new MongoClient();
MongoClient = new MongoClient();
ResetDatabases();

Factory = new ServalWebApplicationFactory();
Expand Down Expand Up @@ -2795,8 +2842,8 @@ public CorporaClient CreateCorporaClient()

public void ResetDatabases()
{
_mongoClient.DropDatabase("serval_test");
_mongoClient.DropDatabase("serval_test_jobs");
MongoClient.DropDatabase("serval_test");
MongoClient.DropDatabase("serval_test_jobs");
}

private static IFileSystem CreateFileSystem(IServiceProvider sp)
Expand Down
Loading