diff --git a/tests/Aetheria.Shared.Tests/CatalogResaveTests.cs b/tests/Aetheria.Shared.Tests/CatalogResaveTests.cs new file mode 100644 index 000000000..eca4524a0 --- /dev/null +++ b/tests/Aetheria.Shared.Tests/CatalogResaveTests.cs @@ -0,0 +1,109 @@ +using System; +using System.Collections.Generic; +using System.IO; +using System.Linq; +using GameCult.Caching; +using GameCult.Caching.MessagePack; +using MessagePack; +using Xunit; + +// AetherDb catalog resave (follow-up aetherdb-catalog-resave): a record stored under an older schema id is re-saved under the +// current one, a dry run writes nothing, and a second apply writes nothing. The two probe types share one schema name; the +// second appends a member, which is how a real schema change restamps a record. +public sealed class CatalogResaveTests : IDisposable +{ + [MessagePackObject] + [CultDocument("aetheria.test.resaveprobe", "1")] + public sealed class ProbeV1 + { + [Key(0)] public string Name = ""; + } + + [MessagePackObject] + [CultDocument("aetheria.test.resaveprobe", "1")] + public sealed class ProbeV2 + { + [Key(0)] public string Name = ""; + [Key(1)] public int Appended = 7; + } + + private readonly TempDirectory _directory = new TempDirectory(); + private string Path_ => System.IO.Path.Combine(_directory.Path, "probe.cc"); + + public void Dispose() => _directory.Dispose(); + + private static CultCache Open(string path, Type type) + { + var cache = new CultCache(CultDocumentRegistry.ForTypes(new[] { type })); + cache.AddBackingStore(new SingleFileMessagePackBackingStore(path), new[] { type }); + return cache; + } + + private void WriteOld() + { + using var cache = Open(Path_, typeof(ProbeV1)); + cache.Commit(batch => + { + batch.Upsert(typeof(ProbeV1), new ProbeV1 { Name = "a" }, new CultRecordKey("a")); + batch.Upsert(typeof(ProbeV1), new ProbeV1 { Name = "b" }, new CultRecordKey("b")); + }); + cache.FlushAsync().Wait(); + } + + private string[] StoredIds() => + CultDocumentMessagePackSerialization.DeserializeSnapshot(File.ReadAllBytes(Path_)).Records.Select(r => r.SchemaId).ToArray(); + + [Fact] + public void ADryRunListsAndWritesNothing() + { + WriteOld(); + var before = File.ReadAllBytes(Path_); + var oldIds = StoredIds(); + using (var cache = Open(Path_, typeof(ProbeV2))) + { + var output = new StringWriter(); + Assert.Equal(0, CatalogResaveCommand.Run(cache, apply: false, output)); + Assert.Contains("2 record(s) would be re-saved", output.ToString()); + } + Assert.Equal(before, File.ReadAllBytes(Path_)); + Assert.Equal(oldIds, StoredIds()); + } + + [Fact] + public void ApplyRestampsEveryRecordAndASecondApplyWritesNothing() + { + WriteOld(); + var oldIds = StoredIds(); + string[] newIds; + using (var cache = Open(Path_, typeof(ProbeV2))) + { + Assert.Equal(0, CatalogResaveCommand.Run(cache, apply: true, new StringWriter())); + } + newIds = StoredIds(); + Assert.Equal(2, newIds.Length); + Assert.All(newIds, id => Assert.DoesNotContain(id, oldIds)); + var written = File.ReadAllBytes(Path_); + + using (var cache = Open(Path_, typeof(ProbeV2))) + { + Assert.Equal(new[] { "a", "b" }, cache.GetAll().Select(p => p.Name).OrderBy(n => n)); + Assert.All(cache.GetAll(), p => Assert.Equal(7, p.Appended)); + var output = new StringWriter(); + Assert.Equal(0, CatalogResaveCommand.Run(cache, apply: true, output)); + Assert.Contains("0 record(s) would be re-saved", output.ToString()); + } + Assert.Equal(written, File.ReadAllBytes(Path_)); + } + + // The catalog the game ships is current: resaving it changes nothing. + [Fact] + public void TheShippedCatalogIsAlreadyCurrent() + { + var copy = System.IO.Path.Combine(_directory.Path, "Aetheria.cc"); + File.Copy(System.IO.Path.Combine(RunStartTests.FindRepoRoot(), "GameData", "Aetheria.cc"), copy); + using var cache = AetheriaStores.Open(copy, catalogWritable: true, registry: TestCatalog.Registry()); + var output = new StringWriter(); + Assert.Equal(0, CatalogResaveCommand.Run(cache, apply: true, output)); + Assert.Contains("0 record(s) would be re-saved", output.ToString()); + } +} diff --git a/tools/AetherDb/CatalogResaveCommand.cs b/tools/AetherDb/CatalogResaveCommand.cs new file mode 100644 index 000000000..3b461defb --- /dev/null +++ b/tools/AetherDb/CatalogResaveCommand.cs @@ -0,0 +1,31 @@ +/* This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at https://mozilla.org/MPL/2.0/. */ + +using System.IO; +using System.Linq; +using GameCult.Caching; + +// catalog resave [apply] (aetheria-living-world follow-up aetherdb-catalog-resave): re-saves every record the catalog holds +// under an older schema id through the current schema, so a schema change (an appended field, a restamped id) never needs a +// hand restamp of GameData/Aetheria.cc. The cache loads each record through the game's own types, so a record reads whole +// and an appended field takes its initializer; the write puts it under the current id. A record already under the current +// id is not written, so a second apply writes nothing. Dry run unless apply. +public static class CatalogResaveCommand +{ + public static int Run(CultCache catalog, bool apply, TextWriter output) + { + var stale = catalog.AllStoredDocuments.Where(stored => stored.StoredSchemaId != stored.Descriptor.SchemaId).ToArray(); + foreach (var group in stale.GroupBy(stored => stored.Descriptor.SchemaName).OrderBy(group => group.Key, System.StringComparer.Ordinal)) + output.WriteLine($"{group.Key}: {group.Count()} record(s) under an older schema id"); + output.WriteLine($"{stale.Length} record(s) would be re-saved"); + if (!apply || stale.Length == 0) return 0; + catalog.Commit(batch => + { + foreach (var stored in stale) batch.Upsert(stored.Descriptor.DocumentType, stored.Document, stored.Key); + }); + catalog.FlushAsync().GetAwaiter().GetResult(); + output.WriteLine($"{stale.Length} record(s) re-saved"); + return 0; + } +} diff --git a/tools/AetherDb/Program.cs b/tools/AetherDb/Program.cs index fd75530ac..ff5fdaac6 100644 --- a/tools/AetherDb/Program.cs +++ b/tools/AetherDb/Program.cs @@ -86,6 +86,13 @@ public static class Program if (apply) db.Cache.FlushAsync().GetAwaiter().GetResult(); return code; } + case "catalog": + { + if (args.Length < 2 || args[1] != "resave") { Console.WriteLine("catalog resave [apply]"); return 1; } + var apply = args.Contains("apply"); + using var db = AetherDb.Open(catalogWritable: apply); + return CatalogResaveCommand.Run(db.Cache, apply, Console.Out); + } case "economy": return EconomyCommand.Run(args.Skip(1).ToArray(), Console.Out); case "auto-guns": return AutoGunsCatalog.Run(AetherDb.Open(catalogWritable: args.Contains("apply")).Cache, args.Contains("apply"), Console.Out); case "doctrine-catalog": return DoctrineCatalog.Run(AetherDb.Open(catalogWritable: args.Contains("apply")).Cache, args.Contains("apply"), Console.Out);