-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathConflictResolutionEngine.cs
More file actions
342 lines (289 loc) · 10.7 KB
/
Copy pathConflictResolutionEngine.cs
File metadata and controls
342 lines (289 loc) · 10.7 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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
// =============================================================================
// Author: Vladyslav Zaiets | https://sarmkadan.com
// CTO & Software Architect
// =============================================================================
#nullable enable
using EfMigrationDiff.Models;
namespace EfMigrationDiff.Analysis;
/// <summary>
/// Engine for analyzing and suggesting resolutions for migration conflicts.
/// Detects conflict patterns and provides recommendations for resolution strategies.
/// </summary>
public sealed class ConflictResolutionEngine
{
private readonly Dictionary<EfMigrationDiff.Models.ConflictType, Func<ConflictInfo, ResolutionStrategy>> _strategies = new();
/// <summary>
/// Initializes a new instance of the <see cref="ConflictResolutionEngine"/> class.
/// </summary>
public ConflictResolutionEngine()
{
InitializeDefaultStrategies();
}
/// <summary>
/// Initializes default resolution strategies for common conflict types.
/// </summary>
private void InitializeDefaultStrategies()
{
_strategies[EfMigrationDiff.Models.ConflictType.TableConflict] = conflict => new ResolutionStrategy
{
Type = ResolutionType.Manual,
Description = "Manually review and merge the conflicting table operations",
Priority = 2
};
_strategies[EfMigrationDiff.Models.ConflictType.ColumnConflict] = conflict => new ResolutionStrategy
{
Type = ResolutionType.Review,
Description = "Review and test the conflicting column changes carefully to prevent data loss",
Priority = 3,
IsHighRisk = true
};
_strategies[EfMigrationDiff.Models.ConflictType.IndexConflict] = conflict => new ResolutionStrategy
{
Type = ResolutionType.Automatic,
Description = "Can be safely merged - index operations are usually idempotent",
Priority = 1
};
}
/// <summary>
/// Analyzes a conflict and returns resolution suggestions.
/// </summary>
public ConflictResolution ResolveConflict(ConflictInfo conflict)
{
var resolution = new ConflictResolution
{
ConflictId = conflict.Id,
ConflictType = conflict.ConflictType,
AnalyzedAt = DateTime.UtcNow
};
// Determine conflict type and get strategy
if (_strategies.TryGetValue(conflict.ConflictType, out var strategyFunc))
{
resolution.RecommendedStrategy = strategyFunc(conflict);
}
else
{
resolution.RecommendedStrategy = GetDefaultStrategy(conflict);
}
// Analyze severity
resolution.Severity = AnalyzeSeverity(conflict);
// Generate recommendations
resolution.Recommendations = GenerateRecommendations(conflict);
return resolution;
}
/// <summary>
/// Analyzes multiple conflicts and generates a batch resolution report.
/// </summary>
public ConflictResolutionReport ResolveBatch(IEnumerable<ConflictInfo> conflicts)
{
var report = new ConflictResolutionReport
{
AnalyzedAt = DateTime.UtcNow
};
var conflictList = conflicts.ToList();
foreach (var conflict in conflictList)
{
var resolution = ResolveConflict(conflict);
report.Resolutions.Add(resolution);
}
// Calculate summary
report.TotalConflicts = conflictList.Count;
report.CriticalCount = report.Resolutions.Count(r => r.Severity == ConflictSeverity.Critical);
report.HighCount = report.Resolutions.Count(r => r.Severity == ConflictSeverity.High);
report.CanAutoResolve = report.Resolutions.Count(r => r.RecommendedStrategy.Type == ResolutionType.Automatic);
return report;
}
/// <summary>
/// Gets the default resolution strategy for unknown conflict types.
/// </summary>
private ResolutionStrategy GetDefaultStrategy(ConflictInfo conflict)
{
return new ResolutionStrategy
{
Type = ResolutionType.Manual,
Description = "Requires manual review and resolution",
Priority = 2
};
}
/// <summary>
/// Analyzes the severity of a conflict.
/// </summary>
private ConflictSeverity AnalyzeSeverity(ConflictInfo conflict)
{
// Check for data loss potential
if (conflict.ConflictType == EfMigrationDiff.Models.ConflictType.ColumnConflict)
return ConflictSeverity.Critical;
// Check for blocking conflicts
if (conflict.IsBlocking())
return ConflictSeverity.High;
return ConflictSeverity.Medium;
}
/// <summary>
/// Generates specific recommendations for resolving a conflict.
/// </summary>
private List<string> GenerateRecommendations(ConflictInfo conflict)
{
var recommendations = new List<string>();
switch (conflict.ConflictType)
{
case EfMigrationDiff.Models.ConflictType.TableConflict:
recommendations.Add("Review competing table changes side by side before merging");
recommendations.Add("Validate the final table definition against both branches");
recommendations.Add("Test any dependent queries or procedures after resolution");
break;
case EfMigrationDiff.Models.ConflictType.ColumnConflict:
recommendations.Add("Backup database before applying this migration");
recommendations.Add("Verify no application code depends on the conflicting column definition");
recommendations.Add("Document the final column contract after resolution");
break;
case EfMigrationDiff.Models.ConflictType.IndexConflict:
recommendations.Add("Verify index names don't conflict");
recommendations.Add("Consider merging index definitions if possible");
break;
case EfMigrationDiff.Models.ConflictType.ConstraintConflict:
recommendations.Add("Review foreign key constraints");
recommendations.Add("Ensure referential integrity is maintained");
recommendations.Add("Check for circular dependencies");
break;
default:
recommendations.Add("Review conflict manually");
recommendations.Add("Run unit tests after resolution");
break;
}
recommendations.Add("Run comprehensive integration tests");
return recommendations;
}
/// <summary>
/// Registers a custom resolution strategy for a conflict type.
/// </summary>
public void RegisterStrategy(EfMigrationDiff.Models.ConflictType type, Func<ConflictInfo, ResolutionStrategy> strategy)
{
_strategies[type] = strategy;
}
}
/// <summary>
/// Resolution strategy for a conflict.
/// </summary>
public class ResolutionStrategy
{
/// <summary>
/// Gets or sets the recommended resolution type.
/// </summary>
public ResolutionType Type { get; set; }
/// <summary>
/// Gets or sets the human-readable strategy description.
/// </summary>
public string Description { get; set; } = string.Empty;
/// <summary>
/// Gets or sets the strategy priority where lower values indicate higher priority.
/// </summary>
public int Priority { get; set; }
/// <summary>
/// Gets or sets a value indicating whether the strategy carries elevated risk.
/// </summary>
public bool IsHighRisk { get; set; }
}
/// <summary>
/// Complete resolution analysis for a conflict.
/// </summary>
public class ConflictResolution
{
/// <summary>
/// Gets or sets the identifier of the analyzed conflict.
/// </summary>
public string ConflictId { get; set; } = string.Empty;
/// <summary>
/// Gets or sets the original conflict type.
/// </summary>
public EfMigrationDiff.Models.ConflictType ConflictType { get; set; }
/// <summary>
/// Gets or sets the UTC timestamp when analysis completed.
/// </summary>
public DateTime AnalyzedAt { get; set; }
/// <summary>
/// Gets or sets the derived resolution severity.
/// </summary>
public ConflictSeverity Severity { get; set; }
/// <summary>
/// Gets or sets the recommended strategy.
/// </summary>
public ResolutionStrategy RecommendedStrategy { get; set; } = new();
/// <summary>
/// Gets or sets the generated recommendations.
/// </summary>
public List<string> Recommendations { get; set; } = new();
}
/// <summary>
/// Report of conflict resolutions for a batch.
/// </summary>
public class ConflictResolutionReport
{
/// <summary>
/// Gets or sets the UTC timestamp when the batch analysis completed.
/// </summary>
public DateTime AnalyzedAt { get; set; }
/// <summary>
/// Gets or sets the per-conflict resolution details.
/// </summary>
public List<ConflictResolution> Resolutions { get; set; } = new();
/// <summary>
/// Gets or sets the total number of analyzed conflicts.
/// </summary>
public int TotalConflicts { get; set; }
/// <summary>
/// Gets or sets the number of critical conflicts.
/// </summary>
public int CriticalCount { get; set; }
/// <summary>
/// Gets or sets the number of high-severity conflicts.
/// </summary>
public int HighCount { get; set; }
/// <summary>
/// Gets or sets the number of conflicts that can be automatically resolved.
/// </summary>
public int CanAutoResolve { get; set; }
/// <summary>
/// Gets a value indicating whether the batch can proceed without manual intervention.
/// </summary>
public bool CanProceedWithoutManualIntervention =>
!Resolutions.Any(r => r.RecommendedStrategy.Type == ResolutionType.Manual && r.Severity == ConflictSeverity.Critical);
}
/// <summary>
/// Severity levels used by the conflict resolution analysis.
/// </summary>
public enum ConflictSeverity
{
/// <summary>
/// Low severity.
/// </summary>
Low,
/// <summary>
/// Medium severity.
/// </summary>
Medium,
/// <summary>
/// High severity.
/// </summary>
High,
/// <summary>
/// Critical severity.
/// </summary>
Critical
}
/// <summary>
/// Resolution modes produced by the conflict analysis engine.
/// </summary>
public enum ResolutionType
{
/// <summary>
/// The conflict can be resolved automatically.
/// </summary>
Automatic,
/// <summary>
/// The conflict requires manual intervention.
/// </summary>
Manual,
/// <summary>
/// The conflict requires explicit review before proceeding.
/// </summary>
Review
}