-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathContextDiff.cs
More file actions
266 lines (221 loc) · 12.3 KB
/
ContextDiff.cs
File metadata and controls
266 lines (221 loc) · 12.3 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
using AutoMapper;
using Microsoft.EntityFrameworkCore;
using Newtonsoft.Json.Linq;
using System;
using System.Buffers.Text;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data.SqlTypes;
using System.Diagnostics.CodeAnalysis;
using System.Linq;
using System.Reflection.Metadata;
using System.Runtime.Intrinsics.X86;
using System.Text;
using System.Threading.Tasks;
using static System.Net.Mime.MediaTypeNames;
namespace DataHelper
{
public class DbContextDiff
{
public class DbContextDifferences
{
public List<string> TablesOnlyInSource { get; set; } = new List<string>();
public List<string> TablesOnlyInDestination { get; set; } = new List<string>();
public Dictionary<string, string> MissingTableDescriptions { get; set; } = new Dictionary<string, string>();
public Dictionary<string, List<string>> ColumnsOnlyInSource { get; set; } = new Dictionary<string, List<string>>();
public Dictionary<string, List<string>> ColumnsOnlyInDestination { get; set; } = new Dictionary<string, List<string>>();
public Dictionary<string, Dictionary<string, TypeDiff>> ColumnTypeDifferences { get; set; } = new Dictionary<string, Dictionary<string, TypeDiff>>();
public Dictionary<string, string> MissingPrimaryKeys { get; set; } = new Dictionary<string, string>();
public Dictionary<string, Dictionary<string, object>> MissingDefaultValues { get; set; } = new Dictionary<string, Dictionary<string, object>>();
public Dictionary<string, List<string>> FieldsNotNullInDestination { get; set; } = new Dictionary<string, List<string>>();
public Dictionary<string, List<string>> FieldsNeedManualReview { get; set; } = new Dictionary<string, List<string>>();
public class TypeDiff
{
public Type SourceType { get; set; }
public Type DestinationType { get; set; }
public string Description { get; set; }
}
}
public static string GenerateSqlScript(DbContext sourceContext, DbContextDifferences differences)
{
var sqlBuilder = new StringBuilder();
foreach (var table in differences.TablesOnlyInSource)
{
sqlBuilder.AppendLine(GenerateCreateTableScript(sourceContext, table));
}
foreach (var table in differences.ColumnsOnlyInSource.Keys)
{
foreach (var column in differences.ColumnsOnlyInSource[table])
{
var entityType = sourceContext.Model.GetEntityTypes().FirstOrDefault(e => e.GetTableName() == table);
var prop = entityType?.GetProperties().FirstOrDefault(p => p.Name == column);
if (prop != null)
{
sqlBuilder.AppendLine(GenerateAddColumnScript(table, column, prop.GetColumnType()));
// Handle default values
var defaultValue = prop.GetDefaultValueSql() ?? prop.GetDefaultValue()?.ToString();
if (defaultValue != null)
{
sqlBuilder.AppendLine(GenerateAddDefaultValueScript(table, column, defaultValue));
}
}
}
}
foreach (var table in differences.MissingPrimaryKeys.Keys)
{
sqlBuilder.AppendLine(GenerateAddPrimaryKeyScript(table, differences.MissingPrimaryKeys[table]));
}
foreach (var table in differences.FieldsNotNullInDestination.Keys)
{
foreach (var column in differences.FieldsNotNullInDestination[table])
{
var entityType = sourceContext.Model.GetEntityTypes().FirstOrDefault(e => e.GetTableName() == table);
var prop = entityType?.GetProperties().FirstOrDefault(p => p.Name == column);
if (prop != null)
{
var defaultValue = prop.GetDefaultValueSql() ?? prop.GetDefaultValue()?.ToString();
//if (defaultValue != null)
//{
// // If a default value is available, set it
// sqlBuilder.AppendLine(GenerateAddDefaultValueScript(table, column, defaultValue));
//}
// Add the NOT NULL constraint
sqlBuilder.AppendLine($"ALTER TABLE {table} ALTER COLUMN {column} {prop.GetColumnType()} NOT NULL;");
if (defaultValue == null)
{
// Append a comment to the SQL script notifying the user of the potential risks
sqlBuilder.AppendLine($"-- WARNING: {table}.{column} is set to NOT NULL. Ensure no existing records have NULL values for this column before running the script.");
}
}
}
}
return sqlBuilder.ToString();
}
public static DbContextDifferences CompareDbContexts(DbContext sourceContext, DbContext destinationContext)
{
var result = new DbContextDifferences();
var sourceEntityTypes = sourceContext.Model.GetEntityTypes()
.Where(e => !string.IsNullOrEmpty(e.GetTableName()))
.ToList();
var destinationEntityTypes = destinationContext.Model.GetEntityTypes()
.Where(e => !string.IsNullOrEmpty(e.GetTableName()))
.ToList();
// Check for tables and columns only in the source context
foreach (var sourceEntityType in sourceEntityTypes)
{
var sourceTableName = sourceEntityType.GetTableName();
var destinationEntityType = destinationEntityTypes.FirstOrDefault(e => e.GetTableName() == sourceTableName);
if (destinationEntityType == null)
{
result.TablesOnlyInSource.Add(sourceTableName);
result.MissingTableDescriptions[sourceTableName] = $"Table '{sourceTableName}' exists in source context but not in destination context.";
}
else
{
foreach (var sourceColumn in sourceEntityType.GetProperties())
{
var destinationColumn = destinationEntityType.GetProperties().FirstOrDefault(p => p.Name == sourceColumn.Name);
if (destinationColumn == null)
{
if (!result.ColumnsOnlyInSource.ContainsKey(sourceTableName))
{
result.ColumnsOnlyInSource[sourceTableName] = new List<string>();
}
result.ColumnsOnlyInSource[sourceTableName].Add(sourceColumn.Name);
}
else if (sourceColumn.ClrType != destinationColumn.ClrType)
{
if (!result.ColumnTypeDifferences.ContainsKey(sourceTableName))
{
result.ColumnTypeDifferences[sourceTableName] = new Dictionary<string, DbContextDifferences.TypeDiff>();
}
result.ColumnTypeDifferences[sourceTableName][sourceColumn.Name] = new DbContextDifferences.TypeDiff
{
SourceType = sourceColumn.ClrType,
DestinationType = destinationColumn.ClrType,
Description = $"Column '{sourceColumn.Name}' in table '{sourceTableName}' has type '{sourceColumn.ClrType.Name}' in source but '{destinationColumn.ClrType.Name}' in destination."
};
}
else
{
if (!sourceColumn.IsNullable && destinationColumn.IsNullable)
{
if (!result.FieldsNotNullInDestination.ContainsKey(sourceTableName))
{
result.FieldsNotNullInDestination[sourceTableName] = new List<string>();
}
result.FieldsNotNullInDestination[sourceTableName].Add(sourceColumn.Name);
}
}
}
}
}
// Check for tables and columns only in the destination context
foreach (var destinationEntityType in destinationEntityTypes)
{
var destinationTableName = destinationEntityType.GetTableName();
var sourceEntityType = sourceEntityTypes.FirstOrDefault(e => e.GetTableName() == destinationTableName);
if (sourceEntityType == null)
{
result.TablesOnlyInDestination.Add(destinationTableName);
result.MissingTableDescriptions[destinationTableName] = $"Table '{destinationTableName}' exists in destination context but not in source context.";
}
else
{
foreach (var destinationColumn in destinationEntityType.GetProperties())
{
var sourceColumn = sourceEntityType.GetProperties().FirstOrDefault(p => p.Name == destinationColumn.Name);
if (sourceColumn == null)
{
if (!result.ColumnsOnlyInDestination.ContainsKey(destinationTableName))
{
result.ColumnsOnlyInDestination[destinationTableName] = new List<string>();
}
result.ColumnsOnlyInDestination[destinationTableName].Add(destinationColumn.Name);
}
}
}
}
return result;
}
private static string GenerateCreateTableScript(DbContext sourceContext, string tableName)
{
var entityType = sourceContext.Model.GetEntityTypes().FirstOrDefault(e => e.GetTableName() == tableName);
if (entityType == null)
return null; // or throw exception
// Start building the SQL script
var sqlBuilder = new StringBuilder($"CREATE TABLE {tableName} (");
foreach (var prop in entityType.GetProperties())
{
sqlBuilder.Append($"{prop.Name} {prop.GetColumnType()}, ");
}
// Remove trailing comma and space
sqlBuilder.Remove(sqlBuilder.Length - 2, 2);
// Add primary key if exists
var primaryKey = entityType.FindPrimaryKey();
if (primaryKey != null)
{
var primaryKeyColumns = string.Join(", ", primaryKey.Properties.Select(p => p.Name));
sqlBuilder.Append($", PRIMARY KEY ({primaryKeyColumns})");
}
sqlBuilder.Append(");");
return sqlBuilder.ToString();
}
private static string GenerateAddColumnScript(string tableName, string columnName, string columnType)
{
return $"ALTER TABLE {tableName} ADD {columnName} {columnType};";
}
public static string GenerateAddPrimaryKeyScript(string tableName, string primaryKeyColumn)
{
string constraintName = $"PK_{tableName}";
return $"ALTER TABLE {tableName} ADD CONSTRAINT {constraintName} PRIMARY KEY ({primaryKeyColumn});";
}
public static string GenerateAddDefaultValueScript(string tableName, string columnName, object defaultValue)
{
// Assuming the default value is a string or number; adjust if other data types need special handling.
string constraintName = $"DF_{tableName}_{columnName}";
return $"ALTER TABLE {tableName} ADD CONSTRAINT {constraintName} DEFAULT {defaultValue} FOR {columnName};";
}
}
}