-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathAgDatabaseMove.cs
More file actions
executable file
·90 lines (70 loc) · 2.91 KB
/
Copy pathAgDatabaseMove.cs
File metadata and controls
executable file
·90 lines (70 loc) · 2.91 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
using System.Runtime.CompilerServices;
[assembly: InternalsVisibleTo("AgDatabaseMove.Integration")]
[assembly: InternalsVisibleTo("AgDatabaseMove.Unit")]
namespace AgDatabaseMove
{
using System;
using System.Linq;
using Exceptions;
using SmoFacade;
public class MoveOptions
{
public IAgDatabase Source { get; set; }
public IAgDatabase Destination { get; set; }
public bool Overwrite { get; set; }
public bool Finalize { get; set; }
public bool CopyLogins { get; set; }
public Func<int, TimeSpan> RetryDuration { get; set; }
public Func<string, string> FileRelocator { get; set; }
}
/// <summary>
/// Used to manage the restore process.
/// </summary>
public class AgDatabaseMove
{
internal readonly MoveOptions _options;
public AgDatabaseMove(MoveOptions options)
{
_options = options;
}
internal LoginProperties UpdateDefaultDb(LoginProperties loginProperties)
{
loginProperties.DefaultDatabase =
_options.Source.Name.Equals(loginProperties.DefaultDatabase, StringComparison.InvariantCultureIgnoreCase)
? _options.Destination.Name
: "master";
return loginProperties;
}
/// <summary>
/// AgDatabaseMove the database to all instances of the availability group.
/// To join the AG, Finalize must be set.
/// </summary>
/// <param name="lastLsn">The last restored LSN used to continue while in no recovery mode.</param>
/// <returns>The last LSN restored.</returns>
public decimal Move(decimal? lastLsn = null)
{
if(!_options.Overwrite && _options.Destination.Exists() && !_options.Destination.Restoring)
throw new ArgumentException("Database exists and overwrite option is not set");
if(_options.Overwrite && lastLsn == null)
_options.Destination.Delete();
if(lastLsn == null && _options.Destination.Restoring)
throw new
ArgumentException("lastLsn parameter can only be used if the Destination database is in a restoring state");
_options.Source.LogBackup();
var backupChain = new BackupChain(_options.Source);
var stripedBackupList = backupChain.OrderedBackups.ToList();
if(_options.Destination.Restoring && lastLsn != null)
stripedBackupList.RemoveAll(b => b.LastLsn <= lastLsn.Value);
if(!stripedBackupList.Any())
throw new BackupChainException("No backups found to restore");
_options.Destination.Restore(stripedBackupList, _options.RetryDuration, _options.FileRelocator);
if(_options.CopyLogins)
foreach(var loginProperty in _options.Source.AssociatedLogins().Select(UpdateDefaultDb))
_options.Destination.AddLogin(loginProperty);
if(_options.Finalize) {
_options.Destination.JoinAg();
}
return stripedBackupList.Max(bl => bl.LastLsn);
}
}
}