Skip to content

Commit 1609133

Browse files
authored
Merge branch 'master' into fix/backup-chain/server-name
2 parents e2dc3d8 + a4bf27b commit 1609133

5 files changed

Lines changed: 70 additions & 2 deletions

File tree

src/AgDatabase.cs

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ namespace AgDatabaseMove
1212
using System.Data.SqlClient;
1313
using System.Linq;
1414
using System.Threading;
15+
using Exceptions;
1516
using Polly;
1617
using SmoFacade;
1718

@@ -35,6 +36,7 @@ void Restore(IEnumerable<BackupMetadata> backupOrder, Func<int, TimeSpan> retryD
3536
void DropAllLogins();
3637
void AddRole(LoginProperties login, RoleProperties role);
3738
IEnumerable<RoleProperties> AssociatedRoles();
39+
void ContainsLogin(string loginName);
3840
}
3941

4042

@@ -233,5 +235,43 @@ public void MultiUserMode()
233235
{
234236
_listener.Primary.Database(Name).MultiUserMode();
235237
}
238+
239+
private void CheckLoginExists(Server server, AvailabilityGroup availabilityGroup, string loginName)
240+
{
241+
var matchingLogins = server.Logins.Where(l => l.Name == loginName);
242+
243+
if (matchingLogins.Count() == 0)
244+
throw new MissingLoginException($"Login missing on {server.Name}, {_listener.AvailabilityGroup.Name}, {loginName}");
245+
246+
if (matchingLogins.Count() > 1)
247+
throw new
248+
MultipleLoginException($"Multiple logins exist on {server.Name}, {_listener.AvailabilityGroup.Name}, {loginName}");
249+
250+
var sid = matchingLogins.First().Sid;
251+
if (sid == null || sid.Length == 0)
252+
throw new MissingSidException($"Sid missing on {server.Name}, {_listener.AvailabilityGroup.Name}, {loginName}");
253+
}
254+
255+
public void ContainsLogin(string loginName)
256+
{
257+
var exceptions = new ConcurrentQueue<Exception>();
258+
259+
_listener.ForEachAgInstance((s, ag) => {
260+
try {
261+
CheckLoginExists(s, ag, loginName);
262+
}
263+
catch(MissingLoginException ex) {
264+
exceptions.Enqueue(ex);
265+
}
266+
catch(MultipleLoginException ex) {
267+
exceptions.Enqueue(ex);
268+
}
269+
catch(MissingSidException ex) {
270+
exceptions.Enqueue(ex);
271+
}
272+
});
273+
274+
if(exceptions.Count > 0) throw new AggregateException(exceptions);
275+
}
236276
}
237277
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
namespace AgDatabaseMove.Exceptions
2+
{
3+
4+
public class MissingLoginException : AgDatabaseMoveException
5+
{
6+
public MissingLoginException(string message) : base(message) { }
7+
}
8+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
namespace AgDatabaseMove.Exceptions
2+
{
3+
4+
public class MissingSidException : AgDatabaseMoveException
5+
{
6+
public MissingSidException(string message) : base(message) { }
7+
}
8+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
namespace AgDatabaseMove.Exceptions
2+
{
3+
4+
public class MultipleLoginException : AgDatabaseMoveException
5+
{
6+
public MultipleLoginException(string message) : base(message) { }
7+
}
8+
}

src/SmoFacade/Login.cs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -104,11 +104,15 @@ private static Microsoft.SqlServer.Management.Smo.Login ConstructLogin(LoginProp
104104
{
105105
var login = new Microsoft.SqlServer.Management.Smo.Login(server._server, loginProperties.Name) {
106106
LoginType = loginProperties.LoginType,
107-
Sid = loginProperties.Sid,
108107
DefaultDatabase = loginProperties.DefaultDatabase
109108
};
110109

111-
if(loginProperties.LoginType == LoginType.SqlLogin) {
110+
if (loginProperties.Sid != null)
111+
{
112+
login.Sid = loginProperties.Sid;
113+
}
114+
115+
if (loginProperties.LoginType == LoginType.SqlLogin) {
112116
if(loginProperties.PasswordHash != null)
113117
login.Create(loginProperties.PasswordHash, LoginCreateOptions.IsHashed);
114118
else if(loginProperties.Password != null)

0 commit comments

Comments
 (0)