-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathTimescaleMigrationsModelDiffer.cs
More file actions
73 lines (65 loc) · 3.19 KB
/
TimescaleMigrationsModelDiffer.cs
File metadata and controls
73 lines (65 loc) · 3.19 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
69
70
71
72
73
using CmdScale.EntityFrameworkCore.TimescaleDB.Internals.Features;
using CmdScale.EntityFrameworkCore.TimescaleDB.Internals.Features.ContinuousAggregates;
using CmdScale.EntityFrameworkCore.TimescaleDB.Internals.Features.Hypertables;
using CmdScale.EntityFrameworkCore.TimescaleDB.Internals.Features.ReorderPolicies;
using CmdScale.EntityFrameworkCore.TimescaleDB.Operations;
using Microsoft.EntityFrameworkCore.Metadata;
using Microsoft.EntityFrameworkCore.Migrations;
using Microsoft.EntityFrameworkCore.Migrations.Internal;
using Microsoft.EntityFrameworkCore.Migrations.Operations;
using Microsoft.EntityFrameworkCore.Storage;
using Microsoft.EntityFrameworkCore.Update.Internal;
namespace CmdScale.EntityFrameworkCore.TimescaleDB.Internals
{
#pragma warning disable EF1001 // Suppress warning about internal APIs usage, common for providers/extensions
public class TimescaleMigrationsModelDiffer(
IRelationalTypeMappingSource typeMappingSource,
IMigrationsAnnotationProvider migrationsAnnotationProvider,
IRelationalAnnotationProvider relationalAnnotationProvider,
IRowIdentityMapFactory rowIdentityMapFactory,
CommandBatchPreparerDependencies commandBatchPreparerDependencies) : MigrationsModelDiffer(typeMappingSource, migrationsAnnotationProvider, relationalAnnotationProvider, rowIdentityMapFactory, commandBatchPreparerDependencies)
{
private readonly IReadOnlyList<IFeatureDiffer> _featureDiffers = [
new HypertableDiffer(),
new ReorderPolicyDiffer(),
new ContinuousAggregateDiffer(),
];
public override IReadOnlyList<MigrationOperation> GetDifferences(IRelationalModel? source, IRelationalModel? target)
{
// Get all operations
List<MigrationOperation> allOperations = [.. base.GetDifferences(source, target)];
foreach (IFeatureDiffer differ in _featureDiffers)
{
allOperations.AddRange(differ.GetDifferences(source, target));
}
// Sort the entire list based on the priority defined in the helper method
List<MigrationOperation> sortedOperations = [.. allOperations.OrderBy(GetOperationPriority)];
return sortedOperations;
}
/// <summary>
/// Assigns a priority to operations to ensure correct execution order.
/// Lower numbers execute first.
/// </summary>
private static int GetOperationPriority(MigrationOperation operation)
{
switch (operation)
{
case CreateHypertableOperation:
return 10;
case AddReorderPolicyOperation:
case AlterReorderPolicyOperation:
case DropReorderPolicyOperation:
return 20;
case CreateContinuousAggregateOperation:
return 30;
case AlterContinuousAggregateOperation:
case DropContinuousAggregateOperation:
return 40;
// Standard EF Core operations (CreateTable, etc.)
default:
return 0;
}
}
}
#pragma warning restore EF1001
}