-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHypertableConvention.cs
More file actions
68 lines (60 loc) · 3.39 KB
/
HypertableConvention.cs
File metadata and controls
68 lines (60 loc) · 3.39 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
using Microsoft.EntityFrameworkCore.Metadata;
using Microsoft.EntityFrameworkCore.Metadata.Builders;
using Microsoft.EntityFrameworkCore.Metadata.Conventions;
using System.Reflection;
namespace CmdScale.EntityFrameworkCore.TimescaleDB.Configuration.Hypertable
{
/// <summary>
/// A convention that configures an entity as a hypertable based on the presence of
/// the [Hypertable] attribute.
/// </summary>
public class HypertableConvention : IEntityTypeAddedConvention
{
/// <summary>
/// Called when an entity type is added to the model.
/// </summary>
/// <param name="entityTypeBuilder">The builder for the entity type.</param>
/// <param name="context">Additional information available during convention execution.</param>
public void ProcessEntityTypeAdded(IConventionEntityTypeBuilder entityTypeBuilder, IConventionContext<IConventionEntityTypeBuilder> context)
{
IConventionEntityType entityType = entityTypeBuilder.Metadata;
HypertableAttribute? attribute = entityType.ClrType?.GetCustomAttribute<HypertableAttribute>();
if (attribute != null)
{
// Apply the annotations that the Fluent API would have applied.
entityTypeBuilder.HasAnnotation(HypertableAnnotations.IsHypertable, true);
entityTypeBuilder.HasAnnotation(HypertableAnnotations.HypertableTimeColumn, attribute.TimeColumnName);
if (!string.IsNullOrEmpty(attribute.ChunkTimeInterval))
{
entityTypeBuilder.HasAnnotation(HypertableAnnotations.ChunkTimeInterval, attribute.ChunkTimeInterval);
}
if (attribute.EnableCompression == true)
{
entityTypeBuilder.HasAnnotation(HypertableAnnotations.EnableCompression, true);
}
if (attribute.MigrateData == true)
{
entityTypeBuilder.HasAnnotation(HypertableAnnotations.MigrateData, true);
}
if (attribute.ChunkSkipColumns != null && attribute.ChunkSkipColumns.Length > 0)
{
// Chunk skipping requires compression to be enabled
entityTypeBuilder.HasAnnotation(HypertableAnnotations.EnableCompression, true);
entityTypeBuilder.HasAnnotation(HypertableAnnotations.ChunkSkipColumns, string.Join(",", attribute.ChunkSkipColumns));
}
if (attribute.CompressionSegmentBy != null && attribute.CompressionSegmentBy.Length > 0)
{
// SegmentBy requires compression to be enabled
entityTypeBuilder.HasAnnotation(HypertableAnnotations.EnableCompression, true);
entityTypeBuilder.HasAnnotation(HypertableAnnotations.CompressionSegmentBy, string.Join(", ", attribute.CompressionSegmentBy));
}
if (attribute.CompressionOrderBy != null && attribute.CompressionOrderBy.Length > 0)
{
// OrderBy requires compression to be enabled
entityTypeBuilder.HasAnnotation(HypertableAnnotations.EnableCompression, true);
entityTypeBuilder.HasAnnotation(HypertableAnnotations.CompressionOrderBy, string.Join(", ", attribute.CompressionOrderBy));
}
}
}
}
}