Merge lp:~adam-rpconnelly/dbversion/saved-connection into lp:dbversion
- saved-connection
- Merge into trunk
Proposed by
Adam Connelly
Status: | Merged | ||||
---|---|---|---|---|---|
Merged at revision: | 43 | ||||
Proposed branch: | lp:~adam-rpconnelly/dbversion/saved-connection | ||||
Merge into: | lp:dbversion | ||||
Diff against target: |
2759 lines (+2008/-381) 21 files modified
src/DatabaseVersion.Console.Tests/Command/Create/CreateCommandTests.cs (+7/-6) src/DatabaseVersion.Console.Tests/Command/History/HistoryCommandTests.cs (+6/-0) src/DatabaseVersion.Console.Tests/Command/SavedConnection/SavedConnectionCommandTests.cs (+546/-0) src/DatabaseVersion.Console.Tests/dbversion.Console.Tests.csproj (+1/-0) src/DatabaseVersion.Console/Arguments.cs (+0/-59) src/DatabaseVersion.Console/Command/ConnectionArguments.cs (+46/-0) src/DatabaseVersion.Console/Command/ConnectionCommandBase.cs (+233/-0) src/DatabaseVersion.Console/Command/Create/CreateArguments.cs (+17/-0) src/DatabaseVersion.Console/Command/Create/CreateCommand.cs (+9/-167) src/DatabaseVersion.Console/Command/History/HistoryArguments.cs (+1/-25) src/DatabaseVersion.Console/Command/History/HistoryCommand.cs (+8/-114) src/DatabaseVersion.Console/Command/SavedConnection/SavedConnectionArguments.cs (+52/-0) src/DatabaseVersion.Console/Command/SavedConnection/SavedConnectionCommand.cs (+326/-0) src/DatabaseVersion.Console/dbversion.Console.csproj (+13/-8) src/DatabaseVersion.Tests/Connections/SavedConnectionServiceTests.cs (+313/-0) src/DatabaseVersion.sln (+1/-1) src/DatabaseVersion/Connections/ISavedConnectionService.cs (+92/-0) src/DatabaseVersion/Connections/SavedConnection.cs (+150/-0) src/DatabaseVersion/Connections/SavedConnectionService.cs (+171/-0) src/DatabaseVersion/Settings/SettingsService.cs (+13/-1) src/DatabaseVersion/dbversion.csproj (+3/-0) |
||||
To merge this branch: | bzr merge lp:~adam-rpconnelly/dbversion/saved-connection | ||||
Related bugs: |
|
Reviewer | Review Type | Date Requested | Status |
---|---|---|---|
dbversion committers | Pending | ||
Review via email: mp+82325@code.launchpad.net |
Commit message
Description of the change
I've added the saved-connection command. The command allows multiple connections to be saved with a name. This means that when you want to specify a database to connect to you just have to specify the connection name instead of typing out the full connection details.
The command also allows existing saved connections to be used as a template when creating new ones so that only the connection properties that have changed need to be specified, and allows a default connection to be specified which will be used by default if no connection name is specified when running commands.
To post a comment you must log in.
Preview Diff
[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1 | === modified file 'src/DatabaseVersion.Console.Tests/Command/Create/CreateCommandTests.cs' |
2 | --- src/DatabaseVersion.Console.Tests/Command/Create/CreateCommandTests.cs 2011-09-05 19:50:57 +0000 |
3 | +++ src/DatabaseVersion.Console.Tests/Command/Create/CreateCommandTests.cs 2011-11-15 22:04:37 +0000 |
4 | @@ -4,6 +4,7 @@ |
5 | |
6 | using dbversion; |
7 | using dbversion.Archives; |
8 | + using dbversion.Connections; |
9 | using dbversion.Console.Command.Create; |
10 | using dbversion.Property; |
11 | using dbversion.Session; |
12 | @@ -25,6 +26,7 @@ |
13 | private readonly Mock<IDatabaseArchive> archive = new Mock<IDatabaseArchive>(); |
14 | private readonly Mock<IDatabaseArchiveFactory> archiveFactory = new Mock<IDatabaseArchiveFactory>(); |
15 | private readonly Mock<ISettingsService> settingsService = new Mock<ISettingsService>(); |
16 | + private readonly Mock<ISavedConnectionService> savedConnectionService = new Mock<ISavedConnectionService>(); |
17 | |
18 | #endregion |
19 | |
20 | @@ -58,8 +60,7 @@ |
21 | public void ShouldPrintMessageIfArchiveIsNotSpecified() |
22 | { |
23 | // Arrange |
24 | - var command = new CreateCommand(); |
25 | - command.MessageService = messageService.Object; |
26 | + var command = this.CreateCommand(); |
27 | |
28 | // Act |
29 | command.Execute(new string[] { }); |
30 | @@ -85,10 +86,9 @@ |
31 | public void ShouldPrintMessageIfArchiveIsNotSupported() |
32 | { |
33 | // Arrange |
34 | - var command = new CreateCommand(); |
35 | - command.MessageService = messageService.Object; |
36 | - var archiveFactory = new Mock<IDatabaseArchiveFactory>(); |
37 | - command.ArchiveFactories = new[] { archiveFactory.Object }; |
38 | + var command = this.CreateCommand(); |
39 | + command.ArchiveFactories = new[] { this.archiveFactory.Object }; |
40 | + this.archiveFactory.Setup(f => f.Create(It.IsAny<string>())).Returns((IDatabaseArchive)null); |
41 | |
42 | // Act |
43 | command.Execute(new[] { "-a", "myArchive" }); |
44 | @@ -324,6 +324,7 @@ |
45 | command.SettingsService = this.settingsService.Object; |
46 | command.PropertyService = this.propertyService.Object; |
47 | command.MessageService = this.messageService.Object; |
48 | + command.SavedConnectionService = this.savedConnectionService.Object; |
49 | |
50 | archiveFactory.Setup(f => f.CanCreate("myArchive")).Returns(true); |
51 | archiveFactory.Setup(f => f.Create("myArchive")).Returns(archive.Object); |
52 | |
53 | === modified file 'src/DatabaseVersion.Console.Tests/Command/History/HistoryCommandTests.cs' |
54 | --- src/DatabaseVersion.Console.Tests/Command/History/HistoryCommandTests.cs 2011-10-17 20:07:45 +0000 |
55 | +++ src/DatabaseVersion.Console.Tests/Command/History/HistoryCommandTests.cs 2011-11-15 22:04:37 +0000 |
56 | @@ -6,6 +6,8 @@ |
57 | using System.Linq; |
58 | using System.Text; |
59 | |
60 | + using dbversion.Archives; |
61 | + using dbversion.Connections; |
62 | using dbversion.Console.Command.History; |
63 | using dbversion.Property; |
64 | using dbversion.Session; |
65 | @@ -27,6 +29,8 @@ |
66 | private readonly Mock<ISessionFactoryProvider> sessionFactoryProvider = new Mock<ISessionFactoryProvider>() { DefaultValue = DefaultValue.Mock }; |
67 | private readonly Mock<ISettingsService> settingsService = new Mock<ISettingsService>(); |
68 | private readonly Mock<IPropertyService> propertyService = new Mock<IPropertyService>(); |
69 | + private readonly Mock<IDatabaseArchiveFactory> archiveFactory = new Mock<IDatabaseArchiveFactory>(); |
70 | + private readonly Mock<ISavedConnectionService> savedConnectionService = new Mock<ISavedConnectionService>(); |
71 | |
72 | public HistoryCommandTests() |
73 | { |
74 | @@ -248,6 +252,8 @@ |
75 | command.SessionFactoryProvider = this.sessionFactoryProvider.Object; |
76 | command.SettingsService = this.settingsService.Object; |
77 | command.PropertyService = this.propertyService.Object; |
78 | + command.ArchiveFactories = new[] { this.archiveFactory.Object }; |
79 | + command.SavedConnectionService = this.savedConnectionService.Object; |
80 | |
81 | return command; |
82 | } |
83 | |
84 | === added directory 'src/DatabaseVersion.Console.Tests/Command/SavedConnection' |
85 | === added file 'src/DatabaseVersion.Console.Tests/Command/SavedConnection/SavedConnectionCommandTests.cs' |
86 | --- src/DatabaseVersion.Console.Tests/Command/SavedConnection/SavedConnectionCommandTests.cs 1970-01-01 00:00:00 +0000 |
87 | +++ src/DatabaseVersion.Console.Tests/Command/SavedConnection/SavedConnectionCommandTests.cs 2011-11-15 22:04:37 +0000 |
88 | @@ -0,0 +1,546 @@ |
89 | +namespace dbversion.Console.Tests.Command.SavedConnection |
90 | +{ |
91 | + using System; |
92 | + using System.Collections.Generic; |
93 | + |
94 | + using dbversion.Connections; |
95 | + |
96 | + using dbversion.Console.Command.SavedConnection; |
97 | + |
98 | + using Moq; |
99 | + |
100 | + using Xunit; |
101 | + |
102 | + public class SavedConnectionCommandTests |
103 | + { |
104 | + [Fact] |
105 | + public void ShouldHaveCorrectCommandName() |
106 | + { |
107 | + // Arrange |
108 | + var command = new SavedConnectionCommand(); |
109 | + |
110 | + // Act |
111 | + var name = command.Name; |
112 | + |
113 | + // Assert |
114 | + Assert.Equal("saved-connection", name); |
115 | + } |
116 | + |
117 | + [Fact] |
118 | + public void ShouldHaveCorrectCommandDescription() |
119 | + { |
120 | + // Arrange |
121 | + var command = new SavedConnectionCommand(); |
122 | + |
123 | + // Act |
124 | + var description = command.Description; |
125 | + |
126 | + // Assert |
127 | + Assert.Equal("Allows connection details to be saved and managed.", description); |
128 | + } |
129 | + |
130 | + [Fact] |
131 | + public void ShouldListAllConnectionsWhenNoArgumentsAreSpecified() |
132 | + { |
133 | + // Arrange |
134 | + var command = new SavedConnectionCommand(); |
135 | + var service = new Mock<ISavedConnectionService>(); |
136 | + var messageService = new MessageServiceMock(); |
137 | + |
138 | + command.SavedConnectionService = service.Object; |
139 | + command.MessageService = messageService; |
140 | + |
141 | + service.Setup(s => s.SavedConnections).Returns(CreateConnections()); |
142 | + |
143 | + // Act |
144 | + command.Execute(new[] { "saved-connection" }); |
145 | + |
146 | + // Assert |
147 | + var expectedMessage = "connection2:" + Environment.NewLine + |
148 | + " Connection String: connString2" + Environment.NewLine + |
149 | + " Driver Class: driver2" + Environment.NewLine + |
150 | + " Provider: provider2" + Environment.NewLine + |
151 | + " Dialect: dialect2" + Environment.NewLine + |
152 | + " Default: True" + Environment.NewLine + Environment.NewLine + |
153 | + "connection1:" + Environment.NewLine + |
154 | + " Connection String: connString" + Environment.NewLine + |
155 | + " Driver Class: driver" + Environment.NewLine + |
156 | + " Provider: provider" + Environment.NewLine + |
157 | + " Dialect: dialect" + Environment.NewLine + |
158 | + " Default: False" + Environment.NewLine + Environment.NewLine; |
159 | + Assert.Equal(expectedMessage, messageService.Contents); |
160 | + } |
161 | + |
162 | + [Fact] |
163 | + public void ShouldDisplayMessageWhenNoSavedConnectionsExist() |
164 | + { |
165 | + // Arrange |
166 | + var command = new SavedConnectionCommand(); |
167 | + var service = new Mock<ISavedConnectionService>(); |
168 | + var messageService = new MessageServiceMock(); |
169 | + |
170 | + command.SavedConnectionService = service.Object; |
171 | + command.MessageService = messageService; |
172 | + |
173 | + // Act |
174 | + command.Execute(new[] { "saved-connection" }); |
175 | + |
176 | + // Assert |
177 | + var expectedMessage = "There are no saved connections." + Environment.NewLine; |
178 | + Assert.Equal(expectedMessage, messageService.Contents); |
179 | + } |
180 | + |
181 | + [Fact] |
182 | + public void ShouldListCorrectConnectionWhenConnectionNameIsSpecified() |
183 | + { |
184 | + // Arrange |
185 | + var command = new SavedConnectionCommand(); |
186 | + var service = new Mock<ISavedConnectionService>(); |
187 | + var messageService = new MessageServiceMock(); |
188 | + |
189 | + command.SavedConnectionService = service.Object; |
190 | + command.MessageService = messageService; |
191 | + |
192 | + service.Setup(s => s.SavedConnections).Returns(CreateConnections()); |
193 | + |
194 | + // Act |
195 | + command.Execute(new[] { "saved-connection", "-n", "connection1" }); |
196 | + |
197 | + // Assert |
198 | + var expectedMessage = "connection1:" + Environment.NewLine + |
199 | + " Connection String: connString" + Environment.NewLine + |
200 | + " Driver Class: driver" + Environment.NewLine + |
201 | + " Provider: provider" + Environment.NewLine + |
202 | + " Dialect: dialect" + Environment.NewLine + |
203 | + " Default: False" + Environment.NewLine + Environment.NewLine; |
204 | + Assert.Equal(expectedMessage, messageService.Contents); |
205 | + } |
206 | + |
207 | + [Fact] |
208 | + public void ShouldDisplayMessageWhenSpecifiedConnectionDoesNotExist() |
209 | + { |
210 | + // Arrange |
211 | + var command = new SavedConnectionCommand(); |
212 | + var service = new Mock<ISavedConnectionService>(); |
213 | + var messageService = new MessageServiceMock(); |
214 | + |
215 | + command.SavedConnectionService = service.Object; |
216 | + command.MessageService = messageService; |
217 | + |
218 | + service.Setup(s => s.SavedConnections).Returns(CreateConnections()); |
219 | + |
220 | + // Act |
221 | + command.Execute(new[] { "saved-connection", "-n", "abc" }); |
222 | + |
223 | + // Assert |
224 | + var expectedMessage = "There is no saved connection called \"abc\"." + Environment.NewLine; |
225 | + Assert.Equal(expectedMessage, messageService.Contents); |
226 | + } |
227 | + |
228 | + [Fact] |
229 | + public void ShouldBeAbleToCreateNewSavedConnection() |
230 | + { |
231 | + // Arrange |
232 | + var command = new SavedConnectionCommand(); |
233 | + var service = new Mock<ISavedConnectionService>(); |
234 | + var messageService = new MessageServiceMock(); |
235 | + |
236 | + command.SavedConnectionService = service.Object; |
237 | + command.MessageService = messageService; |
238 | + |
239 | + service.Setup(s => s.CreateSavedConnection("connection1", "connString", "provider", "driver", "dialect")) |
240 | + .Returns(new SavedConnection("connection1", "connString", "provider", "driver", "dialect", true)); |
241 | + |
242 | + // Act |
243 | + command.Execute( |
244 | + new[] |
245 | + { |
246 | + "saved-connection", |
247 | + "-n", "connection1", |
248 | + "-c", "connString", |
249 | + "-l", "dialect", |
250 | + "-p", "provider", |
251 | + "-d", "driver" |
252 | + }); |
253 | + |
254 | + // Assert |
255 | + var expectedMessage = "Created new saved connection \"connection1\":" + Environment.NewLine + |
256 | + " Connection String: connString" + Environment.NewLine + |
257 | + " Driver Class: driver" + Environment.NewLine + |
258 | + " Provider: provider" + Environment.NewLine + |
259 | + " Dialect: dialect" + Environment.NewLine + |
260 | + " Default: True" + Environment.NewLine + Environment.NewLine; |
261 | + Assert.Equal(expectedMessage, messageService.Contents); |
262 | + service.Verify(s => s.CreateSavedConnection("connection1", "connString", "provider", "driver", "dialect")); |
263 | + service.Verify(s => s.SaveConnections()); |
264 | + } |
265 | + |
266 | + [Fact] |
267 | + public void ShouldBeAbleToUpdateExistingSavedConnection() |
268 | + { |
269 | + // Arrange |
270 | + var command = new SavedConnectionCommand(); |
271 | + var service = new Mock<ISavedConnectionService>(); |
272 | + var messageService = new MessageServiceMock(); |
273 | + |
274 | + command.SavedConnectionService = service.Object; |
275 | + command.MessageService = messageService; |
276 | + |
277 | + service.Setup(s => s.SavedConnections).Returns( |
278 | + new[] { new SavedConnection("connection1", "connString", "provider", "driver", "dialect", true) }); |
279 | + service.Setup(s => s.CreateSavedConnection("connection1", "newConnString", "provider", "driver", "dialect")) |
280 | + .Returns(new SavedConnection("connection1", "newConnString", "provider", "driver", "dialect", true)); |
281 | + |
282 | + // Act |
283 | + command.Execute( |
284 | + new[] |
285 | + { |
286 | + "saved-connection", |
287 | + "-n", "connection1", |
288 | + "-c", "newConnString" |
289 | + }); |
290 | + |
291 | + // Assert |
292 | + service.Verify(s => s.CreateSavedConnection("connection1", "newConnString", "provider", "driver", "dialect")); |
293 | + service.Verify(s => s.SaveConnections()); |
294 | + } |
295 | + |
296 | + [Fact] |
297 | + public void ShouldBeAbleToDeleteSavedConnection() |
298 | + { |
299 | + // Arrange |
300 | + var command = new SavedConnectionCommand(); |
301 | + var service = new Mock<ISavedConnectionService>(); |
302 | + var messageService = new MessageServiceMock(); |
303 | + |
304 | + command.SavedConnectionService = service.Object; |
305 | + command.MessageService = messageService; |
306 | + |
307 | + service.Setup(s => s.SavedConnections).Returns(CreateConnections()); |
308 | + service.Setup(s => s.DeleteConnection("connection1")).Returns(true); |
309 | + |
310 | + // Act |
311 | + command.Execute(new[] { "saved-connection", "-rn", "connection1" }); |
312 | + |
313 | + // Assert |
314 | + var expectedMessage = "\"connection1\" has been deleted." + Environment.NewLine; |
315 | + Assert.Equal(expectedMessage, messageService.Contents); |
316 | + service.Verify(s => s.SaveConnections()); |
317 | + } |
318 | + |
319 | + [Fact] |
320 | + public void ShouldPrintErrorMessageIfDeletingConnectionFails() |
321 | + { |
322 | + // Arrange |
323 | + var command = new SavedConnectionCommand(); |
324 | + var service = new Mock<ISavedConnectionService>(); |
325 | + var messageService = new MessageServiceMock(); |
326 | + |
327 | + command.SavedConnectionService = service.Object; |
328 | + command.MessageService = messageService; |
329 | + |
330 | + service.Setup(s => s.SavedConnections).Returns(CreateConnections()); |
331 | + service.Setup(s => s.DeleteConnection("connection1")).Returns(false); |
332 | + |
333 | + // Act |
334 | + command.Execute(new[] { "saved-connection", "-rn", "connection1" }); |
335 | + |
336 | + // Assert |
337 | + var expectedMessage = "There is no saved connection called \"connection1\"." + Environment.NewLine; |
338 | + Assert.Equal(expectedMessage, messageService.Contents); |
339 | + service.Verify(s => s.SaveConnections(), Times.Never()); |
340 | + } |
341 | + |
342 | + [Fact] |
343 | + public void ShouldBeAbleToDefaultSavedConnection() |
344 | + { |
345 | + // Arrange |
346 | + var command = new SavedConnectionCommand(); |
347 | + var service = new Mock<ISavedConnectionService>(); |
348 | + var messageService = new MessageServiceMock(); |
349 | + |
350 | + command.SavedConnectionService = service.Object; |
351 | + command.MessageService = messageService; |
352 | + |
353 | + service.Setup(s => s.SavedConnections).Returns(CreateConnections()); |
354 | + service.Setup(s => s.SetDefaultConnection("connection1")).Returns(true); |
355 | + |
356 | + // Act |
357 | + command.Execute(new[] { "saved-connection", "-n", "connection1", "--default" }); |
358 | + |
359 | + // Assert |
360 | + var expectedMessage = "\"connection1\" is now the default connection." + Environment.NewLine; |
361 | + Assert.Equal(expectedMessage, messageService.Contents); |
362 | + service.Verify(s => s.SaveConnections()); |
363 | + } |
364 | + |
365 | + [Fact] |
366 | + public void ShouldPrintAnErrorMessageIfDefaultingFails() |
367 | + { |
368 | + // Arrange |
369 | + var command = new SavedConnectionCommand(); |
370 | + var service = new Mock<ISavedConnectionService>(); |
371 | + var messageService = new MessageServiceMock(); |
372 | + |
373 | + command.SavedConnectionService = service.Object; |
374 | + command.MessageService = messageService; |
375 | + |
376 | + service.Setup(s => s.SavedConnections).Returns(CreateConnections()); |
377 | + service.Setup(s => s.SetDefaultConnection("connection5")).Returns(false); |
378 | + |
379 | + // Act |
380 | + command.Execute(new[] { "saved-connection", "-n", "connection5", "--default" }); |
381 | + |
382 | + // Assert |
383 | + var expectedMessage = "There is no saved connection called \"connection5\"." + Environment.NewLine; |
384 | + Assert.Equal(expectedMessage, messageService.Contents); |
385 | + service.Verify(s => s.SaveConnections(), Times.Never()); |
386 | + } |
387 | + |
388 | + [Fact] |
389 | + public void ShouldBeAbleToCreateAConnectionFromATemplate() |
390 | + { |
391 | + // Arrange |
392 | + var command = new SavedConnectionCommand(); |
393 | + var service = new Mock<ISavedConnectionService>(); |
394 | + var messageService = new MessageServiceMock(); |
395 | + |
396 | + command.SavedConnectionService = service.Object; |
397 | + command.MessageService = messageService; |
398 | + |
399 | + service.Setup(s => s.SavedConnections).Returns(CreateConnections()); |
400 | + service.Setup(s => s.CreateSavedConnection("connection5", "newConnString", "newProvider", "newDriver", "newDialect")) |
401 | + .Returns(new SavedConnection("connection5", "newConnString", "newProvider", "newDriver", "newDialect", false)); |
402 | + |
403 | + // Act |
404 | + command.Execute( |
405 | + new[] |
406 | + { |
407 | + "saved-connection", |
408 | + "-n", "connection5", |
409 | + "-t", "connection1", |
410 | + "-c", "newConnString", |
411 | + "-d", "newDriver", |
412 | + "-l", "newDialect", |
413 | + "-p", "newProvider" |
414 | + }); |
415 | + |
416 | + // Assert |
417 | + var expectedMessage = "Created a new connection \"connection5\" based on \"connection1\"." + Environment.NewLine + |
418 | + " Connection String: newConnString" + Environment.NewLine + |
419 | + " Driver Class: newDriver" + Environment.NewLine + |
420 | + " Provider: newProvider" + Environment.NewLine + |
421 | + " Dialect: newDialect" + Environment.NewLine + |
422 | + " Default: False" + Environment.NewLine + Environment.NewLine; |
423 | + Assert.Equal(expectedMessage, messageService.Contents); |
424 | + service.Verify(s => s.SaveConnections()); |
425 | + } |
426 | + |
427 | + [Fact] |
428 | + public void ShouldNotOverwriteConnectionStringWhenCreatingFromTemplate() |
429 | + { |
430 | + // Arrange |
431 | + var command = new SavedConnectionCommand(); |
432 | + var service = new Mock<ISavedConnectionService>(); |
433 | + var messageService = new MessageServiceMock(); |
434 | + |
435 | + command.SavedConnectionService = service.Object; |
436 | + command.MessageService = messageService; |
437 | + |
438 | + service.Setup(s => s.SavedConnections).Returns(CreateConnections()); |
439 | + service.Setup(s => s.CreateSavedConnection("connection5", "connString", "newProvider", "newDriver", "newDialect")) |
440 | + .Returns(new SavedConnection("connection5", "connString", "newProvider", "newDriver", "newDialect", false)); |
441 | + |
442 | + // Act |
443 | + command.Execute( |
444 | + new[] |
445 | + { |
446 | + "saved-connection", |
447 | + "-n", "connection5", |
448 | + "-t", "connection1", |
449 | + "-d", "newDriver", |
450 | + "-l", "newDialect", |
451 | + "-p", "newProvider" |
452 | + }); |
453 | + |
454 | + // Assert |
455 | + var expectedMessage = "Created a new connection \"connection5\" based on \"connection1\"." + Environment.NewLine + |
456 | + " Connection String: connString" + Environment.NewLine + |
457 | + " Driver Class: newDriver" + Environment.NewLine + |
458 | + " Provider: newProvider" + Environment.NewLine + |
459 | + " Dialect: newDialect" + Environment.NewLine + |
460 | + " Default: False" + Environment.NewLine + Environment.NewLine; |
461 | + Assert.Equal(expectedMessage, messageService.Contents); |
462 | + service.Verify(s => s.SaveConnections()); |
463 | + } |
464 | + |
465 | + [Fact] |
466 | + public void ShouldNotOverwriteDriverClassWhenCreatingFromTemplate() |
467 | + { |
468 | + // Arrange |
469 | + var command = new SavedConnectionCommand(); |
470 | + var service = new Mock<ISavedConnectionService>(); |
471 | + var messageService = new MessageServiceMock(); |
472 | + |
473 | + command.SavedConnectionService = service.Object; |
474 | + command.MessageService = messageService; |
475 | + |
476 | + service.Setup(s => s.SavedConnections).Returns(CreateConnections()); |
477 | + service.Setup(s => s.CreateSavedConnection("connection5", "newConnString", "newProvider", "driver", "newDialect")) |
478 | + .Returns(new SavedConnection("connection5", "newConnString", "newProvider", "driver", "newDialect", false)); |
479 | + |
480 | + // Act |
481 | + command.Execute( |
482 | + new[] |
483 | + { |
484 | + "saved-connection", |
485 | + "-n", "connection5", |
486 | + "-t", "connection1", |
487 | + "-c", "newConnString", |
488 | + "-l", "newDialect", |
489 | + "-p", "newProvider" |
490 | + }); |
491 | + |
492 | + // Assert |
493 | + var expectedMessage = "Created a new connection \"connection5\" based on \"connection1\"." + Environment.NewLine + |
494 | + " Connection String: newConnString" + Environment.NewLine + |
495 | + " Driver Class: driver" + Environment.NewLine + |
496 | + " Provider: newProvider" + Environment.NewLine + |
497 | + " Dialect: newDialect" + Environment.NewLine + |
498 | + " Default: False" + Environment.NewLine + Environment.NewLine; |
499 | + Assert.Equal(expectedMessage, messageService.Contents); |
500 | + service.Verify(s => s.SaveConnections()); |
501 | + } |
502 | + |
503 | + [Fact] |
504 | + public void ShouldNotOverwriteDialectWhenCreatingFromTemplate() |
505 | + { |
506 | + // Arrange |
507 | + var command = new SavedConnectionCommand(); |
508 | + var service = new Mock<ISavedConnectionService>(); |
509 | + var messageService = new MessageServiceMock(); |
510 | + |
511 | + command.SavedConnectionService = service.Object; |
512 | + command.MessageService = messageService; |
513 | + |
514 | + service.Setup(s => s.SavedConnections).Returns(CreateConnections()); |
515 | + service.Setup(s => s.CreateSavedConnection("connection5", "newConnString", "newProvider", "newDriver", "dialect")) |
516 | + .Returns(new SavedConnection("connection5", "newConnString", "newProvider", "newDriver", "dialect", false)); |
517 | + |
518 | + // Act |
519 | + command.Execute( |
520 | + new[] |
521 | + { |
522 | + "saved-connection", |
523 | + "-n", "connection5", |
524 | + "-t", "connection1", |
525 | + "-c", "newConnString", |
526 | + "-d", "newDriver", |
527 | + "-p", "newProvider" |
528 | + }); |
529 | + |
530 | + // Assert |
531 | + var expectedMessage = "Created a new connection \"connection5\" based on \"connection1\"." + Environment.NewLine + |
532 | + " Connection String: newConnString" + Environment.NewLine + |
533 | + " Driver Class: newDriver" + Environment.NewLine + |
534 | + " Provider: newProvider" + Environment.NewLine + |
535 | + " Dialect: dialect" + Environment.NewLine + |
536 | + " Default: False" + Environment.NewLine + Environment.NewLine; |
537 | + Assert.Equal(expectedMessage, messageService.Contents); |
538 | + service.Verify(s => s.SaveConnections()); |
539 | + } |
540 | + |
541 | + [Fact] |
542 | + public void ShouldNotOverwriteProviderWhenCreatingFromTemplate() |
543 | + { |
544 | + // Arrange |
545 | + var command = new SavedConnectionCommand(); |
546 | + var service = new Mock<ISavedConnectionService>(); |
547 | + var messageService = new MessageServiceMock(); |
548 | + |
549 | + command.SavedConnectionService = service.Object; |
550 | + command.MessageService = messageService; |
551 | + |
552 | + service.Setup(s => s.SavedConnections).Returns(CreateConnections()); |
553 | + service.Setup(s => s.CreateSavedConnection("connection5", "newConnString", "provider", "newDriver", "newDialect")) |
554 | + .Returns(new SavedConnection("connection5", "newConnString", "provider", "newDriver", "newDialect", false)); |
555 | + |
556 | + // Act |
557 | + command.Execute( |
558 | + new[] |
559 | + { |
560 | + "saved-connection", |
561 | + "-n", "connection5", |
562 | + "-t", "connection1", |
563 | + "-c", "newConnString", |
564 | + "-d", "newDriver", |
565 | + "-l", "newDialect" |
566 | + }); |
567 | + |
568 | + // Assert |
569 | + var expectedMessage = "Created a new connection \"connection5\" based on \"connection1\"." + Environment.NewLine + |
570 | + " Connection String: newConnString" + Environment.NewLine + |
571 | + " Driver Class: newDriver" + Environment.NewLine + |
572 | + " Provider: provider" + Environment.NewLine + |
573 | + " Dialect: newDialect" + Environment.NewLine + |
574 | + " Default: False" + Environment.NewLine + Environment.NewLine; |
575 | + Assert.Equal(expectedMessage, messageService.Contents); |
576 | + service.Verify(s => s.SaveConnections()); |
577 | + } |
578 | + |
579 | + [Fact] |
580 | + public void ShouldPrintErrorIfTemplateCannotBeFound() |
581 | + { |
582 | + // Arrange |
583 | + var command = new SavedConnectionCommand(); |
584 | + var service = new Mock<ISavedConnectionService>(); |
585 | + var messageService = new MessageServiceMock(); |
586 | + |
587 | + command.SavedConnectionService = service.Object; |
588 | + command.MessageService = messageService; |
589 | + |
590 | + service.Setup(s => s.SavedConnections).Returns(CreateConnections()); |
591 | + |
592 | + // Act |
593 | + command.Execute( |
594 | + new[] |
595 | + { |
596 | + "saved-connection", |
597 | + "-n", "connection5", |
598 | + "-t", "missingConnection", |
599 | + "-c", "newConnString" |
600 | + }); |
601 | + |
602 | + // Assert |
603 | + var expectedMessage = |
604 | + "Template connection \"missingConnection\" could not be found." + Environment.NewLine; |
605 | + Assert.Equal(expectedMessage, messageService.Contents); |
606 | + } |
607 | + |
608 | + private static IEnumerable<SavedConnection> CreateConnections() |
609 | + { |
610 | + return new[] |
611 | + { |
612 | + new SavedConnection |
613 | + { |
614 | + Name = "connection1", |
615 | + ConnectionProvider = "provider", |
616 | + ConnectionString = "connString", |
617 | + Dialect = "dialect", |
618 | + DriverClass = "driver", |
619 | + IsDefault = false |
620 | + }, |
621 | + new SavedConnection |
622 | + { |
623 | + Name = "connection2", |
624 | + ConnectionProvider = "provider2", |
625 | + ConnectionString = "connString2", |
626 | + Dialect = "dialect2", |
627 | + DriverClass = "driver2", |
628 | + IsDefault = true |
629 | + } |
630 | + }; |
631 | + } |
632 | + } |
633 | +} |
634 | + |
635 | |
636 | === modified file 'src/DatabaseVersion.Console.Tests/dbversion.Console.Tests.csproj' |
637 | --- src/DatabaseVersion.Console.Tests/dbversion.Console.Tests.csproj 2011-10-25 21:46:32 +0000 |
638 | +++ src/DatabaseVersion.Console.Tests/dbversion.Console.Tests.csproj 2011-11-15 22:04:37 +0000 |
639 | @@ -48,6 +48,7 @@ |
640 | <Compile Include="Command\Help\HelpCommandTests.cs" /> |
641 | <Compile Include="MessageServiceMock.cs" /> |
642 | <Compile Include="Command\History\HistoryCommandTests.cs" /> |
643 | + <Compile Include="Command\SavedConnection\SavedConnectionCommandTests.cs" /> |
644 | </ItemGroup> |
645 | <Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" /> |
646 | <ItemGroup> |
647 | |
648 | === removed file 'src/DatabaseVersion.Console/Arguments.cs' |
649 | --- src/DatabaseVersion.Console/Arguments.cs 2011-09-04 22:51:13 +0000 |
650 | +++ src/DatabaseVersion.Console/Arguments.cs 1970-01-01 00:00:00 +0000 |
651 | @@ -1,59 +0,0 @@ |
652 | -using System.ComponentModel; |
653 | -using System; |
654 | -using CommandLine; |
655 | -using CommandLine.Text; |
656 | - |
657 | -namespace dbversion.Console |
658 | -{ |
659 | - /// <summary> |
660 | - /// The command line arguments that define how the application should function. |
661 | - /// </summary> |
662 | - public class Arguments |
663 | - { |
664 | - /// <summary> |
665 | - /// The connection string to the database. |
666 | - /// </summary> |
667 | - [Option("c", "connectionString", HelpText = "The database connection string.")] |
668 | - public string ConnectionString; |
669 | - |
670 | - /// <summary> |
671 | - /// The archive containing the database scripts. |
672 | - /// </summary> |
673 | - [Option("a", "archive", HelpText = "The archive containing the database versions.")] |
674 | - public string Archive; |
675 | - |
676 | - /// <summary> |
677 | - /// The version to create or upgrade to. |
678 | - /// </summary> |
679 | - [Option("v", "version", HelpText = "The version to create or upgrade to.")] |
680 | - public string Version; |
681 | - |
682 | - /// <summary> |
683 | - /// Gets or sets the hibernate connection provider. |
684 | - /// </summary> |
685 | - [Option("p", "connectionProvider", HelpText = "The hibernate connection provider.")] |
686 | - public string ConnectionProvider; |
687 | - |
688 | - /// <summary> |
689 | - /// Gets or sets the hibernate driver class. |
690 | - /// </summary> |
691 | - [Option("d", "driverClass", HelpText = "The hibernate driver class.")] |
692 | - public string DriverClass; |
693 | - |
694 | - /// <summary> |
695 | - /// Gets or sets the hibernate dialect. |
696 | - /// </summary> |
697 | - [Option("l", "dialect", HelpText = "The hibernate dialect.")] |
698 | - public string Dialect; |
699 | - |
700 | - [HelpOption("h", "help", HelpText = "Display this help screen.")] |
701 | - public string GetHelp() |
702 | - { |
703 | - var help = new HelpText("dbversion"); |
704 | - help.AdditionalNewLineAfterOption = true; |
705 | - help.AddOptions(this); |
706 | - |
707 | - return help; |
708 | - } |
709 | - } |
710 | -} |
711 | |
712 | === added file 'src/DatabaseVersion.Console/Command/ConnectionArguments.cs' |
713 | --- src/DatabaseVersion.Console/Command/ConnectionArguments.cs 1970-01-01 00:00:00 +0000 |
714 | +++ src/DatabaseVersion.Console/Command/ConnectionArguments.cs 2011-11-15 22:04:37 +0000 |
715 | @@ -0,0 +1,46 @@ |
716 | +using System.ComponentModel; |
717 | +using System; |
718 | +using CommandLine; |
719 | +using CommandLine.Text; |
720 | + |
721 | +namespace dbversion.Console.Command |
722 | +{ |
723 | + /// <summary> |
724 | + /// The command line arguments that define how the application should function. |
725 | + /// </summary> |
726 | + public class ConnectionArguments |
727 | + { |
728 | + /// <summary> |
729 | + /// The connection string to the database. |
730 | + /// </summary> |
731 | + [Option("c", "connectionString", HelpText = "The database connection string.")] |
732 | + public string ConnectionString; |
733 | + |
734 | + /// <summary> |
735 | + /// Gets or sets the hibernate connection provider. |
736 | + /// </summary> |
737 | + [Option("p", "connectionProvider", HelpText = "The hibernate connection provider.")] |
738 | + public string ConnectionProvider; |
739 | + |
740 | + /// <summary> |
741 | + /// Gets or sets the hibernate driver class. |
742 | + /// </summary> |
743 | + [Option("d", "driverClass", HelpText = "The hibernate driver class.")] |
744 | + public string DriverClass; |
745 | + |
746 | + /// <summary> |
747 | + /// Gets or sets the hibernate dialect. |
748 | + /// </summary> |
749 | + [Option("l", "dialect", HelpText = "The hibernate dialect.")] |
750 | + public string Dialect; |
751 | + |
752 | + [Option("s", "saved-connection")] |
753 | + public string SavedConnection; |
754 | + |
755 | + /// <summary> |
756 | + /// The archive containing the database scripts. |
757 | + /// </summary> |
758 | + [Option("a", "archive", HelpText = "The archive containing the database versions.")] |
759 | + public string Archive; |
760 | + } |
761 | +} |
762 | |
763 | === added file 'src/DatabaseVersion.Console/Command/ConnectionCommandBase.cs' |
764 | --- src/DatabaseVersion.Console/Command/ConnectionCommandBase.cs 1970-01-01 00:00:00 +0000 |
765 | +++ src/DatabaseVersion.Console/Command/ConnectionCommandBase.cs 2011-11-15 22:04:37 +0000 |
766 | @@ -0,0 +1,233 @@ |
767 | +namespace dbversion.Console.Command |
768 | +{ |
769 | + using System.Collections.Generic; |
770 | + using System.ComponentModel.Composition; |
771 | + using System.Linq; |
772 | + |
773 | + using CommandLine; |
774 | + |
775 | + using dbversion.Archives; |
776 | + using dbversion.Connections; |
777 | + using dbversion.Property; |
778 | + using dbversion.Session; |
779 | + using dbversion.Settings; |
780 | + |
781 | + /// <summary> |
782 | + /// A base class for commands that need to connect to the database. |
783 | + /// </summary> |
784 | + public abstract class ConnectionCommandBase<T> : IConsoleCommand where T : ConnectionArguments, new() |
785 | + { |
786 | + [Import] |
787 | + public IMessageService MessageService |
788 | + { |
789 | + get; |
790 | + set; |
791 | + } |
792 | + |
793 | + [Import] |
794 | + public IPropertyService PropertyService |
795 | + { |
796 | + get; |
797 | + set; |
798 | + } |
799 | + |
800 | + [Import] |
801 | + public ISettingsService SettingsService |
802 | + { |
803 | + get; |
804 | + set; |
805 | + } |
806 | + |
807 | + [Import] |
808 | + public ISavedConnectionService SavedConnectionService |
809 | + { |
810 | + get; |
811 | + set; |
812 | + } |
813 | + |
814 | + [ImportMany] |
815 | + public IEnumerable<IDatabaseArchiveFactory> ArchiveFactories |
816 | + { |
817 | + get; |
818 | + set; |
819 | + } |
820 | + |
821 | + public abstract string Name { get; } |
822 | + |
823 | + public abstract string Description { get; } |
824 | + |
825 | + public abstract string Usage { get; } |
826 | + |
827 | + public abstract IEnumerable<CommandParameter> Parameters { get; } |
828 | + |
829 | + /// <summary> |
830 | + /// Execute the command with the specified arguments. |
831 | + /// </summary> |
832 | + /// <param name='args'> |
833 | + /// The arguments. |
834 | + /// </param> |
835 | + public void Execute(string[] args) |
836 | + { |
837 | + T arguments = ParseArguments(args); |
838 | + |
839 | + var archive = GetArchive(arguments.Archive); |
840 | + |
841 | + this.SavedConnectionService.LoadConnections(); |
842 | + |
843 | + this.PropertyService.SetDefaultProperties(); |
844 | + this.MergePropertiesFromSettings(); |
845 | + |
846 | + if (archive != null) |
847 | + { |
848 | + this.PropertyService.Merge(archive.Properties); |
849 | + } |
850 | + |
851 | + this.MergeDefaultConnectionProperties(); |
852 | + this.SetPropertiesFromCommandArguments(arguments); |
853 | + |
854 | + this.Execute(args, arguments, archive); |
855 | + } |
856 | + |
857 | + protected abstract void Execute(string[] args, T arguments, IDatabaseArchive archive); |
858 | + |
859 | + /// <summary> |
860 | + /// Parses the arguments. |
861 | + /// </summary> |
862 | + /// <param name='args'> |
863 | + /// The command line arguments. |
864 | + /// </param> |
865 | + /// <returns> |
866 | + /// The parsed arguments. |
867 | + /// </returns> |
868 | + private static T ParseArguments(string[] args) |
869 | + { |
870 | + T arguments = new T(); |
871 | + var parserSettings = new CommandLineParserSettings(); |
872 | + parserSettings.CaseSensitive = true; |
873 | + parserSettings.HelpWriter = System.Console.Out; |
874 | + |
875 | + var parser = new CommandLineParser(parserSettings); |
876 | + parser.ParseArguments(args, arguments); |
877 | + |
878 | + return arguments; |
879 | + } |
880 | + |
881 | + /// <summary> |
882 | + /// Gets the archive. |
883 | + /// </summary> |
884 | + /// <param name='archivePath'> |
885 | + /// Archive path. |
886 | + /// </param> |
887 | + /// <returns> |
888 | + /// The archive or null if none could be created. |
889 | + /// </returns> |
890 | + private IDatabaseArchive GetArchive(string archivePath) |
891 | + { |
892 | + IDatabaseArchiveFactory archiveFactory = GetArchiveFactory(archivePath); |
893 | + if (archiveFactory == null) |
894 | + { |
895 | + return null; |
896 | + } |
897 | + |
898 | + return archiveFactory.Create(archivePath); |
899 | + } |
900 | + |
901 | + /// <summary> |
902 | + /// Gets the archive factory. |
903 | + /// </summary> |
904 | + /// <param name='archivePath'> |
905 | + /// Archive path. |
906 | + /// </param> |
907 | + /// <returns> |
908 | + /// The archive factory or null if none can be found. |
909 | + /// </returns> |
910 | + private IDatabaseArchiveFactory GetArchiveFactory(string archivePath) |
911 | + { |
912 | + return this.ArchiveFactories.FirstOrDefault(f => f.CanCreate(archivePath)); |
913 | + } |
914 | + |
915 | + /// <summary> |
916 | + /// Sets the properties from the command arguments. |
917 | + /// </summary> |
918 | + /// <param name='arguments'> |
919 | + /// The command arguments. |
920 | + /// </param> |
921 | + private void SetPropertiesFromCommandArguments(T arguments) |
922 | + { |
923 | + if (!string.IsNullOrEmpty(arguments.ConnectionString)) |
924 | + { |
925 | + this.PropertyService.Add( |
926 | + new Property |
927 | + { |
928 | + Key = SessionFactoryProvider.ConnectionStringProperty, |
929 | + Value = arguments.ConnectionString |
930 | + }); |
931 | + } |
932 | + |
933 | + if (!string.IsNullOrEmpty(arguments.ConnectionProvider)) |
934 | + { |
935 | + this.PropertyService.Add( |
936 | + new Property |
937 | + { |
938 | + Key = SessionFactoryProvider.ProviderProperty, |
939 | + Value = arguments.ConnectionProvider |
940 | + }); |
941 | + } |
942 | + |
943 | + if (!string.IsNullOrEmpty(arguments.DriverClass)) |
944 | + { |
945 | + this.PropertyService.Add( |
946 | + new Property |
947 | + { |
948 | + Key = SessionFactoryProvider.DriverClassProperty, |
949 | + Value = arguments.DriverClass |
950 | + }); |
951 | + } |
952 | + |
953 | + if (!string.IsNullOrEmpty(arguments.Dialect)) |
954 | + { |
955 | + this.PropertyService.Add( |
956 | + new Property |
957 | + { |
958 | + Key = SessionFactoryProvider.DialectProperty, |
959 | + Value = arguments.Dialect |
960 | + }); |
961 | + } |
962 | + |
963 | + if (!string.IsNullOrEmpty(arguments.SavedConnection)) |
964 | + { |
965 | + var savedConnection = |
966 | + this.SavedConnectionService.SavedConnections.FirstOrDefault(c => c.Name == arguments.SavedConnection); |
967 | + if (savedConnection != null) |
968 | + { |
969 | + this.PropertyService.Merge(savedConnection.GetConnectionProperties()); |
970 | + } |
971 | + } |
972 | + } |
973 | + |
974 | + /// <summary> |
975 | + /// Merges the properties from user settings. |
976 | + /// </summary> |
977 | + private void MergePropertiesFromSettings() |
978 | + { |
979 | + var propertyCollection = this.SettingsService.DeSerialize<PropertyCollection>("properties.xml"); |
980 | + if (propertyCollection != null) |
981 | + { |
982 | + this.PropertyService.Merge(propertyCollection.Properties); |
983 | + } |
984 | + } |
985 | + |
986 | + /// <summary> |
987 | + /// Merges the default connection properties. |
988 | + /// </summary> |
989 | + private void MergeDefaultConnectionProperties() |
990 | + { |
991 | + var defaultConnection = this.SavedConnectionService.DefaultConnection; |
992 | + if (defaultConnection != null) |
993 | + { |
994 | + this.PropertyService.Merge(defaultConnection.GetConnectionProperties()); |
995 | + } |
996 | + } |
997 | + } |
998 | +} |
999 | + |
1000 | |
1001 | === added file 'src/DatabaseVersion.Console/Command/Create/CreateArguments.cs' |
1002 | --- src/DatabaseVersion.Console/Command/Create/CreateArguments.cs 1970-01-01 00:00:00 +0000 |
1003 | +++ src/DatabaseVersion.Console/Command/Create/CreateArguments.cs 2011-11-15 22:04:37 +0000 |
1004 | @@ -0,0 +1,17 @@ |
1005 | +namespace dbversion.Console.Command.Create |
1006 | +{ |
1007 | + using CommandLine; |
1008 | + |
1009 | + /// <summary> |
1010 | + /// The arguments for the create command. |
1011 | + /// </summary> |
1012 | + public class CreateArguments : ConnectionArguments |
1013 | + { |
1014 | + /// <summary> |
1015 | + /// The version to create or upgrade to. |
1016 | + /// </summary> |
1017 | + [Option("v", "version", HelpText = "The version to create or upgrade to.")] |
1018 | + public string Version; |
1019 | + } |
1020 | +} |
1021 | + |
1022 | |
1023 | === modified file 'src/DatabaseVersion.Console/Command/Create/CreateCommand.cs' |
1024 | --- src/DatabaseVersion.Console/Command/Create/CreateCommand.cs 2011-10-06 11:08:56 +0000 |
1025 | +++ src/DatabaseVersion.Console/Command/Create/CreateCommand.cs 2011-11-15 22:04:37 +0000 |
1026 | @@ -1,23 +1,16 @@ |
1027 | namespace dbversion.Console.Command.Create |
1028 | { |
1029 | - using System; |
1030 | using System.Collections.Generic; |
1031 | using System.ComponentModel.Composition; |
1032 | - using System.Linq; |
1033 | - |
1034 | - using CommandLine; |
1035 | |
1036 | using dbversion.Archives; |
1037 | - using dbversion.Property; |
1038 | - using dbversion.Session; |
1039 | - using dbversion.Settings; |
1040 | + using dbversion.Version; |
1041 | using dbversion.Tasks; |
1042 | - using dbversion.Version; |
1043 | |
1044 | [Export(typeof(IConsoleCommand))] |
1045 | - public class CreateCommand : IConsoleCommand |
1046 | + public class CreateCommand : ConnectionCommandBase<CreateArguments> |
1047 | { |
1048 | - public string Name |
1049 | + public override string Name |
1050 | { |
1051 | get |
1052 | { |
1053 | @@ -25,7 +18,7 @@ |
1054 | } |
1055 | } |
1056 | |
1057 | - public string Description |
1058 | + public override string Description |
1059 | { |
1060 | get |
1061 | { |
1062 | @@ -33,7 +26,7 @@ |
1063 | } |
1064 | } |
1065 | |
1066 | - public string Usage |
1067 | + public override string Usage |
1068 | { |
1069 | get |
1070 | { |
1071 | @@ -41,7 +34,7 @@ |
1072 | } |
1073 | } |
1074 | |
1075 | - public IEnumerable<CommandParameter> Parameters |
1076 | + public override IEnumerable<CommandParameter> Parameters |
1077 | { |
1078 | get |
1079 | { |
1080 | @@ -52,7 +45,8 @@ |
1081 | new CommandParameter("-v", "--version", "The version to create or upgrade to."), |
1082 | new CommandParameter("-p", "--connectionProvider", "The hibernate connection provider."), |
1083 | new CommandParameter("-d", "--driverClass", "The hibernate driver class."), |
1084 | - new CommandParameter("-l", "--dialect", "The hibernate dialect.") |
1085 | + new CommandParameter("-l", "--dialect", "The hibernate dialect."), |
1086 | + new CommandParameter("-s", "--saved-connection", "The name of the saved connection to use.") |
1087 | }; |
1088 | } |
1089 | } |
1090 | @@ -64,61 +58,26 @@ |
1091 | set; |
1092 | } |
1093 | |
1094 | - [ImportMany] |
1095 | - public IEnumerable<IDatabaseArchiveFactory> ArchiveFactories |
1096 | - { |
1097 | - get; |
1098 | - set; |
1099 | - } |
1100 | - |
1101 | - [Import] |
1102 | - public IMessageService MessageService |
1103 | - { |
1104 | - get; |
1105 | - set; |
1106 | - } |
1107 | - |
1108 | - [Import] |
1109 | - public IPropertyService PropertyService |
1110 | - { |
1111 | - get; |
1112 | - set; |
1113 | - } |
1114 | - |
1115 | - [Import] |
1116 | - public ISettingsService SettingsService |
1117 | - { |
1118 | - get; |
1119 | - set; |
1120 | - } |
1121 | - |
1122 | /// <summary> |
1123 | /// Execute the command with the specified arguments. |
1124 | /// </summary> |
1125 | /// <param name='args'> |
1126 | /// The arguments. |
1127 | /// </param> |
1128 | - public void Execute(string[] args) |
1129 | + protected override void Execute (string[] args, CreateArguments arguments, IDatabaseArchive archive) |
1130 | { |
1131 | - Arguments arguments = ParseArguments(args); |
1132 | if (string.IsNullOrEmpty(arguments.Archive)) |
1133 | { |
1134 | this.MessageService.WriteLine("Please specify an archive using the -a switch"); |
1135 | return; |
1136 | } |
1137 | |
1138 | - var archive = GetArchive(arguments.Archive); |
1139 | if (archive == null) |
1140 | { |
1141 | this.MessageService.WriteLine("The specified archive is not supported"); |
1142 | return; |
1143 | } |
1144 | |
1145 | - this.PropertyService.SetDefaultProperties(); |
1146 | - this.MergePropertiesFromSettings(); |
1147 | - this.PropertyService.Merge(archive.Properties); |
1148 | - this.SetPropertiesFromCommandArguments(arguments); |
1149 | - |
1150 | try |
1151 | { |
1152 | this.Creator.Create(archive, arguments.Version, new ConsoleTaskExecuter(MessageService)); |
1153 | @@ -132,122 +91,5 @@ |
1154 | this.MessageService.WriteLine(t.Message); |
1155 | } |
1156 | } |
1157 | - |
1158 | - /// <summary> |
1159 | - /// Parses the arguments. |
1160 | - /// </summary> |
1161 | - /// <param name='args'> |
1162 | - /// The command line arguments. |
1163 | - /// </param> |
1164 | - /// <returns> |
1165 | - /// The parsed arguments. |
1166 | - /// </returns> |
1167 | - private static Arguments ParseArguments(string[] args) |
1168 | - { |
1169 | - Arguments arguments = new Arguments(); |
1170 | - var parserSettings = new CommandLineParserSettings(); |
1171 | - parserSettings.CaseSensitive = true; |
1172 | - parserSettings.HelpWriter = System.Console.Out; |
1173 | - |
1174 | - var parser = new CommandLineParser(parserSettings); |
1175 | - parser.ParseArguments(args, arguments); |
1176 | - |
1177 | - return arguments; |
1178 | - } |
1179 | - |
1180 | - /// <summary> |
1181 | - /// Gets the archive. |
1182 | - /// </summary> |
1183 | - /// <param name='archivePath'> |
1184 | - /// Archive path. |
1185 | - /// </param> |
1186 | - /// <returns> |
1187 | - /// The archive or null if none could be created. |
1188 | - /// </returns> |
1189 | - private IDatabaseArchive GetArchive(string archivePath) |
1190 | - { |
1191 | - IDatabaseArchiveFactory archiveFactory = GetArchiveFactory(archivePath); |
1192 | - if (archiveFactory == null) |
1193 | - { |
1194 | - return null; |
1195 | - } |
1196 | - |
1197 | - return archiveFactory.Create(archivePath); |
1198 | - } |
1199 | - |
1200 | - /// <summary> |
1201 | - /// Gets the archive factory. |
1202 | - /// </summary> |
1203 | - /// <param name='archivePath'> |
1204 | - /// Archive path. |
1205 | - /// </param> |
1206 | - /// <returns> |
1207 | - /// The archive factory or null if none can be found. |
1208 | - /// </returns> |
1209 | - private IDatabaseArchiveFactory GetArchiveFactory(string archivePath) |
1210 | - { |
1211 | - return this.ArchiveFactories.FirstOrDefault(f => f.CanCreate(archivePath)); |
1212 | - } |
1213 | - |
1214 | - /// <summary> |
1215 | - /// Sets the properties from the command arguments. |
1216 | - /// </summary> |
1217 | - /// <param name='arguments'> |
1218 | - /// The command arguments. |
1219 | - /// </param> |
1220 | - private void SetPropertiesFromCommandArguments(Arguments arguments) |
1221 | - { |
1222 | - if (!string.IsNullOrEmpty(arguments.ConnectionString)) |
1223 | - { |
1224 | - this.PropertyService.Add( |
1225 | - new Property |
1226 | - { |
1227 | - Key = SessionFactoryProvider.ConnectionStringProperty, |
1228 | - Value = arguments.ConnectionString |
1229 | - }); |
1230 | - } |
1231 | - |
1232 | - if (!string.IsNullOrEmpty(arguments.ConnectionProvider)) |
1233 | - { |
1234 | - this.PropertyService.Add( |
1235 | - new Property |
1236 | - { |
1237 | - Key = SessionFactoryProvider.ProviderProperty, |
1238 | - Value = arguments.ConnectionProvider |
1239 | - }); |
1240 | - } |
1241 | - |
1242 | - if (!string.IsNullOrEmpty(arguments.DriverClass)) |
1243 | - { |
1244 | - this.PropertyService.Add( |
1245 | - new Property |
1246 | - { |
1247 | - Key = SessionFactoryProvider.DriverClassProperty, |
1248 | - Value = arguments.DriverClass |
1249 | - }); |
1250 | - } |
1251 | - |
1252 | - if (!string.IsNullOrEmpty(arguments.Dialect)) |
1253 | - { |
1254 | - this.PropertyService.Add( |
1255 | - new Property |
1256 | - { |
1257 | - Key = SessionFactoryProvider.DialectProperty, |
1258 | - Value = arguments.Dialect |
1259 | - }); |
1260 | - } |
1261 | - } |
1262 | - |
1263 | - /// <summary> |
1264 | - /// Merges the properties from user settings. |
1265 | - /// </summary> |
1266 | - private void MergePropertiesFromSettings() |
1267 | - { |
1268 | - var propertyCollection = this.SettingsService.DeSerialize<PropertyCollection>("properties.xml"); |
1269 | - if (propertyCollection != null) |
1270 | - { |
1271 | - this.PropertyService.Merge(propertyCollection.Properties); |
1272 | - } |
1273 | - } |
1274 | } |
1275 | } |
1276 | |
1277 | === modified file 'src/DatabaseVersion.Console/Command/History/HistoryArguments.cs' |
1278 | --- src/DatabaseVersion.Console/Command/History/HistoryArguments.cs 2011-10-17 17:38:28 +0000 |
1279 | +++ src/DatabaseVersion.Console/Command/History/HistoryArguments.cs 2011-11-15 22:04:37 +0000 |
1280 | @@ -5,33 +5,9 @@ |
1281 | /// <summary> |
1282 | /// The arguments for the history command. |
1283 | /// </summary> |
1284 | - public class HistoryArguments |
1285 | + public class HistoryArguments : ConnectionArguments |
1286 | { |
1287 | /// <summary> |
1288 | - /// The connection string. |
1289 | - /// </summary> |
1290 | - [Option("c", "connectionString")] |
1291 | - public string ConnectionString; |
1292 | - |
1293 | - /// <summary> |
1294 | - /// Gets or sets the hibernate connection provider. |
1295 | - /// </summary> |
1296 | - [Option("p", "connectionProvider", HelpText = "The hibernate connection provider.")] |
1297 | - public string ConnectionProvider; |
1298 | - |
1299 | - /// <summary> |
1300 | - /// Gets or sets the hibernate driver class. |
1301 | - /// </summary> |
1302 | - [Option("d", "driverClass", HelpText = "The hibernate driver class.")] |
1303 | - public string DriverClass; |
1304 | - |
1305 | - /// <summary> |
1306 | - /// Gets or sets the hibernate dialect. |
1307 | - /// </summary> |
1308 | - [Option("l", "dialect", HelpText = "The hibernate dialect.")] |
1309 | - public string Dialect; |
1310 | - |
1311 | - /// <summary> |
1312 | /// The sort order. |
1313 | /// </summary> |
1314 | [Option("o", "order")] |
1315 | |
1316 | === modified file 'src/DatabaseVersion.Console/Command/History/HistoryCommand.cs' |
1317 | --- src/DatabaseVersion.Console/Command/History/HistoryCommand.cs 2011-10-17 20:07:45 +0000 |
1318 | +++ src/DatabaseVersion.Console/Command/History/HistoryCommand.cs 2011-11-15 22:04:37 +0000 |
1319 | @@ -17,12 +17,12 @@ |
1320 | /// A command for outputting the history of versions installed in the database. |
1321 | /// </summary> |
1322 | [Export(typeof(IConsoleCommand))] |
1323 | - public class HistoryCommand : IConsoleCommand |
1324 | + public class HistoryCommand : ConnectionCommandBase<HistoryArguments> |
1325 | { |
1326 | /// <summary> |
1327 | /// Gets the command name. |
1328 | /// </summary> |
1329 | - public string Name |
1330 | + public override string Name |
1331 | { |
1332 | get |
1333 | { |
1334 | @@ -33,7 +33,7 @@ |
1335 | /// <summary> |
1336 | /// Gets a description of the command. |
1337 | /// </summary> |
1338 | - public string Description |
1339 | + public override string Description |
1340 | { |
1341 | get |
1342 | { |
1343 | @@ -44,7 +44,7 @@ |
1344 | /// <summary> |
1345 | /// Gets the usage of the command. |
1346 | /// </summary> |
1347 | - public string Usage |
1348 | + public override string Usage |
1349 | { |
1350 | get |
1351 | { |
1352 | @@ -55,7 +55,7 @@ |
1353 | /// <summary> |
1354 | /// Gets the command parameters. |
1355 | /// </summary> |
1356 | - public IEnumerable<CommandParameter> Parameters |
1357 | + public override IEnumerable<CommandParameter> Parameters |
1358 | { |
1359 | get |
1360 | { |
1361 | @@ -66,18 +66,13 @@ |
1362 | new CommandParameter("-d", "--driverClass", "The hibernate driver class."), |
1363 | new CommandParameter("-l", "--dialect", "The hibernate dialect."), |
1364 | new CommandParameter("-o", "--order", "The order to sort the versions by [asc|desc]."), |
1365 | - new CommandParameter("-t", "--showTasks", "Indicates whether the tasks for each version should be output.") |
1366 | + new CommandParameter("-t", "--showTasks", "Indicates whether the tasks for each version should be output."), |
1367 | + new CommandParameter("-s", "--saved-connection", "The name of the saved connection to use.") |
1368 | }; |
1369 | } |
1370 | } |
1371 | |
1372 | /// <summary> |
1373 | - /// Gets or sets the message service. |
1374 | - /// </summary> |
1375 | - [Import] |
1376 | - public IMessageService MessageService { get; set; } |
1377 | - |
1378 | - /// <summary> |
1379 | /// Gets or sets the session factory provider. |
1380 | /// </summary> |
1381 | [Import] |
1382 | @@ -90,31 +85,13 @@ |
1383 | public IVersionProvider VersionProvider { get; set; } |
1384 | |
1385 | /// <summary> |
1386 | - /// Gets or sets the property service. |
1387 | - /// </summary> |
1388 | - [Import] |
1389 | - public IPropertyService PropertyService { get; set; } |
1390 | - |
1391 | - /// <summary> |
1392 | - /// Gets or sets the settings service. |
1393 | - /// </summary> |
1394 | - [Import] |
1395 | - public ISettingsService SettingsService { get; set; } |
1396 | - |
1397 | - /// <summary> |
1398 | /// Execute the command with the specified arguments. |
1399 | /// </summary> |
1400 | /// <param name='args'> |
1401 | /// The arguments. |
1402 | /// </param> |
1403 | - public void Execute (string[] args) |
1404 | + protected override void Execute (string[] args, HistoryArguments arguments, dbversion.Archives.IDatabaseArchive archive) |
1405 | { |
1406 | - var arguments = ParseArguments(args); |
1407 | - |
1408 | - this.PropertyService.SetDefaultProperties(); |
1409 | - this.MergePropertiesFromSettings(); |
1410 | - this.SetPropertiesFromCommandArguments(arguments); |
1411 | - |
1412 | VersionBase requiredVersion = null; |
1413 | if (!string.IsNullOrEmpty(arguments.Version)) |
1414 | { |
1415 | @@ -241,89 +218,6 @@ |
1416 | this.MessageService.WriteLine(taskString); |
1417 | } |
1418 | } |
1419 | - |
1420 | - /// <summary> |
1421 | - /// Parses the arguments. |
1422 | - /// </summary> |
1423 | - /// <param name='args'> |
1424 | - /// The command line arguments. |
1425 | - /// </param> |
1426 | - /// <returns> |
1427 | - /// The parsed arguments. |
1428 | - /// </returns> |
1429 | - private static HistoryArguments ParseArguments(string[] args) |
1430 | - { |
1431 | - HistoryArguments arguments = new HistoryArguments(); |
1432 | - var parserSettings = new CommandLineParserSettings(); |
1433 | - parserSettings.CaseSensitive = true; |
1434 | - parserSettings.HelpWriter = System.Console.Out; |
1435 | - |
1436 | - var parser = new CommandLineParser(parserSettings); |
1437 | - parser.ParseArguments(args, arguments); |
1438 | - |
1439 | - return arguments; |
1440 | - } |
1441 | - |
1442 | - /// <summary> |
1443 | - /// Merges the properties from user settings. |
1444 | - /// </summary> |
1445 | - private void MergePropertiesFromSettings() |
1446 | - { |
1447 | - var propertyCollection = this.SettingsService.DeSerialize<PropertyCollection>("properties.xml"); |
1448 | - if (propertyCollection != null) |
1449 | - { |
1450 | - this.PropertyService.Merge(propertyCollection.Properties); |
1451 | - } |
1452 | - } |
1453 | - |
1454 | - /// <summary> |
1455 | - /// Sets the properties from the command arguments. |
1456 | - /// </summary> |
1457 | - /// <param name='arguments'> |
1458 | - /// The command arguments. |
1459 | - /// </param> |
1460 | - private void SetPropertiesFromCommandArguments(HistoryArguments arguments) |
1461 | - { |
1462 | - if (!string.IsNullOrEmpty(arguments.ConnectionString)) |
1463 | - { |
1464 | - this.PropertyService.Add( |
1465 | - new Property |
1466 | - { |
1467 | - Key = Session.SessionFactoryProvider.ConnectionStringProperty, |
1468 | - Value = arguments.ConnectionString |
1469 | - }); |
1470 | - } |
1471 | - |
1472 | - if (!string.IsNullOrEmpty(arguments.ConnectionProvider)) |
1473 | - { |
1474 | - this.PropertyService.Add( |
1475 | - new Property |
1476 | - { |
1477 | - Key = Session.SessionFactoryProvider.ProviderProperty, |
1478 | - Value = arguments.ConnectionProvider |
1479 | - }); |
1480 | - } |
1481 | - |
1482 | - if (!string.IsNullOrEmpty(arguments.DriverClass)) |
1483 | - { |
1484 | - this.PropertyService.Add( |
1485 | - new Property |
1486 | - { |
1487 | - Key = Session.SessionFactoryProvider.DriverClassProperty, |
1488 | - Value = arguments.DriverClass |
1489 | - }); |
1490 | - } |
1491 | - |
1492 | - if (!string.IsNullOrEmpty(arguments.Dialect)) |
1493 | - { |
1494 | - this.PropertyService.Add( |
1495 | - new Property |
1496 | - { |
1497 | - Key = Session.SessionFactoryProvider.DialectProperty, |
1498 | - Value = arguments.Dialect |
1499 | - }); |
1500 | - } |
1501 | - } |
1502 | } |
1503 | } |
1504 | |
1505 | |
1506 | === added directory 'src/DatabaseVersion.Console/Command/SavedConnection' |
1507 | === added file 'src/DatabaseVersion.Console/Command/SavedConnection/SavedConnectionArguments.cs' |
1508 | --- src/DatabaseVersion.Console/Command/SavedConnection/SavedConnectionArguments.cs 1970-01-01 00:00:00 +0000 |
1509 | +++ src/DatabaseVersion.Console/Command/SavedConnection/SavedConnectionArguments.cs 2011-11-15 22:04:37 +0000 |
1510 | @@ -0,0 +1,52 @@ |
1511 | +namespace dbversion.Console.Command.SavedConnection |
1512 | +{ |
1513 | + using System.ComponentModel; |
1514 | + using System; |
1515 | + using CommandLine; |
1516 | + using CommandLine.Text; |
1517 | + |
1518 | + /// <summary> |
1519 | + /// The command line arguments that define how the application should function. |
1520 | + /// </summary> |
1521 | + public class SavedConnectionArguments |
1522 | + { |
1523 | + /// <summary> |
1524 | + /// The archive containing the database scripts. |
1525 | + /// </summary> |
1526 | + [Option("n", "name")] |
1527 | + public string Name; |
1528 | + |
1529 | + /// <summary> |
1530 | + /// The connection string to the database. |
1531 | + /// </summary> |
1532 | + [Option("c", "connectionString", HelpText = "The database connection string.")] |
1533 | + public string ConnectionString; |
1534 | + |
1535 | + /// <summary> |
1536 | + /// Gets or sets the hibernate connection provider. |
1537 | + /// </summary> |
1538 | + [Option("p", "connectionProvider", HelpText = "The hibernate connection provider.")] |
1539 | + public string ConnectionProvider; |
1540 | + |
1541 | + /// <summary> |
1542 | + /// Gets or sets the hibernate driver class. |
1543 | + /// </summary> |
1544 | + [Option("d", "driverClass", HelpText = "The hibernate driver class.")] |
1545 | + public string DriverClass; |
1546 | + |
1547 | + /// <summary> |
1548 | + /// Gets or sets the hibernate dialect. |
1549 | + /// </summary> |
1550 | + [Option("l", "dialect", HelpText = "The hibernate dialect.")] |
1551 | + public string Dialect; |
1552 | + |
1553 | + [Option("r", "delete")] |
1554 | + public bool Delete; |
1555 | + |
1556 | + [Option(null, "default")] |
1557 | + public bool SetDefaultConnection; |
1558 | + |
1559 | + [Option("t", "template")] |
1560 | + public string TemplateConnection; |
1561 | + } |
1562 | +} |
1563 | |
1564 | === added file 'src/DatabaseVersion.Console/Command/SavedConnection/SavedConnectionCommand.cs' |
1565 | --- src/DatabaseVersion.Console/Command/SavedConnection/SavedConnectionCommand.cs 1970-01-01 00:00:00 +0000 |
1566 | +++ src/DatabaseVersion.Console/Command/SavedConnection/SavedConnectionCommand.cs 2011-11-15 22:04:37 +0000 |
1567 | @@ -0,0 +1,326 @@ |
1568 | +namespace dbversion.Console.Command.SavedConnection |
1569 | +{ |
1570 | + using System.Collections.Generic; |
1571 | + using System.ComponentModel.Composition; |
1572 | + using System.Linq; |
1573 | + |
1574 | + using CommandLine; |
1575 | + |
1576 | + using dbversion.Connections; |
1577 | + |
1578 | + using dbversion.Console.Command; |
1579 | + |
1580 | + [Export(typeof(IConsoleCommand))] |
1581 | + public class SavedConnectionCommand : IConsoleCommand |
1582 | + { |
1583 | + /// <summary> |
1584 | + /// Gets the name of the command. This is the name that will be used |
1585 | + /// to execute the command at the command line. |
1586 | + /// </summary> |
1587 | + /// <value> |
1588 | + /// The name of the command to execute. |
1589 | + /// </value> |
1590 | + public string Name |
1591 | + { |
1592 | + get |
1593 | + { |
1594 | + return "saved-connection"; |
1595 | + } |
1596 | + } |
1597 | + |
1598 | + /// <summary> |
1599 | + /// Gets a short description of what the command does. |
1600 | + /// </summary> |
1601 | + /// <value> |
1602 | + /// The description of the command. |
1603 | + /// </value> |
1604 | + public string Description |
1605 | + { |
1606 | + get |
1607 | + { |
1608 | + return "Allows connection details to be saved and managed."; |
1609 | + } |
1610 | + } |
1611 | + |
1612 | + /// <summary> |
1613 | + /// Gets the command usage. |
1614 | + /// </summary> |
1615 | + /// <value> |
1616 | + /// The command usage. |
1617 | + /// </value> |
1618 | + public string Usage |
1619 | + { |
1620 | + get |
1621 | + { |
1622 | + return "dbversion saved-connection [options]"; |
1623 | + } |
1624 | + } |
1625 | + |
1626 | + /// <summary> |
1627 | + /// Gets the parameters that can be used with the command. |
1628 | + /// </summary> |
1629 | + /// <value> |
1630 | + /// The parameters or an empty enumerable. |
1631 | + /// </value> |
1632 | + public System.Collections.Generic.IEnumerable<CommandParameter> Parameters |
1633 | + { |
1634 | + get |
1635 | + { |
1636 | + return new[] |
1637 | + { |
1638 | + new CommandParameter("-n", "--name", "The name of the connection."), |
1639 | + new CommandParameter("-c", "--connectionString", "The database connection string."), |
1640 | + new CommandParameter("-p", "--connectionProvider", "The hibernate connection provider."), |
1641 | + new CommandParameter("-d", "--driverClass", "The hibernate driver class."), |
1642 | + new CommandParameter("-l", "--dialect", "The hibernate dialect."), |
1643 | + new CommandParameter("-r", "--remove", "Deletes the connection specified using the -n option."), |
1644 | + new CommandParameter(string.Empty, "--default", "Sets the connection specified using the -n option as the default."), |
1645 | + new CommandParameter("-t", "--template", "Uses the specified connection as a template.") |
1646 | + }; |
1647 | + } |
1648 | + } |
1649 | + |
1650 | + [Import] |
1651 | + public IMessageService MessageService |
1652 | + { |
1653 | + get; |
1654 | + set; |
1655 | + } |
1656 | + |
1657 | + [Import] |
1658 | + public ISavedConnectionService SavedConnectionService |
1659 | + { |
1660 | + get; |
1661 | + set; |
1662 | + } |
1663 | + |
1664 | + |
1665 | + public void Execute (string[] args) |
1666 | + { |
1667 | + var arguments = ParseArguments(args); |
1668 | + |
1669 | + this.SavedConnectionService.LoadConnections(); |
1670 | + |
1671 | + if (!string.IsNullOrEmpty(arguments.Name) && |
1672 | + !string.IsNullOrEmpty(arguments.TemplateConnection)) |
1673 | + { |
1674 | + this.CreateFromTemplate(arguments); |
1675 | + } |
1676 | + else if (!string.IsNullOrEmpty(arguments.Name) && |
1677 | + (!string.IsNullOrEmpty(arguments.ConnectionProvider) || |
1678 | + !string.IsNullOrEmpty(arguments.ConnectionString) || |
1679 | + !string.IsNullOrEmpty(arguments.Dialect) || |
1680 | + !string.IsNullOrEmpty(arguments.DriverClass))) |
1681 | + { |
1682 | + this.CreateNewConnection(arguments); |
1683 | + } |
1684 | + else if (!string.IsNullOrEmpty(arguments.Name) && arguments.SetDefaultConnection) |
1685 | + { |
1686 | + this.SetDefaultConnection(arguments); |
1687 | + } |
1688 | + else if (!string.IsNullOrEmpty(arguments.Name) && arguments.Delete) |
1689 | + { |
1690 | + this.DeleteConnection(arguments); |
1691 | + } |
1692 | + else |
1693 | + { |
1694 | + this.ListConnections(arguments); |
1695 | + } |
1696 | + } |
1697 | + |
1698 | + private void CreateFromTemplate (SavedConnectionArguments arguments) |
1699 | + { |
1700 | + var template = this.SavedConnectionService.SavedConnections.FirstOrDefault( |
1701 | + c => c.Name == arguments.TemplateConnection); |
1702 | + |
1703 | + if (template != null) |
1704 | + { |
1705 | + var connectionString = !string.IsNullOrEmpty(arguments.ConnectionString) |
1706 | + ? arguments.ConnectionString : template.ConnectionString; |
1707 | + var driverClass = !string.IsNullOrEmpty(arguments.DriverClass) |
1708 | + ? arguments.DriverClass : template.DriverClass; |
1709 | + var dialect = !string.IsNullOrEmpty(arguments.Dialect) |
1710 | + ? arguments.Dialect : template.Dialect; |
1711 | + var provider = !string.IsNullOrEmpty(arguments.ConnectionProvider) |
1712 | + ? arguments.ConnectionProvider : template.ConnectionProvider; |
1713 | + |
1714 | + var connection = this.SavedConnectionService.CreateSavedConnection( |
1715 | + arguments.Name, |
1716 | + connectionString, |
1717 | + provider, |
1718 | + driverClass, |
1719 | + dialect); |
1720 | + this.SavedConnectionService.SaveConnections(); |
1721 | + |
1722 | + this.MessageService.WriteLine( |
1723 | + string.Format( |
1724 | + "Created a new connection \"{0}\" based on \"{1}\".", arguments.Name, arguments.TemplateConnection)); |
1725 | + this.WriteConnectionInfo(connection); |
1726 | + } |
1727 | + else |
1728 | + { |
1729 | + this.MessageService.WriteLine( |
1730 | + string.Format("Template connection \"{0}\" could not be found.", arguments.TemplateConnection)); |
1731 | + } |
1732 | + } |
1733 | + |
1734 | + /// <summary> |
1735 | + /// Creates the new connection. |
1736 | + /// </summary> |
1737 | + /// <param name='arguments'> |
1738 | + /// The arguments to create the connection with. |
1739 | + /// </param> |
1740 | + /// <returns> |
1741 | + /// The new connection. |
1742 | + /// </returns> |
1743 | + private void CreateNewConnection(SavedConnectionArguments arguments) |
1744 | + { |
1745 | + var existingConnection = |
1746 | + this.SavedConnectionService.SavedConnections.FirstOrDefault(c => c.Name == arguments.Name); |
1747 | + |
1748 | + string name; |
1749 | + string connectionString; |
1750 | + string connectionProvider; |
1751 | + string driverClass; |
1752 | + string dialect; |
1753 | + |
1754 | + if (existingConnection != null) |
1755 | + { |
1756 | + name = existingConnection.Name; |
1757 | + connectionString = !string.IsNullOrEmpty(arguments.ConnectionString) |
1758 | + ? arguments.ConnectionString : existingConnection.ConnectionString; |
1759 | + connectionProvider = !string.IsNullOrEmpty(arguments.ConnectionProvider) |
1760 | + ? arguments.ConnectionProvider : existingConnection.ConnectionProvider; |
1761 | + driverClass = !string.IsNullOrEmpty(arguments.DriverClass) |
1762 | + ? arguments.DriverClass : existingConnection.DriverClass; |
1763 | + dialect = !string.IsNullOrEmpty(arguments.Dialect) |
1764 | + ? arguments.Dialect : existingConnection.Dialect; |
1765 | + } |
1766 | + else |
1767 | + { |
1768 | + name = arguments.Name; |
1769 | + connectionString = arguments.ConnectionString; |
1770 | + connectionProvider = arguments.ConnectionProvider; |
1771 | + driverClass = arguments.DriverClass; |
1772 | + dialect = arguments.Dialect; |
1773 | + } |
1774 | + |
1775 | + var connection = this.SavedConnectionService.CreateSavedConnection( |
1776 | + name, |
1777 | + connectionString, |
1778 | + connectionProvider, |
1779 | + driverClass, |
1780 | + dialect); |
1781 | + this.SavedConnectionService.SaveConnections(); |
1782 | + |
1783 | + this.MessageService.WriteLine( |
1784 | + string.Format("Created new saved connection \"{0}\":", connection.Name)); |
1785 | + this.WriteConnectionInfo(connection); |
1786 | + } |
1787 | + |
1788 | + private void SetDefaultConnection (SavedConnectionArguments arguments) |
1789 | + { |
1790 | + if (this.SavedConnectionService.SetDefaultConnection(arguments.Name)) |
1791 | + { |
1792 | + this.SavedConnectionService.SaveConnections(); |
1793 | + this.MessageService.WriteLine( |
1794 | + string.Format("\"{0}\" is now the default connection.", arguments.Name)); |
1795 | + } |
1796 | + else |
1797 | + { |
1798 | + this.MessageService.WriteLine( |
1799 | + string.Format("There is no saved connection called \"{0}\".", arguments.Name)); |
1800 | + } |
1801 | + } |
1802 | + |
1803 | + private void DeleteConnection (SavedConnectionArguments arguments) |
1804 | + { |
1805 | + if (this.SavedConnectionService.DeleteConnection(arguments.Name)) |
1806 | + { |
1807 | + this.SavedConnectionService.SaveConnections(); |
1808 | + this.MessageService.WriteLine( |
1809 | + string.Format("\"{0}\" has been deleted.", arguments.Name)); |
1810 | + } |
1811 | + else |
1812 | + { |
1813 | + this.MessageService.WriteLine( |
1814 | + string.Format("There is no saved connection called \"{0}\".", arguments.Name)); |
1815 | + } |
1816 | + } |
1817 | + |
1818 | + private void ListConnections (SavedConnectionArguments arguments) |
1819 | + { |
1820 | + var connections = this.GetConnections(arguments); |
1821 | + |
1822 | + if (connections.Count() == 0) |
1823 | + { |
1824 | + if (!string.IsNullOrEmpty(arguments.Name)) |
1825 | + { |
1826 | + this.MessageService.WriteLine( |
1827 | + string.Format("There is no saved connection called \"{0}\".", arguments.Name)); |
1828 | + } |
1829 | + else |
1830 | + { |
1831 | + this.MessageService.WriteLine("There are no saved connections."); |
1832 | + } |
1833 | + } |
1834 | + else |
1835 | + { |
1836 | + foreach (var connection in connections) |
1837 | + { |
1838 | + this.MessageService.WriteLine(connection.Name + ":"); |
1839 | + this.WriteConnectionInfo(connection); |
1840 | + } |
1841 | + } |
1842 | + } |
1843 | + |
1844 | + /// <summary> |
1845 | + /// Parses the arguments. |
1846 | + /// </summary> |
1847 | + /// <param name='args'> |
1848 | + /// The command line arguments. |
1849 | + /// </param> |
1850 | + /// <returns> |
1851 | + /// The parsed arguments. |
1852 | + /// </returns> |
1853 | + private static SavedConnectionArguments ParseArguments(string[] args) |
1854 | + { |
1855 | + var arguments = new SavedConnectionArguments(); |
1856 | + var parserSettings = new CommandLineParserSettings(); |
1857 | + parserSettings.CaseSensitive = true; |
1858 | + parserSettings.HelpWriter = System.Console.Out; |
1859 | + |
1860 | + var parser = new CommandLineParser(parserSettings); |
1861 | + parser.ParseArguments(args, arguments); |
1862 | + |
1863 | + return arguments; |
1864 | + } |
1865 | + |
1866 | + private IEnumerable<SavedConnection> GetConnections(SavedConnectionArguments arguments) |
1867 | + { |
1868 | + if (!string.IsNullOrEmpty(arguments.Name)) |
1869 | + { |
1870 | + return this.SavedConnectionService.SavedConnections |
1871 | + .Where(c => c.Name == arguments.Name); |
1872 | + } |
1873 | + |
1874 | + return this.SavedConnectionService.SavedConnections |
1875 | + .Where(c => c.IsDefault) |
1876 | + .Union( |
1877 | + this.SavedConnectionService.SavedConnections |
1878 | + .Where(c => !c.IsDefault) |
1879 | + .OrderBy(c => c.Name)); |
1880 | + } |
1881 | + |
1882 | + private void WriteConnectionInfo(SavedConnection connection) |
1883 | + { |
1884 | + this.MessageService.WriteLine(string.Format(" Connection String: {0}", connection.ConnectionString)); |
1885 | + this.MessageService.WriteLine(string.Format(" Driver Class: {0}", connection.DriverClass)); |
1886 | + this.MessageService.WriteLine(string.Format(" Provider: {0}", connection.ConnectionProvider)); |
1887 | + this.MessageService.WriteLine(string.Format(" Dialect: {0}", connection.Dialect)); |
1888 | + this.MessageService.WriteLine(string.Format(" Default: {0}", connection.IsDefault)); |
1889 | + this.MessageService.WriteLine(); |
1890 | + } |
1891 | + } |
1892 | +} |
1893 | + |
1894 | |
1895 | === modified file 'src/DatabaseVersion.Console/dbversion.Console.csproj' |
1896 | --- src/DatabaseVersion.Console/dbversion.Console.csproj 2011-10-25 21:46:32 +0000 |
1897 | +++ src/DatabaseVersion.Console/dbversion.Console.csproj 2011-11-15 22:04:37 +0000 |
1898 | @@ -13,7 +13,7 @@ |
1899 | <FileAlignment>512</FileAlignment> |
1900 | </PropertyGroup> |
1901 | <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' "> |
1902 | - <PlatformTarget>x86</PlatformTarget> |
1903 | + <PlatformTarget>AnyCPU</PlatformTarget> |
1904 | <DebugSymbols>true</DebugSymbols> |
1905 | <DebugType>full</DebugType> |
1906 | <Optimize>false</Optimize> |
1907 | @@ -21,10 +21,10 @@ |
1908 | <DefineConstants>DEBUG;TRACE</DefineConstants> |
1909 | <ErrorReport>prompt</ErrorReport> |
1910 | <WarningLevel>4</WarningLevel> |
1911 | - <Commandlineparameters>create -c "Server=localhost;Database=library;User ID=adam;Password=adam;" -a /home/adam/bzr/dbversion/console-command/samples/classic-sample.zip -v 16.0</Commandlineparameters> |
1912 | + <Commandlineparameters>create -a "/Users/adamconnelly/bzr/dbversion/saved-connection/samples/classic-sample"</Commandlineparameters> |
1913 | </PropertyGroup> |
1914 | <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' "> |
1915 | - <PlatformTarget>x86</PlatformTarget> |
1916 | + <PlatformTarget>AnyCPU</PlatformTarget> |
1917 | <DebugType>pdbonly</DebugType> |
1918 | <Optimize>true</Optimize> |
1919 | <OutputPath>bin\Release\</OutputPath> |
1920 | @@ -54,7 +54,6 @@ |
1921 | </Reference> |
1922 | </ItemGroup> |
1923 | <ItemGroup> |
1924 | - <Compile Include="Arguments.cs" /> |
1925 | <Compile Include="ConsoleTaskExecuter.cs" /> |
1926 | <Compile Include="Program.cs" /> |
1927 | <Compile Include="Properties\AssemblyInfo.cs" /> |
1928 | @@ -71,6 +70,11 @@ |
1929 | <Compile Include="Command\History\HistoryCommand.cs" /> |
1930 | <Compile Include="Command\History\HistoryArguments.cs" /> |
1931 | <Compile Include="Command\History\HistoryOrder.cs" /> |
1932 | + <Compile Include="Command\SavedConnection\SavedConnectionCommand.cs" /> |
1933 | + <Compile Include="Command\SavedConnection\SavedConnectionArguments.cs" /> |
1934 | + <Compile Include="Command\ConnectionArguments.cs" /> |
1935 | + <Compile Include="Command\ConnectionCommandBase.cs" /> |
1936 | + <Compile Include="Command\Create\CreateArguments.cs" /> |
1937 | </ItemGroup> |
1938 | <Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" /> |
1939 | <!-- To modify your build process, add your task inside one of the targets below and uncomment it. |
1940 | @@ -86,16 +90,17 @@ |
1941 | <None Include="scripts\dbversion" /> |
1942 | </ItemGroup> |
1943 | <ItemGroup> |
1944 | + <Folder Include="Command\SavedConnection\" /> |
1945 | <Folder Include="Command\History\" /> |
1946 | <Folder Include="scripts\" /> |
1947 | </ItemGroup> |
1948 | + <Target Name="AfterBuild"> |
1949 | + <Copy SourceFiles="scripts\dbversion" DestinationFolder="$(OutputPath)" /> |
1950 | + </Target> |
1951 | <ItemGroup> |
1952 | <ProjectReference Include="..\DatabaseVersion\dbversion.csproj"> |
1953 | <Project>{694D9BDF-DCE8-4FC6-A416-CE4573F2F00C}</Project> |
1954 | <Name>dbversion</Name> |
1955 | </ProjectReference> |
1956 | </ItemGroup> |
1957 | - <Target Name="AfterBuild"> |
1958 | - <Copy SourceFiles="scripts\dbversion" DestinationFolder="$(OutputPath)" /> |
1959 | - </Target> |
1960 | -</Project> |
1961 | \ No newline at end of file |
1962 | +</Project> |
1963 | |
1964 | === added directory 'src/DatabaseVersion.Tests/Connections' |
1965 | === added file 'src/DatabaseVersion.Tests/Connections/SavedConnectionServiceTests.cs' |
1966 | --- src/DatabaseVersion.Tests/Connections/SavedConnectionServiceTests.cs 1970-01-01 00:00:00 +0000 |
1967 | +++ src/DatabaseVersion.Tests/Connections/SavedConnectionServiceTests.cs 2011-11-15 22:04:37 +0000 |
1968 | @@ -0,0 +1,313 @@ |
1969 | +namespace DatabaseVersion.Tests.Connections |
1970 | +{ |
1971 | + using System.Collections.Generic; |
1972 | + using System.Linq; |
1973 | + |
1974 | + using dbversion.Connections; |
1975 | + using dbversion.Settings; |
1976 | + |
1977 | + using Moq; |
1978 | + |
1979 | + using Xunit; |
1980 | + |
1981 | + public class SavedConnectionServiceTests |
1982 | + { |
1983 | + #region Default Connection |
1984 | + [Fact] |
1985 | + public void ShouldMakeFirstSavedConnectionTheDefault() |
1986 | + { |
1987 | + // Arrange |
1988 | + var service = new SavedConnectionService(); |
1989 | + |
1990 | + // Act |
1991 | + var connection = service.CreateSavedConnection( |
1992 | + "library", "conn", "provider", "driverClass", "dialect"); |
1993 | + |
1994 | + // Assert |
1995 | + Assert.True(connection.IsDefault); |
1996 | + } |
1997 | + |
1998 | + [Fact] |
1999 | + public void ShouldNotMakeOtherConnectionsTheDefault() |
2000 | + { |
2001 | + // Arrange |
2002 | + var service = new SavedConnectionService(); |
2003 | + |
2004 | + service.CreateSavedConnection("a", "conn", "provider", "driverClass", "dialect"); |
2005 | + |
2006 | + // Act |
2007 | + var connection = service.CreateSavedConnection("b", "conn", "provider", "driverClass", "dialect"); |
2008 | + |
2009 | + // Assert |
2010 | + Assert.False(connection.IsDefault); |
2011 | + } |
2012 | + |
2013 | + [Fact] |
2014 | + public void ShouldReturnTheDefaultConnection() |
2015 | + { |
2016 | + // Arrange |
2017 | + var service = new SavedConnectionService(); |
2018 | + var settingsService = new Mock<ISettingsService>(); |
2019 | + |
2020 | + service.SettingsService = settingsService.Object; |
2021 | + |
2022 | + var savedConnections = new List<SavedConnection>() |
2023 | + { |
2024 | + new SavedConnection { Name = "connection1", IsDefault = false }, |
2025 | + new SavedConnection { Name = "connection2", IsDefault = true }, |
2026 | + new SavedConnection { Name = "connection3", IsDefault = false } |
2027 | + }; |
2028 | + |
2029 | + settingsService.Setup( |
2030 | + s => s.DeSerialize<List<SavedConnection>>(SavedConnectionService.SettingsFileName)) |
2031 | + .Returns(savedConnections); |
2032 | + |
2033 | + service.LoadConnections(); |
2034 | + |
2035 | + // Act |
2036 | + var defaultConnection = service.DefaultConnection; |
2037 | + |
2038 | + // Assert |
2039 | + Assert.Equal("connection2", defaultConnection.Name); |
2040 | + } |
2041 | + |
2042 | + [Fact] |
2043 | + public void ShouldReturnNullDefaultConnectionIfThereIsNoDefaultConnection() |
2044 | + { |
2045 | + // Arrange |
2046 | + var service = new SavedConnectionService(); |
2047 | + var settingsService = new Mock<ISettingsService>(); |
2048 | + |
2049 | + service.SettingsService = settingsService.Object; |
2050 | + |
2051 | + var savedConnections = new List<SavedConnection>() |
2052 | + { |
2053 | + new SavedConnection { Name = "connection1", IsDefault = false } |
2054 | + }; |
2055 | + |
2056 | + settingsService.Setup( |
2057 | + s => s.DeSerialize<List<SavedConnection>>(SavedConnectionService.SettingsFileName)) |
2058 | + .Returns(savedConnections); |
2059 | + |
2060 | + service.LoadConnections(); |
2061 | + |
2062 | + // Act |
2063 | + var defaultConnection = service.DefaultConnection; |
2064 | + |
2065 | + // Assert |
2066 | + Assert.Null(defaultConnection); |
2067 | + } |
2068 | + #endregion |
2069 | + |
2070 | + #region Connection Properties |
2071 | + [Fact] |
2072 | + public void ShouldSetConnectionNameWhenCreatingConnection() |
2073 | + { |
2074 | + // Arrange |
2075 | + var service = new SavedConnectionService(); |
2076 | + |
2077 | + // Act |
2078 | + var connection = service.CreateSavedConnection("connection1", "conn", "provider", "driverClass", "dialect"); |
2079 | + |
2080 | + // Assert |
2081 | + Assert.Equal("connection1", connection.Name); |
2082 | + } |
2083 | + |
2084 | + [Fact] |
2085 | + public void ShouldSetConnectionStringWhenCreatingConnection() |
2086 | + { |
2087 | + // Arrange |
2088 | + var service = new SavedConnectionService(); |
2089 | + |
2090 | + // Act |
2091 | + var connection = service.CreateSavedConnection("connection1", "conn", "provider", "driverClass", "dialect"); |
2092 | + |
2093 | + // Assert |
2094 | + Assert.Equal("conn", connection.ConnectionString); |
2095 | + } |
2096 | + |
2097 | + [Fact] |
2098 | + public void ShouldSetConnectionProviderWhenCreatingConnection() |
2099 | + { |
2100 | + // Arrange |
2101 | + var service = new SavedConnectionService(); |
2102 | + |
2103 | + // Act |
2104 | + var connection = service.CreateSavedConnection("connection1", "conn", "provider", "driverClass", "dialect"); |
2105 | + |
2106 | + // Assert |
2107 | + Assert.Equal("provider", connection.ConnectionProvider); |
2108 | + } |
2109 | + |
2110 | + [Fact] |
2111 | + public void ShouldSetDriverClassWhenCreatingConnection() |
2112 | + { |
2113 | + // Arrange |
2114 | + var service = new SavedConnectionService(); |
2115 | + |
2116 | + // Act |
2117 | + var connection = service.CreateSavedConnection("connection1", "conn", "provider", "driverClass", "dialect"); |
2118 | + |
2119 | + // Assert |
2120 | + Assert.Equal("driverClass", connection.DriverClass); |
2121 | + } |
2122 | + |
2123 | + [Fact] |
2124 | + public void ShouldSetDialectWhenCreatingConnection() |
2125 | + { |
2126 | + // Arrange |
2127 | + var service = new SavedConnectionService(); |
2128 | + |
2129 | + // Act |
2130 | + var connection = service.CreateSavedConnection("connection1", "conn", "provider", "driverClass", "dialect"); |
2131 | + |
2132 | + // Assert |
2133 | + Assert.Equal("dialect", connection.Dialect); |
2134 | + } |
2135 | + #endregion |
2136 | + |
2137 | + [Fact] |
2138 | + public void ShouldReplaceConnectionIfCreatingConnectionWithSameName() |
2139 | + { |
2140 | + // Arrange |
2141 | + var service = new SavedConnectionService(); |
2142 | + |
2143 | + // Act |
2144 | + service.CreateSavedConnection("connection1", null, null, null, null); |
2145 | + var connection = service.CreateSavedConnection("connection1", "conn", "provider", "driverClass", "dialect"); |
2146 | + |
2147 | + // Assert |
2148 | + Assert.Equal(1, service.SavedConnections.Count()); |
2149 | + Assert.Same(connection, service.SavedConnections.Single()); |
2150 | + } |
2151 | + |
2152 | + #region Save |
2153 | + [Fact] |
2154 | + public void ShouldBeAbleToSaveConnections() |
2155 | + { |
2156 | + // Arrange |
2157 | + var service = new SavedConnectionService(); |
2158 | + var settingsService = new Mock<ISettingsService>(); |
2159 | + |
2160 | + service.SettingsService = settingsService.Object; |
2161 | + |
2162 | + List<SavedConnection> expectedConnections = new List<SavedConnection>(); |
2163 | + expectedConnections.Add(service.CreateSavedConnection("a", null, null, null, null)); |
2164 | + expectedConnections.Add(service.CreateSavedConnection("b", null, null, null, null)); |
2165 | + |
2166 | + // Act |
2167 | + service.SaveConnections(); |
2168 | + |
2169 | + // Assert |
2170 | + settingsService.Verify(s => s.Serialize(expectedConnections, "saved-connections.xml")); |
2171 | + } |
2172 | + #endregion |
2173 | + |
2174 | + #region Load |
2175 | + [Fact] |
2176 | + public void ShouldBeAbleToLoadSavedConnections() |
2177 | + { |
2178 | + // Arrange |
2179 | + var service = new SavedConnectionService(); |
2180 | + var settingsService = new Mock<ISettingsService>(); |
2181 | + |
2182 | + service.SettingsService = settingsService.Object; |
2183 | + |
2184 | + List<SavedConnection> expectedConnections = new List<SavedConnection>(); |
2185 | + expectedConnections.Add(new SavedConnection("a", null, null, null, null, true)); |
2186 | + expectedConnections.Add(new SavedConnection("b", null, null, null, null, false)); |
2187 | + |
2188 | + settingsService.Setup( |
2189 | + s => s.DeSerialize<List<SavedConnection>>("saved-connections.xml")).Returns(expectedConnections); |
2190 | + |
2191 | + // Act |
2192 | + service.LoadConnections(); |
2193 | + |
2194 | + // Assert |
2195 | + Assert.Equal(expectedConnections, service.SavedConnections); |
2196 | + } |
2197 | + |
2198 | + [Fact] |
2199 | + public void ShouldHaveNoConnectionsIfNoConnectionsAreSavedInSettings() |
2200 | + { |
2201 | + // Arrange |
2202 | + var service = new SavedConnectionService(); |
2203 | + var settingsService = new Mock<ISettingsService>(); |
2204 | + |
2205 | + service.SettingsService = settingsService.Object; |
2206 | + |
2207 | + settingsService.Setup( |
2208 | + s => s.DeSerialize<List<SavedConnection>>("saved-connections.xml")).Returns((List<SavedConnection>)null); |
2209 | + |
2210 | + // Act |
2211 | + service.LoadConnections(); |
2212 | + |
2213 | + // Assert |
2214 | + Assert.Empty(service.SavedConnections); |
2215 | + } |
2216 | + #endregion |
2217 | + |
2218 | + [Fact] |
2219 | + public void ShouldBeAbleToDeleteASavedConnection() |
2220 | + { |
2221 | + // Arrange |
2222 | + var service = new SavedConnectionService(); |
2223 | + |
2224 | + service.CreateSavedConnection("a", null, null, null, null); |
2225 | + service.CreateSavedConnection("b", null, null, null, null); |
2226 | + service.CreateSavedConnection("c", null, null, null, null); |
2227 | + |
2228 | + // Act |
2229 | + service.DeleteConnection("b"); |
2230 | + |
2231 | + // Assert |
2232 | + Assert.Equal(2, service.SavedConnections.Count()); |
2233 | + Assert.Null(service.SavedConnections.SingleOrDefault(c => c.Name == "b")); |
2234 | + } |
2235 | + |
2236 | + #region Default |
2237 | + |
2238 | + [Fact] |
2239 | + public void ShouldBeAbleToSetConnectionAsDefault() |
2240 | + { |
2241 | + // Arrange |
2242 | + var service = new SavedConnectionService(); |
2243 | + |
2244 | + var connectionA = service.CreateSavedConnection("a", null, null, null, null); |
2245 | + var connectionB = service.CreateSavedConnection("b", null, null, null, null); |
2246 | + var connectionC = service.CreateSavedConnection("c", null, null, null, null); |
2247 | + |
2248 | + // Act |
2249 | + var success = service.SetDefaultConnection("b"); |
2250 | + |
2251 | + // Assert |
2252 | + Assert.True(connectionB.IsDefault, "Connection B should be the default"); |
2253 | + Assert.False(connectionA.IsDefault, "Connection A should not be default"); |
2254 | + Assert.False(connectionC.IsDefault, "Connection C should not be default"); |
2255 | + |
2256 | + Assert.True(success, "The method should have succeeded"); |
2257 | + } |
2258 | + |
2259 | + [Fact] |
2260 | + public void ShouldReturnFalseIfConnectionToDefaultDoesNotExist() |
2261 | + { |
2262 | + // Arrange |
2263 | + var service = new SavedConnectionService(); |
2264 | + |
2265 | + var connectionA = service.CreateSavedConnection("a", null, null, null, null); |
2266 | + service.CreateSavedConnection("b", null, null, null, null); |
2267 | + service.CreateSavedConnection("c", null, null, null, null); |
2268 | + |
2269 | + // Act |
2270 | + var success = service.SetDefaultConnection("ZZZ"); |
2271 | + |
2272 | + // Assert |
2273 | + Assert.True(connectionA.IsDefault, "Connection A should still be default"); |
2274 | + |
2275 | + Assert.False(success, "The method should not have succeeded"); |
2276 | + } |
2277 | + |
2278 | + #endregion |
2279 | + } |
2280 | +} |
2281 | + |
2282 | |
2283 | === modified file 'src/DatabaseVersion.sln' |
2284 | --- src/DatabaseVersion.sln 2011-10-17 08:44:54 +0000 |
2285 | +++ src/DatabaseVersion.sln 2011-11-15 22:04:37 +0000 |
2286 | @@ -1,4 +1,4 @@ |
2287 | - |
2288 | + |
2289 | Microsoft Visual Studio Solution File, Format Version 11.00 |
2290 | # Visual Studio 2010 |
2291 | Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "dbversion", "DatabaseVersion\dbversion.csproj", "{694D9BDF-DCE8-4FC6-A416-CE4573F2F00C}" |
2292 | |
2293 | === added directory 'src/DatabaseVersion/Connections' |
2294 | === added file 'src/DatabaseVersion/Connections/ISavedConnectionService.cs' |
2295 | --- src/DatabaseVersion/Connections/ISavedConnectionService.cs 1970-01-01 00:00:00 +0000 |
2296 | +++ src/DatabaseVersion/Connections/ISavedConnectionService.cs 2011-11-15 22:04:37 +0000 |
2297 | @@ -0,0 +1,92 @@ |
2298 | +namespace dbversion.Connections |
2299 | +{ |
2300 | + using System.Collections.Generic; |
2301 | + |
2302 | + using dbversion.Settings; |
2303 | + |
2304 | + /// <summary> |
2305 | + /// Used to manage the saved connections. |
2306 | + /// </summary> |
2307 | + public interface ISavedConnectionService |
2308 | + { |
2309 | + /// <summary> |
2310 | + /// Gets the saved connections. |
2311 | + /// </summary> |
2312 | + /// <value> |
2313 | + /// The saved connections. |
2314 | + /// </value> |
2315 | + IEnumerable<SavedConnection> SavedConnections |
2316 | + { |
2317 | + get; |
2318 | + } |
2319 | + |
2320 | + /// <summary> |
2321 | + /// Gets the default connection. |
2322 | + /// </summary> |
2323 | + /// <value> |
2324 | + /// The default connection. |
2325 | + /// </value> |
2326 | + SavedConnection DefaultConnection |
2327 | + { |
2328 | + get; |
2329 | + } |
2330 | + |
2331 | + /// <summary> |
2332 | + /// Creates a new saved connection or updates an existing saved connection if a connection |
2333 | + /// with the specified name already exists. |
2334 | + /// </summary> |
2335 | + /// <param name='name'> |
2336 | + /// The name of the saved connection. |
2337 | + /// </param> |
2338 | + /// <param name='connectionString'> |
2339 | + /// The connection string. |
2340 | + /// </param> |
2341 | + /// <param name='provider'> |
2342 | + /// The connection provider. |
2343 | + /// </param> |
2344 | + /// <param name='driverClass'> |
2345 | + /// The driver class. |
2346 | + /// </param> |
2347 | + /// <param name='dialect'> |
2348 | + /// The dialect. |
2349 | + /// </param> |
2350 | + /// <returns> |
2351 | + /// The saved connection. |
2352 | + /// </returns> |
2353 | + SavedConnection CreateSavedConnection( |
2354 | + string name, string connectionString, string provider, string driverClass, string dialect); |
2355 | + |
2356 | + /// <summary> |
2357 | + /// Saves the connections to settings. |
2358 | + /// </summary> |
2359 | + void SaveConnections(); |
2360 | + |
2361 | + /// <summary> |
2362 | + /// Loads the connections from settings. |
2363 | + /// </summary> |
2364 | + void LoadConnections(); |
2365 | + |
2366 | + /// <summary> |
2367 | + /// Deletes the connection. |
2368 | + /// </summary> |
2369 | + /// <param name='name'> |
2370 | + /// The connection name. |
2371 | + /// </param> |
2372 | + /// <returns> |
2373 | + /// <c>true</c> if the connection was deleted; otherwise <c>false</c>. |
2374 | + /// </returns> |
2375 | + bool DeleteConnection(string name); |
2376 | + |
2377 | + /// <summary> |
2378 | + /// Sets the specified connection as the default. |
2379 | + /// </summary> |
2380 | + /// <param name='name'> |
2381 | + /// The name of the connection to set as default. |
2382 | + /// </param> |
2383 | + /// <returns> |
2384 | + /// <c>true</c> if the connection exists; otherwise <c>false</c>. |
2385 | + /// </returns> |
2386 | + bool SetDefaultConnection(string name); |
2387 | + } |
2388 | +} |
2389 | + |
2390 | |
2391 | === added file 'src/DatabaseVersion/Connections/SavedConnection.cs' |
2392 | --- src/DatabaseVersion/Connections/SavedConnection.cs 1970-01-01 00:00:00 +0000 |
2393 | +++ src/DatabaseVersion/Connections/SavedConnection.cs 2011-11-15 22:04:37 +0000 |
2394 | @@ -0,0 +1,150 @@ |
2395 | +namespace dbversion.Connections |
2396 | +{ |
2397 | + using System.Collections.Generic; |
2398 | + |
2399 | + using dbversion.Property; |
2400 | + using dbversion.Session; |
2401 | + |
2402 | + public class SavedConnection |
2403 | + { |
2404 | + public SavedConnection() |
2405 | + { |
2406 | + } |
2407 | + |
2408 | + public SavedConnection( |
2409 | + string name, string connectionString, string connectionProvider, string driverClass, string dialect, bool isDefault) |
2410 | + { |
2411 | + this.Name = name; |
2412 | + this.ConnectionString = connectionString; |
2413 | + this.ConnectionProvider = connectionProvider; |
2414 | + this.DriverClass = driverClass; |
2415 | + this.Dialect = dialect; |
2416 | + this.IsDefault = isDefault; |
2417 | + } |
2418 | + |
2419 | + /// <summary> |
2420 | + /// Gets or sets a value indicating whether this is the default connection. |
2421 | + /// </summary> |
2422 | + /// <value> |
2423 | + /// <c>true</c> if this instance is default; otherwise, <c>false</c>. |
2424 | + /// </value> |
2425 | + public bool IsDefault |
2426 | + { |
2427 | + get; |
2428 | + set; |
2429 | + } |
2430 | + |
2431 | + /// <summary> |
2432 | + /// Gets or sets the name of the connection. |
2433 | + /// </summary> |
2434 | + /// <value> |
2435 | + /// The name. |
2436 | + /// </value> |
2437 | + public string Name |
2438 | + { |
2439 | + get; |
2440 | + set; |
2441 | + } |
2442 | + |
2443 | + /// <summary> |
2444 | + /// Gets or sets the connection string. |
2445 | + /// </summary> |
2446 | + /// <value> |
2447 | + /// The connection string. |
2448 | + /// </value> |
2449 | + public string ConnectionString |
2450 | + { |
2451 | + get; |
2452 | + set; |
2453 | + } |
2454 | + |
2455 | + /// <summary> |
2456 | + /// Gets or sets the connection provider. |
2457 | + /// </summary> |
2458 | + /// <value> |
2459 | + /// The connection provider. |
2460 | + /// </value> |
2461 | + public string ConnectionProvider |
2462 | + { |
2463 | + get; |
2464 | + set; |
2465 | + } |
2466 | + |
2467 | + /// <summary> |
2468 | + /// Gets or sets the driver class. |
2469 | + /// </summary> |
2470 | + /// <value> |
2471 | + /// The driver class. |
2472 | + /// </value> |
2473 | + public string DriverClass |
2474 | + { |
2475 | + get; |
2476 | + set; |
2477 | + } |
2478 | + |
2479 | + /// <summary> |
2480 | + /// Gets or sets the dialect. |
2481 | + /// </summary> |
2482 | + /// <value> |
2483 | + /// The dialect. |
2484 | + /// </value> |
2485 | + public string Dialect |
2486 | + { |
2487 | + get; |
2488 | + set; |
2489 | + } |
2490 | + |
2491 | + /// <summary> |
2492 | + /// Makes a copy of the connection. |
2493 | + /// </summary> |
2494 | + public SavedConnection Copy() |
2495 | + { |
2496 | + return new SavedConnection( |
2497 | + this.Name, this.ConnectionString, this.ConnectionProvider, this.DriverClass, this.Dialect, this.IsDefault); |
2498 | + } |
2499 | + |
2500 | + /// <summary> |
2501 | + /// Returns the set of connection properties based on the values in this instance. |
2502 | + /// These properties can be used by the <see cref="SessionFactoryProvider"/> to |
2503 | + /// create a session factory. |
2504 | + /// </summary> |
2505 | + /// <returns> |
2506 | + /// The connection properties. |
2507 | + /// </returns> |
2508 | + public IEnumerable<Property> GetConnectionProperties() |
2509 | + { |
2510 | + if (!string.IsNullOrEmpty(this.ConnectionString)) |
2511 | + { |
2512 | + yield return new Property |
2513 | + { |
2514 | + Key = SessionFactoryProvider.ConnectionStringProperty, Value = this.ConnectionString |
2515 | + }; |
2516 | + } |
2517 | + |
2518 | + if (!string.IsNullOrEmpty(this.Dialect)) |
2519 | + { |
2520 | + yield return new Property |
2521 | + { |
2522 | + Key = SessionFactoryProvider.DialectProperty, Value = this.Dialect |
2523 | + }; |
2524 | + } |
2525 | + |
2526 | + if (!string.IsNullOrEmpty(this.DriverClass)) |
2527 | + { |
2528 | + yield return new Property |
2529 | + { |
2530 | + Key = SessionFactoryProvider.DriverClassProperty, Value = this.DriverClass |
2531 | + }; |
2532 | + } |
2533 | + |
2534 | + if (!string.IsNullOrEmpty(this.ConnectionProvider)) |
2535 | + { |
2536 | + yield return new Property |
2537 | + { |
2538 | + Key = SessionFactoryProvider.ProviderProperty, Value = this.ConnectionProvider |
2539 | + }; |
2540 | + } |
2541 | + } |
2542 | + } |
2543 | +} |
2544 | + |
2545 | |
2546 | === added file 'src/DatabaseVersion/Connections/SavedConnectionService.cs' |
2547 | --- src/DatabaseVersion/Connections/SavedConnectionService.cs 1970-01-01 00:00:00 +0000 |
2548 | +++ src/DatabaseVersion/Connections/SavedConnectionService.cs 2011-11-15 22:04:37 +0000 |
2549 | @@ -0,0 +1,171 @@ |
2550 | +namespace dbversion.Connections |
2551 | +{ |
2552 | + using System.Collections.Generic; |
2553 | + using System.ComponentModel.Composition; |
2554 | + using System.Linq; |
2555 | + |
2556 | + using dbversion.Settings; |
2557 | + |
2558 | + /// <summary> |
2559 | + /// Used to manage the saved connections. |
2560 | + /// </summary> |
2561 | + [Export(typeof(ISavedConnectionService))] |
2562 | + public class SavedConnectionService : ISavedConnectionService |
2563 | + { |
2564 | + /// <summary> |
2565 | + /// The name of the file to store the saved connections in. |
2566 | + /// </summary> |
2567 | + public const string SettingsFileName = "saved-connections.xml"; |
2568 | + |
2569 | + /// <summary> |
2570 | + /// The saved connections. |
2571 | + /// </summary> |
2572 | + private List<SavedConnection> savedConnections = new List<SavedConnection>(); |
2573 | + |
2574 | + /// <summary> |
2575 | + /// Gets or sets the settings service. |
2576 | + /// </summary> |
2577 | + /// <value> |
2578 | + /// The settings service. |
2579 | + /// </value> |
2580 | + [Import] |
2581 | + public ISettingsService SettingsService |
2582 | + { |
2583 | + get; |
2584 | + set; |
2585 | + } |
2586 | + |
2587 | + /// <summary> |
2588 | + /// Gets the saved connections. |
2589 | + /// </summary> |
2590 | + /// <value> |
2591 | + /// The saved connections. |
2592 | + /// </value> |
2593 | + public IEnumerable<SavedConnection> SavedConnections |
2594 | + { |
2595 | + get |
2596 | + { |
2597 | + return this.savedConnections; |
2598 | + } |
2599 | + } |
2600 | + |
2601 | + public SavedConnection DefaultConnection |
2602 | + { |
2603 | + get |
2604 | + { |
2605 | + return this.savedConnections.FirstOrDefault(c => c.IsDefault); |
2606 | + } |
2607 | + } |
2608 | + |
2609 | + /// <summary> |
2610 | + /// Creates a new saved connection or updates an existing saved connection if a connection |
2611 | + /// with the specified name already exists. |
2612 | + /// </summary> |
2613 | + /// <param name='name'> |
2614 | + /// The name of the saved connection. |
2615 | + /// </param> |
2616 | + /// <param name='connectionString'> |
2617 | + /// The connection string. |
2618 | + /// </param> |
2619 | + /// <param name='provider'> |
2620 | + /// The connection provider. |
2621 | + /// </param> |
2622 | + /// <param name='driverClass'> |
2623 | + /// The driver class. |
2624 | + /// </param> |
2625 | + /// <param name='dialect'> |
2626 | + /// The dialect. |
2627 | + /// </param> |
2628 | + /// <returns> |
2629 | + /// The saved connection. |
2630 | + /// </returns> |
2631 | + public SavedConnection CreateSavedConnection( |
2632 | + string name, string connectionString, string provider, string driverClass, string dialect) |
2633 | + { |
2634 | + var isDefault = this.savedConnections.Count == 0; |
2635 | + |
2636 | + var connection = this.SavedConnections.FirstOrDefault(c => c.Name == name); |
2637 | + if (connection != null) |
2638 | + { |
2639 | + connection.ConnectionString = connectionString; |
2640 | + connection.ConnectionProvider = provider; |
2641 | + connection.DriverClass = driverClass; |
2642 | + connection.Dialect = dialect; |
2643 | + } |
2644 | + else |
2645 | + { |
2646 | + connection = new SavedConnection(name, connectionString, provider, driverClass, dialect, isDefault); |
2647 | + this.savedConnections.Add(connection); |
2648 | + } |
2649 | + |
2650 | + return connection; |
2651 | + } |
2652 | + |
2653 | + /// <summary> |
2654 | + /// Saves the connections to settings. |
2655 | + /// </summary> |
2656 | + public void SaveConnections() |
2657 | + { |
2658 | + this.SettingsService.Serialize(savedConnections, SettingsFileName); |
2659 | + } |
2660 | + |
2661 | + /// <summary> |
2662 | + /// Loads the connections from settings. |
2663 | + /// </summary> |
2664 | + public void LoadConnections() |
2665 | + { |
2666 | + var connections = this.SettingsService.DeSerialize<List<SavedConnection>>(SettingsFileName); |
2667 | + if (connections != null) |
2668 | + { |
2669 | + this.savedConnections = connections; |
2670 | + } |
2671 | + else |
2672 | + { |
2673 | + this.savedConnections = new List<SavedConnection>(); |
2674 | + } |
2675 | + } |
2676 | + |
2677 | + /// <summary> |
2678 | + /// Deletes the connection. |
2679 | + /// </summary> |
2680 | + /// <param name='name'> |
2681 | + /// The name of the connection to delete. |
2682 | + /// </param> |
2683 | + /// <returns> |
2684 | + /// <c>true</c> if the connection was deleted; otherwise <c>false</c>. |
2685 | + /// </returns> |
2686 | + public bool DeleteConnection(string name) |
2687 | + { |
2688 | + var connection = this.savedConnections.FirstOrDefault(c => c.Name == name); |
2689 | + if (connection != null) |
2690 | + { |
2691 | + return this.savedConnections.Remove(connection); |
2692 | + } |
2693 | + |
2694 | + return false; |
2695 | + } |
2696 | + |
2697 | + /// <summary> |
2698 | + /// Sets the specified connection as the default. |
2699 | + /// </summary> |
2700 | + /// <param name='name'> |
2701 | + /// The name of the connection to set as default. |
2702 | + /// </param> |
2703 | + /// <returns> |
2704 | + /// <c>true</c> if the connection exists; otherwise <c>false</c>. |
2705 | + /// </returns> |
2706 | + public bool SetDefaultConnection(string name) |
2707 | + { |
2708 | + var connection = this.savedConnections.FirstOrDefault(c => c.Name == name); |
2709 | + if (connection != null) |
2710 | + { |
2711 | + this.savedConnections.ForEach(c => c.IsDefault = false); |
2712 | + connection.IsDefault = true; |
2713 | + return true; |
2714 | + } |
2715 | + |
2716 | + return false; |
2717 | + } |
2718 | + } |
2719 | +} |
2720 | + |
2721 | |
2722 | === modified file 'src/DatabaseVersion/Settings/SettingsService.cs' |
2723 | --- src/DatabaseVersion/Settings/SettingsService.cs 2011-10-25 21:44:23 +0000 |
2724 | +++ src/DatabaseVersion/Settings/SettingsService.cs 2011-11-15 22:04:37 +0000 |
2725 | @@ -117,7 +117,19 @@ |
2726 | |
2727 | if (stream != null) |
2728 | { |
2729 | - return XmlSerializer.DeSerialize<T>(stream); |
2730 | + try |
2731 | + { |
2732 | + return XmlSerializer.DeSerialize<T>(stream); |
2733 | + } |
2734 | + catch (Exception) |
2735 | + { |
2736 | + // TODO: Add logging? |
2737 | + return default(T); |
2738 | + } |
2739 | + finally |
2740 | + { |
2741 | + stream.Dispose(); |
2742 | + } |
2743 | } |
2744 | |
2745 | return default(T); |
2746 | |
2747 | === modified file 'src/DatabaseVersion/dbversion.csproj' |
2748 | --- src/DatabaseVersion/dbversion.csproj 2011-10-25 21:46:32 +0000 |
2749 | +++ src/DatabaseVersion/dbversion.csproj 2011-11-15 22:04:37 +0000 |
2750 | @@ -112,6 +112,9 @@ |
2751 | <Compile Include="..\CommonAssemblyInfo.cs"> |
2752 | <Link>Properties\CommonAssemblyInfo.cs</Link> |
2753 | </Compile> |
2754 | + <Compile Include="Connections\ISavedConnectionService.cs" /> |
2755 | + <Compile Include="Connections\SavedConnection.cs" /> |
2756 | + <Compile Include="Connections\SavedConnectionService.cs" /> |
2757 | </ItemGroup> |
2758 | <Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" /> |
2759 | <!-- To modify your build process, add your task inside one of the targets below and uncomment it. |