Skip to content

Commit 27276c6

Browse files
committed
Added support for serial
1 parent d96f824 commit 27276c6

File tree

3 files changed

+68
-4
lines changed

3 files changed

+68
-4
lines changed
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
namespace EfCore.Ydb.Metadata.Internal;
2+
3+
public static class YdbAnnotationNames
4+
{
5+
public const string Prefix = "Ydb";
6+
7+
public const string Serial = Prefix + "Serial";
8+
}
Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,36 @@
1+
using System.Collections.Generic;
2+
using Microsoft.EntityFrameworkCore.Infrastructure;
13
using Microsoft.EntityFrameworkCore.Metadata;
4+
using Microsoft.EntityFrameworkCore.Metadata.Internal;
25

36
namespace EfCore.Ydb.Metadata.Internal;
47

58
public class YdbAnnotationProvider : RelationalAnnotationProvider
69
{
7-
public YdbAnnotationProvider(
8-
RelationalAnnotationProviderDependencies dependencies
9-
) : base(dependencies)
10+
public YdbAnnotationProvider(RelationalAnnotationProviderDependencies dependencies) : base(dependencies)
1011
{
1112
}
13+
14+
public override IEnumerable<IAnnotation> For(IColumn column, bool designTime)
15+
{
16+
if (!designTime)
17+
{
18+
yield break;
19+
}
20+
21+
// TODO: Add Yson here too?
22+
if (column is JsonColumn)
23+
{
24+
yield break;
25+
}
26+
27+
var property = column.PropertyMappings[0].Property;
28+
29+
if (property.ValueGenerated == ValueGenerated.OnAdd
30+
&& property.ClrType == typeof(int)
31+
&& property.FindTypeMapping()?.Converter == null)
32+
{
33+
yield return new Annotation(YdbAnnotationNames.Serial, true);
34+
}
35+
}
1236
}

src/EfCore.Ydb/src/Migrations/YdbMigrationsSqlGenerator.cs

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
using System;
2+
using EfCore.Ydb.Metadata.Internal;
13
using Microsoft.EntityFrameworkCore.Metadata;
24
using Microsoft.EntityFrameworkCore.Migrations;
35
using Microsoft.EntityFrameworkCore.Migrations.Operations;
@@ -46,7 +48,37 @@ protected override void Generate(
4648
EndStatement(builder, suppressTransaction: true);
4749
}
4850
}
49-
51+
52+
protected override void ColumnDefinition(
53+
string? schema,
54+
string table,
55+
string name,
56+
ColumnOperation operation,
57+
IModel? model,
58+
MigrationCommandListBuilder builder
59+
)
60+
{
61+
var columnType = operation.ColumnType ?? GetColumnType(schema, table, name, operation, model)!;
62+
var autoincrement = operation[YdbAnnotationNames.Serial] as bool?;
63+
64+
if (autoincrement == true)
65+
{
66+
columnType = columnType.ToLower() switch
67+
{
68+
"int32" => "SERIAL",
69+
"int64" => "BIGSERIAL",
70+
_ => throw new NotSupportedException("Serial column supported only for int32 and int64")
71+
};
72+
}
73+
74+
builder
75+
.Append(Dependencies.SqlGenerationHelper.DelimitIdentifier(name))
76+
.Append(" ")
77+
// TODO: Add DEFAULT logic somewhere here
78+
.Append(columnType)
79+
.Append(operation.IsNullable ? " NULL" : " NOT NULL");
80+
}
81+
5082
protected override void CreateTablePrimaryKeyConstraint(
5183
CreateTableOperation operation,
5284
IModel? model,

0 commit comments

Comments
 (0)