Path: redb/redb.Tests.Integration/Tests/Base/SchemeRenameTestsBase.cs Snapshot: redb, development snapshot 2026-12-06. This is the development state of the public mirrors; it may differ from the published packages. License: Apache-2.0. Repository: https://github.com/redbase-app/redb Page: https://redbase.app/src/dev-2026-12-06/redb/redb.Tests.Integration/Tests/Base/SchemeRenameTestsBase.cs ---- using redb.Core; using redb.Core.Exceptions; using redb.Core.Models.Entities; using redb.Core.Utils; using redb.Tests.Integration.Models; namespace redb.Tests.Integration.Tests.Base; /// /// RedbScheme.RenamedFrom: the sync renames the stored scheme in place (same id, same objects), a second sync does /// nothing, both names stored is an error, and a class whose scheme was renamed away by a version this application /// does not have stops instead of creating the old scheme again. Every scheme rename records the name it came from in /// _schemes._renamed_from, the FullName-to-explicit-name rename included. /// public abstract class SchemeRenameTestsBase : IAsyncLifetime { protected readonly IRedbService Redb; protected SchemeRenameTestsBase(IRedbService redb) => Redb = redb; private static readonly string[] Schemes = [ typeof(SchemeRenameV1).FullName!, "rename_scheme_v2", typeof(SchemeRenameBothV1).FullName!, "rename_scheme_both", "rename_scheme_chain", "rename_scheme_chain_a", "rename_scheme_chain_b", typeof(SchemeRenameGuardV1).FullName!, "rename_scheme_ghost", typeof(SchemeRenameReuseProps).FullName!, typeof(SchemeRenameLegacyProps).FullName!, "rename_scheme_legacy", ]; public async Task InitializeAsync() => await ResetAsync(); public async Task DisposeAsync() => await ResetAsync(); private async Task ResetAsync() { foreach (var name in Schemes) { await Redb.Context.ExecuteAsync( $"DELETE FROM _objects WHERE _id_scheme IN (SELECT _id FROM _schemes WHERE _name = '{name}')"); await Redb.Context.ExecuteAsync( $"DELETE FROM _structures WHERE _id_scheme IN (SELECT _id FROM _schemes WHERE _name = '{name}')"); await Redb.Context.ExecuteAsync($"DELETE FROM _schemes WHERE _name = '{name}'"); } Redb.Cache.Clear(); } private Task SchemeIdAsync(string name) => Redb.Context.ExecuteScalarAsync($"SELECT _id FROM _schemes WHERE _name = '{name}'"); private Task RenamedFromAsync(long schemeId) => Redb.Context.ExecuteScalarAsync($"SELECT _renamed_from FROM _schemes WHERE _id = {schemeId}"); /// A scheme row written directly: the state another application version left behind. private async Task InsertSchemeRawAsync(string name, string? renamedFrom = null) { var id = await Redb.Context.NextObjectIdAsync(); await Redb.Context.ExecuteAsync($"INSERT INTO _schemes (_id, _name, _type) VALUES ({id}, '{name}', {RedbTypeIds.Class})"); if (renamedFrom != null) await Redb.Context.ExecuteAsync($"UPDATE _schemes SET _renamed_from = '{renamedFrom}' WHERE _id = {id}"); Redb.Cache.Clear(); return id; } [Fact] public async Task RenamedFrom_RenamesTheStoredScheme_ObjectsStay() { var oldName = typeof(SchemeRenameV1).FullName!; var old = await Redb.SyncSchemeAsync(); var obj = new RedbObject { name = "scheme-rename", Props = new SchemeRenameV1 { Title = "s1" } }; obj.id = await Redb.SaveAsync(obj); (await Redb.Query().Where(x => x.Title == "s1").CountAsync()).Should().Be(1); var renamed = await Redb.SyncSchemeAsync(); renamed.Id.Should().Be(old.Id, "the rename keeps the scheme"); renamed.Name.Should().Be("rename_scheme_v2"); (await SchemeIdAsync(oldName)).Should().BeNull(); (await RenamedFromAsync(old.Id)).Should().Be($"[\"{oldName}\"]"); (await Redb.GetSchemeByTypeAsync())!.Id.Should().Be(old.Id, "the cached scheme follows the name"); (await Redb.LoadAsync(obj.id))!.Props.Title.Should().Be("s1"); (await Redb.Query().Where(x => x.Title == "s1").CountAsync()).Should().Be(1); // A second sync is a no-op. (await Redb.SyncSchemeAsync()).Id.Should().Be(old.Id); (await RenamedFromAsync(old.Id)).Should().Be($"[\"{oldName}\"]"); } [Fact] public async Task BothNamesStored_IsAnError_AndChangesNothing() { var oldName = typeof(SchemeRenameBothV1).FullName!; var oldId = (await Redb.SyncSchemeAsync()).Id; var newId = await InsertSchemeRawAsync("rename_scheme_both"); var act = () => Redb.SyncSchemeAsync(); var error = (await act.Should().ThrowAsync()).Which; error.StoredNames.Should().Equal(oldName); (await SchemeIdAsync(oldName)).Should().Be(oldId); (await SchemeIdAsync("rename_scheme_both")).Should().Be(newId); } [Fact] public async Task Chain_TheStoredPreviousName_IsRenamed_AndTheHistoryGrows() { var id = await InsertSchemeRawAsync("rename_scheme_chain_b", "[\"rename_scheme_chain_a\"]"); var scheme = await Redb.SyncSchemeAsync(); scheme.Id.Should().Be(id); (await RenamedFromAsync(id)).Should().Be("[\"rename_scheme_chain_a\",\"rename_scheme_chain_b\"]"); } [Fact] public async Task Chain_TwoPreviousNamesStored_IsAnError() { await InsertSchemeRawAsync("rename_scheme_chain_a"); await InsertSchemeRawAsync("rename_scheme_chain_b"); var act = () => Redb.SyncSchemeAsync(); (await act.Should().ThrowAsync()).Which.StoredNames .Should().BeEquivalentTo("rename_scheme_chain_a", "rename_scheme_chain_b"); (await SchemeIdAsync("rename_scheme_chain")).Should().BeNull("nothing is created"); } [Fact] public async Task OldClass_SchemeRenamedAwayByAnotherVersion_Stops() { var name = typeof(SchemeRenameGuardV1).FullName!; // A version this process does not have renamed the scheme of SchemeRenameGuardV1 to rename_scheme_ghost. var ghostId = await InsertSchemeRawAsync("rename_scheme_ghost", $"[\"{name}\"]"); var act = () => Redb.SyncSchemeAsync(); var error = (await act.Should().ThrowAsync()).Which; error.Name.Should().Be(name); error.StoredNames.Should().Equal("rename_scheme_ghost"); (await SchemeIdAsync(name)).Should().BeNull("the old scheme is not created again"); (await SchemeIdAsync("rename_scheme_ghost")).Should().Be(ghostId); } [Fact] public async Task FreedName_TakenByANewClass_IsAllowed_WhenTheRenamedSchemeIsDeclaredHere() { var name = typeof(SchemeRenameReuseProps).FullName!; // rename_scheme_v2 is declared by SchemeRenameV2 in this application, so the freed name may be taken again. var renamedId = await InsertSchemeRawAsync("rename_scheme_v2", $"[\"{name}\"]"); var scheme = await Redb.SyncSchemeAsync(); scheme.Name.Should().Be(name); scheme.Id.Should().NotBe(renamedId); } [Fact] public async Task FullNameToExplicitName_RecordsTheHistory() { var fullName = typeof(SchemeRenameLegacyProps).FullName!; var id = await InsertSchemeRawAsync(fullName); var scheme = await Redb.SyncSchemeAsync(); scheme.Id.Should().Be(id); scheme.Name.Should().Be("rename_scheme_legacy"); (await RenamedFromAsync(id)).Should().Be($"[\"{fullName}\"]"); } }