Skip to content

Commit

Permalink
Added possibility to explicitly allow enum labels change during upgrade
Browse files Browse the repository at this point in the history
  • Loading branch information
karasek committed Jun 17, 2017
1 parent 90bafe2 commit 4e669ba
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 3 deletions.
1 change: 1 addition & 0 deletions BTDB/BTDB.csproj
Expand Up @@ -116,6 +116,7 @@
<Compile Include="EventStoreLayer\TypeSerializers.cs" />
<Compile Include="EventStoreLayer\TypeSerializersMapping.cs" />
<Compile Include="FieldHandler\BasicSerializersFactory.cs" />
<Compile Include="FieldHandler\BinaryCompatibilityOnlyAttribute.cs" />
<Compile Include="FieldHandler\ByteArrayFieldHandler.cs" />
<Compile Include="FieldHandler\ByteArrayLastFieldHandler.cs" />
<Compile Include="FieldHandler\DBIndirect.cs" />
Expand Down
9 changes: 9 additions & 0 deletions BTDB/FieldHandler/BinaryCompatibilityOnlyAttribute.cs
@@ -0,0 +1,9 @@
using System;

namespace BTDB.FieldHandler
{
[AttributeUsage(AttributeTargets.Enum)]
public class BinaryCompatibilityOnlyAttribute : Attribute
{
}
}
12 changes: 9 additions & 3 deletions BTDB/FieldHandler/EnumFieldHandler.cs
Expand Up @@ -272,11 +272,17 @@ public IFieldHandler SpecializeLoadForType(Type type, IFieldHandler typeHandler)
{
if (typeHandler == this) return this;
var enumTypeHandler = typeHandler as EnumFieldHandler;
if (enumTypeHandler != null)
if (enumTypeHandler != null && _signed == enumTypeHandler._signed)
{
if (_signed == enumTypeHandler._signed && new EnumConfiguration(Configuration).IsSubsetOf(new EnumConfiguration(enumTypeHandler.Configuration)))
if (type.GetCustomAttributes(typeof (BinaryCompatibilityOnlyAttribute), false).Length != 0)
{
return typeHandler;
if (new EnumConfiguration(Configuration).IsBinaryRepresentationSubsetOf(new EnumConfiguration(enumTypeHandler.Configuration)))
return typeHandler;
}
else
{
if (new EnumConfiguration(Configuration).IsSubsetOf(new EnumConfiguration(enumTypeHandler.Configuration)))
return typeHandler;
}
}
if (_enumType == null && type.IsEnum)
Expand Down
44 changes: 44 additions & 0 deletions BTDBTest/ObjectDBTest.cs
Expand Up @@ -2282,7 +2282,51 @@ public void Indirect2InlineAutoConversion()
Assert.Equal("ModifiedRoot", root.Content);
Assert.Equal("ModifiedLeft", root.Left.Content);
}
}

public enum StateV1
{
A = 1,
B = 2,
}

public class WithState1
{
public StateV1 State { get; set; }
public IDictionary<StateV1, string> S { get; set; }
}

[BinaryCompatibilityOnly]
public enum StateV2
{
A2 = 1,
B2 = 2,
}

public class WithState2
{
public StateV2 State { get; set; }
public IDictionary<StateV2, string> S { get; set; }
}

[Fact]
public void BinaryCompatibleEnums()
{
var typeName = _db.RegisterType(typeof(WithState1));
using (var tr = _db.StartTransaction())
{
tr.Store(new WithState1 { State = StateV1.A, S = new Dictionary<StateV1, string> { { StateV1.B, "b" } } });
tr.Commit();
}
ReopenDb();
_db.RegisterType(typeof(WithState2), typeName);
using (var tr = _db.StartReadOnlyTransaction())
{
var v = tr.Enumerate<WithState2>().First();
Assert.Equal(StateV2.A2, v.State);
Assert.Equal("b", v.S[StateV2.B2]);
}
}

}
}

0 comments on commit 4e669ba

Please sign in to comment.