/// <summary> /// Updates the property on the database with the name <paramref name="databaseName"/>. /// </summary> /// <param name="databaseName">The database to update.</param> /// <param name="newDatabaseName"> /// The new database name, or <c>null</c> to not update. /// </param> /// <param name="databaseMaxSize"> /// The new database max size, or <c>null</c> to not update. /// </param> /// <param name="databaseEdition"> /// The new database edition, or <c>null</c> to not update. /// </param> /// <returns>The updated database object.</returns> public Database UpdateDatabase( string databaseName, string newDatabaseName, int?databaseMaxSize, DatabaseEdition?databaseEdition) { // Find the database by name Database database = GetDatabase(databaseName); // Update the name if specified if (newDatabaseName != null) { database.Name = newDatabaseName; } // Update the max size and edition properties database.MaxSizeGB = databaseMaxSize; database.Edition = databaseEdition == null ? null : databaseEdition.ToString(); database.IsRecursiveTriggersOn = null; // Mark the database object for update and submit the changes this.UpdateObject(database); try { this.SaveChanges(); } catch { this.RevertChanges(database); throw; } return(this.GetDatabase(database.Name)); }
/// <summary> /// Updates the property on the database with the name <paramref name="databaseName"/>. /// </summary> /// <param name="databaseName">The database to update.</param> /// <param name="newDatabaseName"> /// The new database name, or <c>null</c> to not update. /// </param> /// <param name="databaseMaxSize"> /// The new database max size, or <c>null</c> to not update. /// </param> /// <param name="databaseEdition"> /// The new database edition, or <c>null</c> to not update. /// </param> /// <param name="serviceObjective"> /// The new service objective, or <c>null</c> to not update. /// </param> /// <returns>The updated database object.</returns> public Database UpdateDatabase( string databaseName, string newDatabaseName, int?databaseMaxSizeGb, long?databaseMaxSizeBytes, DatabaseEdition?databaseEdition, ServiceObjective serviceObjective) { // Find the database by name Database database = GetDatabase(databaseName); // Update the name if specified if (newDatabaseName != null) { database.Name = newDatabaseName; } // Update the max size and edition properties if (databaseMaxSizeGb != null) { database.MaxSizeGB = (int)databaseMaxSizeGb; } if (databaseMaxSizeBytes != null) { database.MaxSizeBytes = (long)databaseMaxSizeBytes; } database.Edition = databaseEdition == null ? null : databaseEdition.ToString(); database.IsRecursiveTriggersOn = null; // Update the service objective property if specified if (serviceObjective != null) { database.ServiceObjectiveId = serviceObjective.Id; } else { database.ServiceObjectiveId = null; } // Mark the database object for update and submit the changes this.UpdateObject(database); try { this.SaveChanges(); } catch { this.RevertChanges(database); throw; } return(this.GetDatabase(database.Name)); }
/// <summary> /// Updates the property on the database with the name <paramref name="databaseName"/>. /// </summary> /// <param name="builder">The sql connection string information</param> /// <param name="databaseName">The database to update.</param> /// <param name="newDatabaseName">The new database name, or <c>null</c> to not update.</param> /// <param name="databaseMaxSizeGb">The max size for the database in GB.</param> /// <param name="databaseMaxSizeBytes">The max size for the database in bytes.</param> /// <param name="databaseEdition">The new database edition, or <c>null</c> to not update.</param> /// <param name="serviceObjective">The new service objective, or <c>null</c> to not update.</param> /// <returns>The updated database object.</returns> public Database UpdateDatabase( string databaseName, string newDatabaseName, int?databaseMaxSizeGb, long?databaseMaxSizeBytes, DatabaseEdition?databaseEdition, ServiceObjective serviceObjective) { return(UpdateDatabase( databaseName, newDatabaseName, databaseMaxSizeGb, databaseMaxSizeBytes, databaseEdition, serviceObjective == null ? null : serviceObjective.Name)); }
/// <summary> /// Updates the property on the database with the name <paramref name="databaseName"/>. /// </summary> /// <param name="builder">The sql connection string information</param> /// <param name="databaseName">The database to update.</param> /// <param name="newDatabaseName">The new database name, or <c>null</c> to not update.</param> /// <param name="databaseMaxSizeGb">The max size for the database in GB.</param> /// <param name="databaseMaxSizeBytes">The max size for the database in bytes.</param> /// <param name="databaseEdition">The new database edition, or <c>null</c> to not update.</param> /// <param name="serviceObjective">The new service objective name, or <c>null</c> to not update.</param> /// <returns>The updated database object.</returns> public Database UpdateDatabase( string databaseName, string newDatabaseName, int?databaseMaxSizeGb, long?databaseMaxSizeBytes, DatabaseEdition?databaseEdition, string serviceObjectiveName) { Database result = null; if (!string.IsNullOrEmpty(newDatabaseName)) { result = AlterDatabaseName(databaseName, newDatabaseName); databaseName = newDatabaseName; } if (databaseMaxSizeBytes.HasValue || databaseMaxSizeGb.HasValue || databaseEdition.HasValue || !string.IsNullOrEmpty(serviceObjectiveName)) { string sizeVal = null; if (databaseMaxSizeGb.HasValue) { sizeVal = databaseMaxSizeGb.Value.ToString() + "GB"; } else if (databaseMaxSizeBytes.HasValue) { if (databaseMaxSizeBytes.Value > 500 * 1024 * 1024) { sizeVal = (databaseMaxSizeBytes.Value / (1024 * 1024 * 1024)).ToString() + "GB"; } else { sizeVal = (databaseMaxSizeBytes.Value / (1024 * 1024)).ToString() + "MB"; } } result = AlterDatabaseProperties(databaseName, sizeVal, databaseEdition, serviceObjectiveName); } result.Context = this; return(result); }
/// <summary> /// Process the request using a temporary connection context using certificate authentication /// </summary> /// <param name="databaseName">The name of the database to update</param> /// <param name="maxSizeGb">the new size for the database or null</param> /// <param name="edition">the new edition for the database or null</param> private void ProcessWithServerName(string databaseName, int?maxSizeGb, DatabaseEdition?edition) { Func <string> GetClientRequestId = () => string.Empty; try { // Get the current subscription data. WindowsAzureSubscription subscription = WindowsAzureProfile.Instance.CurrentSubscription; // Create a temporary context ServerDataServiceCertAuth context = ServerDataServiceCertAuth.Create(this.ServerName, subscription); GetClientRequestId = () => context.ClientRequestId; // Remove the database with the specified name Database database = context.UpdateDatabase( databaseName, this.NewDatabaseName, maxSizeGb, edition, this.ServiceObjective); // Update the passed in database object if (this.MyInvocation.BoundParameters.ContainsKey("Database")) { this.Database.CopyFields(database); database = this.Database; } // If PassThru was specified, write back the updated object to the pipeline if (this.PassThru.IsPresent) { this.WriteObject(database); } } catch (Exception ex) { SqlDatabaseExceptionHandler.WriteErrorDetails( this, GetClientRequestId(), ex); } }
/// <summary> /// Update a database on the server. /// </summary> /// <param name="databaseName">The name of the database to modify.</param> /// <param name="newDatabaseName">The new name of the database.</param> /// <param name="databaseMaxSizeInGB">The new maximum size of the database.</param> /// <param name="databaseEdition">The new edition of the database.</param> /// <param name="serviceObjective">The new service objective of the database.</param> /// <returns>The updated database.</returns> public Database UpdateDatabase( string databaseName, string newDatabaseName, int?databaseMaxSizeInGB, DatabaseEdition?databaseEdition, ServiceObjective serviceObjective) { this.clientRequestId = SqlDatabaseCmdletBase.GenerateClientTracingId(); // Get the SQL management client SqlManagementClient sqlManagementClient = this.subscription.CreateClient <SqlManagementClient>(); this.AddTracingHeaders(sqlManagementClient); // Retrieve the specified database DatabaseGetResponse database = sqlManagementClient.Databases.Get( this.serverName, databaseName); // Update the database with the new properties DatabaseUpdateResponse response = sqlManagementClient.Databases.Update( this.serverName, databaseName, new DatabaseUpdateParameters() { Id = database.Id, Name = !string.IsNullOrEmpty(newDatabaseName) ? newDatabaseName : database.Name, Edition = databaseEdition.HasValue && (databaseEdition != DatabaseEdition.None) ? databaseEdition.ToString() : (database.Edition ?? string.Empty), CollationName = database.CollationName ?? string.Empty, MaximumDatabaseSizeInGB = databaseMaxSizeInGB.HasValue ? databaseMaxSizeInGB.Value : database.MaximumDatabaseSizeInGB, ServiceObjectiveId = serviceObjective != null ? serviceObjective.Id.ToString() : null, }); // Construct the resulting Database object Database updatedDatabase = CreateDatabaseFromResponse(response); return(updatedDatabase); }
/// <summary> /// Process the request using a temporary connection context using certificate authentication /// </summary> /// <param name="databaseName">The name of the database to update</param> /// <param name="maxSizeGb">the new size for the database or null</param> /// <param name="edition">the new edition for the database or null</param> private void ProcessWithServerName(string databaseName, int?maxSizeGb, DatabaseEdition?edition) { string clientRequestId = null; try { // Get the current subscription data. SubscriptionData subscriptionData = this.GetCurrentSubscription(); // Create a temporary context ServerDataServiceCertAuth context = ServerDataServiceCertAuth.Create(this.ServerName, subscriptionData); clientRequestId = context.ClientRequestId; // Remove the database with the specified name Database database = context.UpdateDatabase( databaseName, this.NewDatabaseName, maxSizeGb, edition, this.ServiceObjective); // If PassThru was specified, write back the updated object to the pipeline if (this.PassThru.IsPresent) { this.WriteObject(database); } if (this.MyInvocation.BoundParameters.ContainsKey("Database")) { this.Database.CopyFields(database); } } catch (Exception ex) { SqlDatabaseExceptionHandler.WriteErrorDetails( this, clientRequestId, ex); } }
/// <summary> /// Alter the database properties /// </summary> /// <param name="databaseName">The name of the database to alter</param> /// <param name="sizeVal">The new size of the database (format: ##{GB|MB})</param> /// <param name="databaseEdition">The new edition for the database</param> /// <param name="serviceObjectiveName">The new service objective name</param> /// <returns>The altered database</returns> private Database AlterDatabaseProperties(string databaseName, string sizeVal, DatabaseEdition?databaseEdition, string serviceObjectiveName) { string commandText = "ALTER DATABASE [{0}] MODIFY "; List <string> arguments = new List <string>(); if (!string.IsNullOrEmpty(sizeVal)) { arguments.Add(" MAXSIZE={1} "); } string edition = string.Empty; if (databaseEdition.HasValue && databaseEdition.Value != DatabaseEdition.None) { arguments.Add(" EDITION='{2}' "); edition = databaseEdition.Value.ToString(); } if (!string.IsNullOrEmpty(serviceObjectiveName)) { arguments.Add(" SERVICE_OBJECTIVE='{3}' "); } if (arguments.Count > 0) { commandText += " (" + string.Join(", ", arguments.ToArray()) + ")"; } commandText = string.Format( commandText, SqlEscape(databaseName), SqlEscape(sizeVal), SqlEscape(edition), SqlEscape(serviceObjectiveName)); builder["Database"] = null; using (var connection = CreateConnection()) { using (DbCommand command = connection.CreateCommand()) { command.CommandTimeout = commandTimeout; command.CommandText = commandText; connection.Open(); command.ExecuteNonQuery(); } } return(GetDatabase(databaseName)); }
/// <summary> /// process the request using the connection context. /// </summary> /// <param name="databaseName">the name of the database to alter</param> /// <param name="maxSizeGb">the new maximum size for the database</param> /// <param name="maxSizeBytes"></param> /// <param name="edition">the new edition for the database</param> private void ProcessWithConnectionContext(string databaseName, int?maxSizeGb, long?maxSizeBytes, DatabaseEdition?edition) { try { // Update the database with the specified name Services.Server.Database database = this.ConnectionContext.UpdateDatabase( databaseName, this.NewDatabaseName, maxSizeGb, maxSizeBytes, edition, this.ServiceObjective); if (this.Sync.IsPresent) { // Wait for the operation to complete on the server. database = CmdletCommon.WaitForDatabaseOperation(this, this.ConnectionContext, database, this.DatabaseName, false); } // If PassThru was specified, write back the updated object to the pipeline if (this.PassThru.IsPresent) { this.WriteObject(database); } if (this.ConnectionContext.GetType() == typeof(ServerDataServiceCertAuth)) { if (this.MyInvocation.BoundParameters.ContainsKey("Database")) { this.Database.CopyFields(database); } } } catch (Exception ex) { SqlDatabaseExceptionHandler.WriteErrorDetails( this, this.ConnectionContext.ClientRequestId, ex); } }
/// <summary> /// Process the request using a temporary connection context using certificate authentication /// </summary> /// <param name="databaseName">The name of the database to update</param> /// <param name="maxSizeGb">the new size for the database or null</param> /// <param name="maxSizeBytes"></param> /// <param name="edition">the new edition for the database or null</param> private void ProcessWithServerName(string databaseName, int?maxSizeGb, long?maxSizeBytes, DatabaseEdition?edition) { Func <string> GetClientRequestId = () => string.Empty; try { // Get the current subscription data. AzureSubscription subscription = Profile.Context.Subscription; // Create a temporary context ServerDataServiceCertAuth context = ServerDataServiceCertAuth.Create(this.ServerName, Profile, subscription); GetClientRequestId = () => context.ClientRequestId; // Remove the database with the specified name Services.Server.Database database = context.UpdateDatabase( databaseName, this.NewDatabaseName, maxSizeGb, maxSizeBytes, edition, this.ServiceObjective); if (this.Sync.IsPresent) { // Wait for the operation to complete on the server. database = CmdletCommon.WaitForDatabaseOperation(this, context, database, this.DatabaseName, false); } // Update the passed in database object if (this.MyInvocation.BoundParameters.ContainsKey("Database")) { this.Database.CopyFields(database); database = this.Database; } // If PassThru was specified, write back the updated object to the pipeline if (this.PassThru.IsPresent) { this.WriteObject(database); } } catch (Exception ex) { SqlDatabaseExceptionHandler.WriteErrorDetails( this, GetClientRequestId(), ex); } }
/// <summary> /// Process the command. /// </summary> public override void ExecuteCmdlet() { // Obtain the database name from the given parameters. string databaseName = null; if (this.MyInvocation.BoundParameters.ContainsKey("Database")) { databaseName = this.Database.Name; } else if (this.MyInvocation.BoundParameters.ContainsKey("DatabaseName")) { databaseName = this.DatabaseName; } // Obtain the name of the server string serverName = null; if (this.MyInvocation.BoundParameters.ContainsKey("ServerName")) { serverName = this.ServerName; } else { serverName = this.ConnectionContext.ServerName; } // Determine the max size of the db int?maxSizeGb = null; if (this.MyInvocation.BoundParameters.ContainsKey("MaxSizeGB")) { maxSizeGb = this.MaxSizeGB; } long?maxSizeBytes = null; if (this.MyInvocation.BoundParameters.ContainsKey("MaxSizeBytes")) { maxSizeBytes = this.MaxSizeBytes; } // Determine the edition for the db DatabaseEdition?edition = null; if (this.MyInvocation.BoundParameters.ContainsKey("Edition")) { edition = this.Edition; } string actionDescription = string.Format( CultureInfo.InvariantCulture, Resources.SetAzureSqlDatabaseDescription, serverName, databaseName); string actionWarning = string.Format( CultureInfo.InvariantCulture, Resources.SetAzureSqlDatabaseWarning, serverName, databaseName); this.WriteVerbose(actionDescription); // Do nothing if force is not specified and user cancelled the operation if (!this.Force.IsPresent && !this.ShouldProcess(actionDescription, actionWarning, Resources.ShouldProcessCaption)) { return; } // If service objective is specified, ask the user to confirm the change if (!this.Force.IsPresent && this.ServiceObjective != null) { string serviceObjectiveWarning = string.Format( CultureInfo.InvariantCulture, Resources.SetAzureSqlDatabaseServiceObjectiveWarning, serverName, databaseName); if (!this.ShouldContinue( serviceObjectiveWarning, Resources.ShouldProcessCaption)) { return; } } switch (this.ParameterSetName) { case ByNameWithConnectionContext: case ByObjectWithConnectionContext: this.ProcessWithConnectionContext(databaseName, maxSizeGb, maxSizeBytes, edition); break; case ByNameWithServerName: case ByObjectWithServerName: this.ProcessWithServerName(databaseName, maxSizeGb, maxSizeBytes, edition); break; } }
/// <summary> /// Execute the command. /// </summary> protected override void ProcessRecord() { // Obtain the database name from the given parameters. string databaseName = null; if (this.MyInvocation.BoundParameters.ContainsKey("Database")) { databaseName = this.Database.Name; } else if (this.MyInvocation.BoundParameters.ContainsKey("DatabaseName")) { databaseName = this.DatabaseName; } // Do nothing if force is not specified and user cancelled the operation string actionDescription = string.Format( CultureInfo.InvariantCulture, Resources.SetAzureSqlDatabaseDescription, this.ConnectionContext.ServerName, databaseName); string actionWarning = string.Format( CultureInfo.InvariantCulture, Resources.SetAzureSqlDatabaseWarning, this.ConnectionContext.ServerName, databaseName); this.WriteVerbose(actionDescription); if (!this.Force.IsPresent && !this.ShouldProcess( actionDescription, actionWarning, Resources.ShouldProcessCaption)) { return; } try { int?maxSizeGb = this.MyInvocation.BoundParameters.ContainsKey("MaxSizeGB") ? (int?)this.MaxSizeGB : null; DatabaseEdition?edition = this.MyInvocation.BoundParameters.ContainsKey("Edition") ? (DatabaseEdition?)this.Edition : null; // Update the database with the specified name Database database = this.ConnectionContext.UpdateDatabase( databaseName, this.NewName, maxSizeGb, edition); // If PassThru was specified, write back the updated object to the pipeline if (this.PassThru.IsPresent) { this.WriteObject(database); } } catch (Exception ex) { SqlDatabaseExceptionHandler.WriteErrorDetails( this, this.ConnectionContext.ClientRequestId, ex); } }
/// <summary> /// Update a database on the server. /// </summary> /// <param name="databaseName">The name of the database to modify</param> /// <param name="newDatabaseName">The new name of the database</param> /// <param name="databaseMaxSize">The new maximum size of the database</param> /// <param name="databaseEdition">The new edition of the database</param> /// <returns>The updated database</returns> public Database UpdateDatabase( string databaseName, string newDatabaseName, int?databaseMaxSize, DatabaseEdition?databaseEdition, ServiceObjective serviceObjective) { this.clientRequestId = SqlDatabaseManagementHelper.GenerateClientTracingId(); ISqlDatabaseManagement channel = GetManagementChannel(); Database database = this.GetDatabase(databaseName); //make sure the database exists. if (database == null) { throw new Exception( "Error: Result of GetDatabase() in ServerDataServiceCertAuth.UpdateDatabase() is null"); } SqlDatabaseInput input = new SqlDatabaseInput(); //Set the database ID and collation input.Id = database.Id.ToString(); input.CollationName = database.CollationName; if (serviceObjective != null) { input.ServiceObjectiveId = serviceObjective.Id.ToString(); } //Determine what the new name for the database should be if (!string.IsNullOrEmpty(newDatabaseName)) { input.Name = newDatabaseName; } else { input.Name = database.Name; } //Determine what the new edition for the database should be if (databaseEdition.HasValue && (databaseEdition != DatabaseEdition.None)) { input.Edition = databaseEdition.ToString(); } else { input.Edition = database.Edition; } //Determine what the new maximum size for the database should be. if (databaseMaxSize.HasValue) { input.MaxSizeGB = databaseMaxSize.ToString(); } else { input.MaxSizeGB = database.MaxSizeGB.ToString(); } //Send the update request and wait for the response. SqlDatabaseResponse response = channel.EndUpdateDatabase( channel.BeginUpdateDatabase( this.subscriptionId, this.serverName, databaseName, input, null, null)); //Transform the response into a database object. Database updatedDatabase = CreateDatabaseFromResponse(response); return(updatedDatabase); }