public void CorrectlyAcceptsValidArguments()
        {
            var sut = new DeployArgs
            {
                ConnectionString = @"Server = (localdb)\mssqllocaldb; Initial Catalog = SomeTestDb; Integrated Security = true; Application Name = Galen.CI.Azure.Sql.Sharding.App.Tests; "
            };

            // should not throw exception
            // connection string by itself is valid
            sut.Validate();

            // should not throw exception
            // connection string w/both a login name and password is valid when not using Windows login
            sut.LoginName = "MySqlLogin";
            sut.LoginPassword = "******";
            sut.Validate();

            // should not throw exception
            // connection string w/login name and no password is valid when using Windows login
            sut.LoginPassword = null;
            sut.UseWindowsLogin = true;
            sut.Validate();

            // should not throw exception
            // connection string w/database user name is valid when login name is provided with no password and using Windows login
            sut.DatabaseUserName = "******";
            sut.Validate();

            // should not throw exception
            // connection string w/database user name is valid when login name and password are provided and not using Windows login
            sut.LoginPassword = "******";
            sut.UseWindowsLogin = false;
            sut.Validate();
        }
        public void FailsValidationWhenUsingWindowsLoginWithoutLoginName()
        {
            var sut = new DeployArgs
            {
                ConnectionString = @"Server = (localdb)\mssqllocaldb; Initial Catalog = SomeTestDb; Integrated Security = true; Application Name = Galen.CI.Azure.Sql.Sharding.App.Tests; ",
                UseWindowsLogin = true
            };

            sut.Validate();
        }
        public void Deploy(DeployArgs args)
        {
            args.Validate();

            var connectionStringBuilder = new SqlConnectionStringBuilder(args.ConnectionString);
            var targetDatabase = connectionStringBuilder.InitialCatalog;
            var targetServer = connectionStringBuilder.DataSource;

            connectionStringBuilder.InitialCatalog = string.Empty;
            var serverConnectionString = connectionStringBuilder.ConnectionString;

            Log.Information(LoggingMessageTemplates.CreateDatabase, targetDatabase, targetServer);
            SqlDatabaseUtilities.CreateDatabaseIfNotExists(targetDatabase, serverConnectionString);

            Log.Information(LoggingMessageTemplates.Deploy, targetDatabase, targetServer);

            var shardManagementService = new ShardMapManagementService(args.ConnectionString);
            shardManagementService.Deploy();

            if (args.HasLoginName && args.UseWindowsLogin)
            {
                Log.Information(LoggingMessageTemplates.CreateSqlLoginFromWindowsAccount, args.LoginName, targetServer);
                SqlDatabaseUtilities.CreateWindowsLoginIfNotExists(args.LoginName, serverConnectionString);
            }
            else if (args.HasLoginName)
            {
                Log.Information(LoggingMessageTemplates.CreateSqlLogin, args.LoginName, targetServer);
                SqlDatabaseUtilities.CreateSqlLoginIfNotExists(
                    args.LoginName,
                    args.LoginPassword,
                    serverConnectionString);
            }

            if (args.HasDatabaseUserName)
            {
                Log.Information(
                    LoggingMessageTemplates.CreateDatabaseUser, 
                    args.DatabaseUserName, 
                    targetDatabase,
                    args.LoginName, 
                    targetServer);

                SqlDatabaseUtilities.CreateDatabaseUserIfNotExists(
                    args.LoginName, 
                    args.DatabaseUserName,
                    args.ConnectionString);

                Log.Information(
                    LoggingMessageTemplates.GrantUserReadWritePermissions, 
                    args.DatabaseUserName,
                    targetDatabase, targetServer);

                SqlDatabaseUtilities.GrantUserDatabaseRequiredPermissions(args.DatabaseUserName, args.ConnectionString);
            }
        }
        public void FailsValidationWhenLoginNameProvidedWithoutPasswordAndNotUsingWindowsLogin()
        {
            var sut = new DeployArgs
            {
                ConnectionString = @"Server = (localdb)\mssqllocaldb; Initial Catalog = SomeTestDb; Integrated Security = true; Application Name = Galen.CI.Azure.Sql.Sharding.App.Tests; ",
                LoginName = "MySqlLogin"
            };

            sut.Validate();
        }
        public void FailsValidationWhenDatabaseUserNameProvidedWithoutLoginName()
        {
            var sut = new DeployArgs
            {
                ConnectionString = @"Server = (localdb)\mssqllocaldb; Initial Catalog = SomeTestDb; Integrated Security = true; Application Name = Galen.CI.Azure.Sql.Sharding.App.Tests; ",
                DatabaseUserName = "******"
            };

            sut.Validate();
        }