@@ -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}
0 commit comments