Skip to content

Commit 953ad1c

Browse files
committed
Generate UUIDv7 values for value-generated strings (#3462)
Fixes #3460 (cherry picked from commit 16841f9)
1 parent badb51e commit 953ad1c

File tree

3 files changed

+34
-7
lines changed

3 files changed

+34
-7
lines changed

src/EFCore.PG/ValueGeneration/Internal/NpgsqlValueGeneratorSelector.cs

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -101,9 +101,14 @@ public override bool TrySelect(IProperty property, ITypeBase typeBase, out Value
101101
/// doing so can result in application failures when updating to a new Entity Framework Core release.
102102
/// </summary>
103103
protected override ValueGenerator? FindForType(IProperty property, ITypeBase typeBase, Type clrType)
104-
=> property.ClrType.UnwrapNullableType() == typeof(Guid)
105-
? property.ValueGenerated == ValueGenerated.Never || property.GetDefaultValueSql() is not null
106-
? new TemporaryGuidValueGenerator()
107-
: new NpgsqlSequentialGuidValueGenerator()
108-
: base.FindForType(property, typeBase, clrType);
104+
=> property.ClrType.UnwrapNullableType() switch
105+
{
106+
var t when t == typeof(Guid) && property.ValueGenerated is not ValueGenerated.Never && property.GetDefaultValueSql() is null
107+
=> new NpgsqlSequentialGuidValueGenerator(),
108+
109+
var t when t == typeof(string) && property.ValueGenerated is not ValueGenerated.Never && property.GetDefaultValueSql() is null
110+
=> new NpgsqlSequentialStringValueGenerator(),
111+
112+
_ => base.FindForType(property, typeBase, clrType)
113+
};
109114
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
namespace Npgsql.EntityFrameworkCore.PostgreSQL.ValueGeneration;
2+
3+
/// <summary>
4+
/// This API supports the Entity Framework Core infrastructure and is not intended to be used
5+
/// directly from your code. This API may change or be removed in future releases.
6+
/// </summary>
7+
public class NpgsqlSequentialStringValueGenerator : ValueGenerator<string>
8+
{
9+
private readonly NpgsqlSequentialGuidValueGenerator _guidGenerator = new();
10+
11+
/// <summary>
12+
/// This API supports the Entity Framework Core infrastructure and is not intended to be used
13+
/// directly from your code. This API may change or be removed in future releases.
14+
/// </summary>
15+
public override bool GeneratesTemporaryValues => false;
16+
17+
/// <summary>
18+
/// This API supports the Entity Framework Core infrastructure and is not intended to be used
19+
/// directly from your code. This API may change or be removed in future releases.
20+
/// </summary>
21+
public override string Next(EntityEntry entry) => _guidGenerator.Next(entry).ToString();
22+
}

test/EFCore.PG.Tests/NpgsqlValueGeneratorSelectorTest.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ public void Returns_built_in_generators_for_types_setup_for_value_generation()
2121
AssertGenerator<TemporaryShortValueGenerator>("NullableShort");
2222
AssertGenerator<TemporaryByteValueGenerator>("NullableByte");
2323
AssertGenerator<TemporaryDecimalValueGenerator>("Decimal");
24-
AssertGenerator<StringValueGenerator>("String");
24+
AssertGenerator<NpgsqlSequentialStringValueGenerator>("String");
2525
AssertGenerator<NpgsqlSequentialGuidValueGenerator>("Guid");
2626
AssertGenerator<BinaryValueGenerator>("Binary");
2727
}
@@ -128,7 +128,7 @@ public void Returns_sequence_value_generators_when_configured_for_model()
128128
AssertGenerator<NpgsqlSequenceHiLoValueGenerator<int>>("NullableInt", setSequences: true);
129129
AssertGenerator<NpgsqlSequenceHiLoValueGenerator<long>>("NullableLong", setSequences: true);
130130
AssertGenerator<NpgsqlSequenceHiLoValueGenerator<short>>("NullableShort", setSequences: true);
131-
AssertGenerator<StringValueGenerator>("String", setSequences: true);
131+
AssertGenerator<NpgsqlSequentialStringValueGenerator>("String", setSequences: true);
132132
AssertGenerator<NpgsqlSequentialGuidValueGenerator>("Guid", setSequences: true);
133133
AssertGenerator<BinaryValueGenerator>("Binary", setSequences: true);
134134
}

0 commit comments

Comments
 (0)