-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathfrmMigrationBuilder.cs
264 lines (229 loc) · 9.81 KB
/
frmMigrationBuilder.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
using SqlIntegration.Library;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Forms;
using WinForms.Library;
using WinForms.Library.Extensions.ComboBoxes;
using Zinger.Interfaces;
using Zinger.Models;
using Zinger.Services;
namespace Zinger.Forms
{
public partial class frmMigrationBuilder : Form, ISaveable
{
JsonSDI<DataMigration> _doc = new JsonSDI<DataMigration>(".json", "Json Files|*.json", "Save changes?");
private DataMigrator _migrator;
private DataMigrator.MigrationResult _migrationResult;
public frmMigrationBuilder()
{
InitializeComponent();
dgvSteps.AutoGenerateColumns = false;
dgvParams.AutoGenerateColumns = false;
}
public SavedConnections SavedConnections { get; set; }
public string Filename => _doc.Filename;
public string DefaultExtension => ".json";
private void frmMigrationBuilder_Load(object sender, EventArgs e)
{
_migrator = new DataMigrator(SavedConnections);
_migrator.Progress += ShowProgress;
_migrator.ConsoleMessage += delegate (object sender2, string message)
{
consoleTextBox1.Insert(message);
};
InitBinding();
}
private void ShowProgress(object sender, SqlMigrator<int>.Progress e)
{
pbMain.Value = e.PercentComplete;
tslProgress.Text = $"{e.TotalRows:n0} total rows, {e.RowsMigrated:n0} migrated ({e.PercentComplete}%), {e.RowsSkipped:n0} skipped";
}
private void InitBinding()
{
_doc.Document = new DataMigration();
_doc.SavingFile += delegate (object sender, EventArgs args)
{
_doc.Document.Steps = ((dgvSteps.DataSource as BindingSource).DataSource as BindingList<DataMigration.Step>).ToArray();
_doc.Document.Parameters = ((dgvParams.DataSource as BindingSource).DataSource as BindingList<DataMigration.Parameter>).ToArray();
};
_doc.FileOpened += delegate (object sender, EventArgs args)
{
_migrator.CurrentFilename = _doc.Filename;
InitStepsDataGridView();
InitParamsDataGridView();
};
_doc.Controls.AddItems(cbSourceConnection,
setProperty: (dm) => dm.SourceConnection = (cbSourceConnection.SelectedItem as SavedConnection).Name,
setControl: (dm) => cbSourceConnection.Set<SavedConnection>(item => item.Name.Equals(dm.SourceConnection)),
SavedConnections.Connections.OrderBy(item => item.Name));
_doc.Controls.AddItems(cbDestConnection,
setProperty: (dm) => dm.DestConnection = (cbDestConnection.SelectedItem as SavedConnection).Name,
setControl: (dm) => cbDestConnection.Set<SavedConnection>(item => item.Name.Equals(dm.DestConnection)),
SavedConnections.Connections.OrderBy(item => item.Name));
void InitStepsDataGridView()
{
BindingSource bsSteps = new BindingSource();
bsSteps.DataSource = new BindingList<DataMigration.Step>((_doc.Document?.Steps.OrderBy(row => row.Order) ?? Enumerable.Empty<DataMigration.Step>()).ToList());
dgvSteps.DataSource = bsSteps;
bsSteps.CurrentItemChanged += delegate (object sender, EventArgs args)
{
pbValidation.Image = null;
lblStepResult.Text = null;
var step = bsSteps.Current as DataMigration.Step;
if (step != null && step.Columns == null) step.Columns = new List<DataMigration.Column>();
propertyGrid1.SelectedObject = null;
};
migrationStep1.InitBinding(bsSteps);
}
void InitParamsDataGridView()
{
BindingSource bsParams = new BindingSource();
var data = _doc.Document?.Parameters?.ToList() ?? new List<DataMigration.Parameter>();
bsParams.DataSource = new BindingList<DataMigration.Parameter>(data);
dgvParams.DataSource = bsParams;
}
}
private async void btnSave_Click(object sender, EventArgs e)
{
await _doc.SaveAsync();
}
private async void btnOpen_Click(object sender, EventArgs e)
{
await _doc.PromptOpenAsync();
}
private async void toolStripButton1_Click(object sender, EventArgs e)
{
await _doc.NewAsync();
}
private async void btnBuildColumns_Click(object sender, EventArgs e)
{
try
{
await _migrator.AddColumnsAsync(_doc.Filename, overwrite: true, _doc.Document.GetParameters());
}
catch (Exception exc)
{
MessageBox.Show(exc.Message);
}
}
private async void frmMigrationBuilder_FormClosing(object sender, FormClosingEventArgs e)
{
await _doc.SaveAsync();
await _doc.FormClosingAsync(e);
}
private async void btnValidateStep_Click(object sender, EventArgs e)
{
await RunMigrationTaskAsync(async (step, migration) => await _migrator.ValidateStepAsync(step, migration));
}
private async void btnRun_Click(object sender, EventArgs e)
{
await RunMigrationTaskAsync(async (step, migration) => await _migrator.RunStepAsync(step, migration));
}
private async Task RunMigrationTaskAsync(Func<DataMigration.Step, DataMigration, Task<DataMigrator.MigrationResult>> func)
{
try
{
consoleTextBox1.Clear();
tslProgress.Text = "Querying...";
tslCancel.Visible = true;
pbMain.Visible = true;
var step = (dgvSteps.DataSource as BindingSource).Current as DataMigration.Step;
pbValidation.Image = imageList1.Images["loading"];
_migrationResult = await func.Invoke(step, _doc.Document);
pbValidation.Image = (_migrationResult.Success) ? imageList1.Images["success"] : imageList1.Images["fail"];
lblStepResult.Text = $"{_migrationResult.Message} {_migrationResult.RowsCopied:n0} rows {_migrationResult.Action}";
}
catch (Exception exc)
{
pbValidation.Image = imageList1.Images["fail"];
lblStepResult.Text = exc.Message;
}
finally
{
llSourceSql.Enabled = !string.IsNullOrEmpty(_migrationResult?.SourceSql);
llInsertSql.Enabled = !string.IsNullOrEmpty(_migrationResult?.InsertSql);
pbMain.Visible = false;
tslProgress.Text = "Ready";
tslCancel.Visible = false;
}
}
private void SaveToClipboard(Func<string> getText)
{
try
{
var text = getText.Invoke();
Clipboard.SetText(text);
}
catch (Exception exc)
{
MessageBox.Show(exc.Message);
}
}
private void llSourceSql_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
{
SaveToClipboard(() => _migrationResult.SourceSql);
}
private void llInsertSql_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
{
SaveToClipboard(() => _migrationResult.InsertSql);
}
private async void btnAddStepColumns_Click(object sender, EventArgs e)
{
try
{
var step = (dgvSteps.DataSource as BindingSource).Current as DataMigration.Step;
await _migrator.AddStepColumnsAsync(_doc.Document, step);
}
catch (Exception exc)
{
MessageBox.Show(exc.Message);
}
}
private async void btnImportKeyMap_Click(object sender, EventArgs e)
{
try
{
var result = await _migrator.ImportKeyMapTableAsync(_doc.Document);
MessageBox.Show($"Imported table {result.@object} with {result.rowCount:n0} rows.");
}
catch (Exception exc)
{
MessageBox.Show(exc.Message);
}
}
public async Task SaveAsync(string fileName)
{
_doc.Filename = fileName;
await _doc.SaveAsync();
}
private void tslCancel_Click(object sender, EventArgs e)
{
_migrator?.Cancel();
}
private async void btnRefreshProgress_Click(object sender, EventArgs e)
{
try
{
var step = (dgvSteps.DataSource as BindingSource).Current as DataMigration.Step;
var progress = await _migrator.QueryMappingProgressAsync(_doc.Document, step);
propertyGrid1.SelectedObject = progress;
}
catch (Exception exc)
{
MessageBox.Show(exc.Message);
}
}
private void llImportKeyMap_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e) => btnImportKeyMap_Click(sender, new EventArgs());
private void linkLabel1_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
{
SaveToClipboard(() =>
{
var step = (dgvSteps.DataSource as BindingSource).Current as DataMigration.Step;
return _migrator.GetUnmappedRowsQuery(step);
});
}
}
}