-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathDataModel_Compare.cs
342 lines (292 loc) · 14.2 KB
/
DataModel_Compare.cs
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
using ModelSync.Library.Extensions;
using System.Collections.Generic;
using System.Linq;
namespace ModelSync.Models
{
public partial class DataModel
{
public static IEnumerable<ScriptAction> Compare(DataModel sourceModel, DataModel destModel)
{
List<ScriptAction> results = new List<ScriptAction>();
results.AddRange(CreateSchemas(sourceModel, destModel));
var createTables = CreateTables(sourceModel, destModel);
results.AddRange(createTables);
// nested object creates need to omit tables already created
results.AddRange(AddColumns(sourceModel, destModel, createTables));
results.AddRange(AddChecks(sourceModel, destModel, createTables));
results.AddRange(AddIndexes(sourceModel, destModel, createTables));
results.AddRange(AlterIndexes(sourceModel, destModel));
results.AddRange(AlterChecks(sourceModel, destModel));
results.AddRange(AlterColumns(sourceModel, destModel));
results.AddRange(CreateForeignKeys(sourceModel, destModel));
results.AddRange(AlterForeignKeys(sourceModel, destModel));
var dropTables = DropTables(sourceModel, destModel);
results.AddRange(dropTables);
// nested object drops need to exclude tables that were already dropped
var dropIndexes = DropIndexes(sourceModel, destModel, dropTables);
results.AddRange(dropIndexes);
results.AddRange(DropForeignKeys(sourceModel, destModel, dropTables));
// column drop should also not drop parent indexes if they've already been dropped
results.AddRange(DropColumns(sourceModel, destModel, dropTables, dropIndexes));
results.AddRange(DropChecks(sourceModel, destModel, dropTables));
return results;
}
private static IEnumerable<ScriptAction> AlterForeignKeys(DataModel sourceModel, DataModel destModel)
{
var alteredKFs = from src in sourceModel.ForeignKeys
join dest in destModel.ForeignKeys on src equals dest
where (src.IsAltered(dest, out _))
select new
{
@object = src,
comment = src.GetAlterComment(dest)
};
return alteredKFs.Select(fk => new ScriptAction()
{
Type = ActionType.Alter,
Object = fk.@object,
Commands = [email protected](destModel, fk.comment)
});
}
private static ScriptAction[] CreateSchemas(DataModel sourceModel, DataModel destModel)
{
return sourceModel.Schemas.Except(destModel.Schemas).Select(sch => new ScriptAction()
{
Type = ActionType.Create,
Object = sch,
Commands = sch.CreateStatements()
}).ToArray();
}
private static ScriptAction[] CreateTables(DataModel sourceModel, DataModel destModel)
{
return sourceModel.Tables.Except(destModel.Tables).Select(tbl => new ScriptAction()
{
Type = ActionType.Create,
Object = tbl,
Commands = tbl.CreateStatements()
}).ToArray();
}
private static ScriptAction[] AddColumns(DataModel sourceModel, DataModel destModel, ScriptAction[] exceptCreatedTables)
{
var exceptTables = exceptCreatedTables.Select(scr => scr.Object).OfType<Table>();
var srcColumns = sourceModel.Tables.Except(exceptTables).SelectMany(tbl => tbl.Columns);
var destColumns = destModel.Tables.SelectMany(tbl => tbl.Columns);
return srcColumns.Except(destColumns).Select(col =>
{
var destTable = destModel.TableDictionary[col.Parent.Name];
var srcTable = sourceModel.TableDictionary[col.Parent.Name];
col.DefaultValueRequired = destTable.RowCount > 0 && !col.IsNullable && !srcTable.IsIdentityColumn(col.Name, "Id");
return new ScriptAction()
{
Type = ActionType.Create,
Object = col,
Commands = col.CreateStatements()
};
}).ToArray();
}
private static ScriptAction[] DropTables(DataModel sourceModel, DataModel destModel)
{
var results = destModel.Tables.Except(sourceModel.Tables).Select(tbl =>
new ScriptAction()
{
Type = ActionType.Drop,
Object = tbl,
Commands = tbl.DropStatements(destModel)
}).ToArray();
var ordered = results.ToDependencyOrder(dropTable =>
{
// all tables I (the table being dropped) reference
var parentTables = (dropTable.Object as Table)
.GetReferencedTables(destModel)
.ToArray();
// filtered to this particular diff
var droppedTables = parentTables
.Where(tbl => results.Any(sa => sa.Object.Equals(tbl)))
.ToArray();
// give me the result as list of script actions because this is the T
// expected by ToDependencyOrder<T>
return droppedTables.Select(tbl => new ScriptAction()
{
Type = ActionType.Drop,
Object = tbl,
Commands = tbl.DropStatements(destModel)
});
});
return ordered.ToArray();
}
private static ScriptAction[] DropColumns(
DataModel sourceModel, DataModel destModel,
ScriptAction[] exceptDroppedTables, ScriptAction[] exceptDroppedIndexes)
{
var exceptTables = exceptDroppedTables.Select(scr => scr.Object).OfType<Table>();
var sourceColumns = sourceModel.Tables.SelectMany(tbl => tbl.Columns).ToArray();
var destColumns = destModel.Tables.Except(exceptTables).SelectMany(tbl => tbl.Columns).ToArray();
var results =
destColumns.Except(sourceColumns)
.Select(col =>
{
// don't re-drop indexes you've already dropped
var commands = col.DropStatements(destModel).Except(exceptDroppedIndexes.SelectMany(scr => scr.Commands)).ToArray();
var result = new ScriptAction()
{
Type = ActionType.Drop,
Object = col,
Commands = commands
};
return result;
}).ToArray();
return results;
}
private static ScriptAction[] CreateForeignKeys(DataModel sourceModel, DataModel destModel)
{
return sourceModel.ForeignKeys.Except(destModel.ForeignKeys).Select(fk => new ScriptAction()
{
Type = ActionType.Create,
Object = fk,
Commands = fk.CreateStatements()
}).ToArray();
}
private static ScriptAction[] DropForeignKeys(DataModel sourceModel, DataModel destModel, ScriptAction[] exceptDroppedTables)
{
var droppedTables = exceptDroppedTables.Select(scr => scr.Object).OfType<Table>();
var alreadyDroppedFKs = destModel.ForeignKeys.Where(fk => droppedTables.Contains(fk.Parent));
var deletableFKs = destModel.ForeignKeys.Except(alreadyDroppedFKs).Except(sourceModel.ForeignKeys).ToArray();
return deletableFKs.Select(fk => new ScriptAction()
{
Type = ActionType.Drop,
Object = fk,
Commands = fk.DropStatements(destModel)
}).ToArray();
}
private static ScriptAction[] DropIndexes(DataModel sourceModel, DataModel destModel, ScriptAction[] exceptDroppedTables)
{
var droppedTables = exceptDroppedTables.Select(scr => scr.Object).OfType<Table>();
var alreadyDroppedIndexes = droppedTables.SelectMany(tbl => tbl.Indexes);
var srcIndexes = sourceModel.Tables.SelectMany(tbl => tbl.Indexes).ToArray();
return destModel.Tables
.SelectMany(tbl => tbl.Indexes)
.Except(alreadyDroppedIndexes)
.Except(srcIndexes)
.Select(ndx => new ScriptAction()
{
Type = ActionType.Drop,
Object = ndx,
Commands = ndx.DropStatements(destModel)
}).ToArray();
}
private static ScriptAction[] AddIndexes(DataModel sourceModel, DataModel destModel, ScriptAction[] exceptCreatedTables)
{
var exceptTables = exceptCreatedTables.Select(scr => scr.Object).OfType<Table>();
return sourceModel.Tables
.Except(exceptTables)
.SelectMany(tbl => tbl.Indexes)
.Except(destModel.Tables.SelectMany(tbl => tbl.Indexes))
.Select(ndx => new ScriptAction()
{
Type = ActionType.Create,
Object = ndx,
Commands = ndx.CreateStatements()
}).ToArray();
}
private static ScriptAction[] AlterColumns(DataModel sourceModel, DataModel destModel)
{
var allColumns = from src in sourceModel.Tables.SelectMany(tbl => tbl.Columns)
join dest in destModel.Tables.SelectMany(tbl => tbl.Columns) on src equals dest
select new
{
Source = src,
Dest = dest
};
return allColumns
.Where(columnPair => columnPair.Source.IsAltered(columnPair.Dest, out _))
.Select(columnPair =>
{
columnPair.Source.IsAltered(columnPair.Dest, out string comment);
return new ScriptAction()
{
Type = ActionType.Alter,
Object = columnPair.Source,
Commands = columnPair.Source.AlterStatements(comment, destModel)
};
}).ToArray();
}
private static ScriptAction[] AlterIndexes(DataModel sourceModel, DataModel destModel)
{
var allIndexes = from src in sourceModel.Tables.SelectMany(tbl => tbl.Indexes)
join dest in destModel.Tables.SelectMany(tbl => tbl.Indexes) on src equals dest
select new
{
Source = src,
Dest = dest
};
return allIndexes
.Where(indexPair => indexPair.Source.IsAltered(indexPair.Dest, out _))
.Select(indexPair =>
{
indexPair.Source.IsAltered(indexPair.Dest, out string comment);
return new ScriptAction()
{
Type = ActionType.Alter,
Object = indexPair.Source,
Commands = indexPair.Source.RebuildStatements(destModel, comment)
};
}).ToArray();
}
private static IEnumerable<ScriptAction> AddChecks(DataModel sourceModel, DataModel destModel, ScriptAction[] exceptCreatedTables)
{
var exceptTables = exceptCreatedTables.Select(scr => scr.Object).OfType<Table>();
var srcChecks = sourceModel.Tables.Except(exceptTables).SelectMany(tbl => tbl.CheckConstraints);
var destChecks = destModel.Tables.SelectMany(tbl => tbl.CheckConstraints);
return srcChecks.Except(destChecks).Select(chk =>
{
return new ScriptAction()
{
Type = ActionType.Create,
Object = chk,
Commands = chk.CreateStatements()
};
}).ToArray();
}
private static IEnumerable<ScriptAction> AlterChecks(DataModel sourceModel, DataModel destModel)
{
var allChecks = from src in sourceModel.Tables.SelectMany(tbl => tbl.CheckConstraints)
join dest in destModel.Tables.SelectMany(tbl => tbl.CheckConstraints) on src equals dest
select new
{
Source = src,
Dest = dest
};
return allChecks
.Where(chkPair => chkPair.Source.IsAltered(chkPair.Dest, out _))
.Select(chkPair =>
{
chkPair.Source.IsAltered(chkPair.Dest, out string comment);
return new ScriptAction()
{
Type = ActionType.Alter,
Object = chkPair.Source,
Commands = chkPair.Source.RebuildStatements(destModel, comment)
};
}).ToArray();
}
private static IEnumerable<ScriptAction> DropChecks(DataModel sourceModel, DataModel destModel, ScriptAction[] exceptDroppedTables)
{
var exceptTables = exceptDroppedTables.Select(scr => scr.Object).OfType<Table>();
var sourceChecks = sourceModel.Tables.SelectMany(tbl => tbl.CheckConstraints).ToArray();
var destChecks = destModel.Tables.Except(exceptTables).SelectMany(tbl => tbl.CheckConstraints).ToArray();
var results =
destChecks.Except(sourceChecks)
.Select(chk =>
{
var result = new ScriptAction()
{
Type = ActionType.Drop,
Object = chk,
Commands = chk.DropStatements(destModel)
};
return result;
}).ToArray();
return results;
}
}
}