-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTimescaleDatabaseModelFactory.cs
More file actions
55 lines (48 loc) · 2.51 KB
/
TimescaleDatabaseModelFactory.cs
File metadata and controls
55 lines (48 loc) · 2.51 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
using CmdScale.EntityFrameworkCore.TimescaleDB.Design.Scaffolding;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Diagnostics;
using Microsoft.EntityFrameworkCore.Scaffolding;
using Microsoft.EntityFrameworkCore.Scaffolding.Metadata;
using Npgsql.EntityFrameworkCore.PostgreSQL.Scaffolding.Internal;
using System.Data.Common;
namespace CmdScale.EntityFrameworkCore.TimescaleDB.Design
{
#pragma warning disable EF1001
/// <summary>
/// Database model factory that extends Npgsql's scaffolding to include TimescaleDB-specific features.
/// Handles extraction of hypertables, reorder policies, and continuous aggregates during db-first scaffolding.
/// </summary>
public class TimescaleDatabaseModelFactory(IDiagnosticsLogger<DbLoggerCategory.Scaffolding> logger)
: NpgsqlDatabaseModelFactory(logger)
{
private readonly List<(ITimescaleFeatureExtractor Extractor, IAnnotationApplier Applier)> _features =
[
(new HypertableScaffoldingExtractor(), new HypertableAnnotationApplier()),
(new ReorderPolicyScaffoldingExtractor(), new ReorderPolicyAnnotationApplier()),
(new ContinuousAggregateScaffoldingExtractor(), new ContinuousAggregateAnnotationApplier())
];
public override DatabaseModel Create(DbConnection connection, DatabaseModelFactoryOptions options)
{
DatabaseModel databaseModel = base.Create(connection, options);
// Extract all TimescaleDB features from the database
List<Dictionary<(string Schema, string TableName), object>> allFeatureData = [.. _features.Select(feature => feature.Extractor.Extract(connection))];
// Apply annotations to tables/views in the model
foreach (DatabaseTable table in databaseModel.Tables)
{
if (table?.Schema == null) continue;
(string Schema, string Name) tableKey = (table.Schema, table.Name);
// Apply each feature's annotations if the table has that feature
for (int i = 0; i < _features.Count; i++)
{
Dictionary<(string Schema, string TableName), object> featureData = allFeatureData[i];
if (featureData.TryGetValue(tableKey, out object? featureInfo))
{
_features[i].Applier.ApplyAnnotations(table, featureInfo);
}
}
}
return databaseModel;
}
}
#pragma warning restore EF1001
}