Skip to content

Commit

Permalink
Add convenient Database.Rename methods
Browse files Browse the repository at this point in the history
  • Loading branch information
jaredmoo committed Feb 7, 2018
1 parent b2330fe commit 6edac7e
Show file tree
Hide file tree
Showing 6 changed files with 213 additions and 7 deletions.
65 changes: 65 additions & 0 deletions src/SDKs/SqlManagement/Management.Sql/DatabasesOperations.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License. See License.txt in the project root for
// license information.

namespace Microsoft.Azure.Management.Sql
{
using Microsoft.Rest.Azure;
using System.Collections.Generic;
using System.Threading;
using System.Threading.Tasks;

internal partial class DatabasesOperations
{
/// <summary>
/// Renames a database.
/// </summary>
/// <param name='resourceGroupName'>
/// The name of the resource group that contains the resource. You can
/// obtain this value from the Azure Resource Manager API or the
/// portal.
/// </param>
/// <param name='serverName'>
/// The name of the server.
/// </param>
/// <param name='databaseName'>
/// The name of the database to rename.
/// </param>
/// <param name='newName'>
/// The new name that the database should be renamed to.
/// </param>
/// <param name='customHeaders'>
/// The headers that will be added to request.
/// </param>
/// <param name='cancellationToken'>
/// The cancellation token.
/// </param>
/// <exception cref="Microsoft.Rest.Azure.CloudException">
/// Thrown when the operation returned an invalid status code
/// </exception>
/// <exception cref="Microsoft.Rest.ValidationException">
/// Thrown when a required parameter is null
/// </exception>
public async Task<AzureOperationResponse> RenameWithHttpMessagesAsync(
string resourceGroupName,
string serverName,
string databaseName,
string newName,
Dictionary<string, List<string>> customHeaders = null,
CancellationToken cancellationToken = default(CancellationToken))
{
return await this.RenameWithHttpMessagesAsync(
resourceGroupName,
serverName,
databaseName,
new Models.ResourceMoveDefinition(
string.Format("/subscriptions/{0}/resourceGroups/{1}/providers/Microsoft.Sql/servers/{2}/databases/{3}",
this.Client.SubscriptionId,
resourceGroupName,
serverName,
newName)),
customHeaders,
cancellationToken);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License. See License.txt in the project root for
// license information.

namespace Microsoft.Azure.Management.Sql
{
using System.Threading;
using System.Threading.Tasks;

public partial class DatabasesOperationsExtensions
{
/// <summary>
/// Renames a database.
/// </summary>
/// <param name='operations'>
/// The operations group for this extension method.
/// </param>
/// <param name='resourceGroupName'>
/// The name of the resource group that contains the resource. You can obtain
/// this value from the Azure Resource Manager API or the portal.
/// </param>
/// <param name='serverName'>
/// The name of the server.
/// </param>
/// <param name='databaseName'>
/// The name of the database to rename.
/// </param>
/// <param name='newName'>
/// The new name that the database should be renamed to.
/// </param>
public static void Rename(
this IDatabasesOperations operations,
string resourceGroupName,
string serverName,
string databaseName,
string newName)
{
RenameAsync(operations, resourceGroupName, serverName, databaseName, newName).GetAwaiter().GetResult();
}

/// <summary>
/// Renames a database.
/// </summary>
/// <param name='operations'>
/// The operations group for this extension method.
/// </param>
/// <param name='resourceGroupName'>
/// The name of the resource group that contains the resource. You can obtain
/// this value from the Azure Resource Manager API or the portal.
/// </param>
/// <param name='serverName'>
/// The name of the server.
/// </param>
/// <param name='databaseName'>
/// The name of the database to rename.
/// </param>
/// <param name='newName'>
/// The new name that the database should be renamed to.
/// </param>
/// <param name='cancellationToken'>
/// The cancellation token.
/// </param>
public static async Task RenameAsync(
this IDatabasesOperations operations,
string resourceGroupName,
string serverName,
string databaseName,
string newName,
CancellationToken cancellationToken = default(CancellationToken))
{
(await operations.RenameWithHttpMessagesAsync(
resourceGroupName,
serverName,
databaseName,
newName,
null,
cancellationToken).ConfigureAwait(false)).Dispose();
}
}
}
51 changes: 51 additions & 0 deletions src/SDKs/SqlManagement/Management.Sql/IDatabasesOperations.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License. See License.txt in the project root for
// license information.

namespace Microsoft.Azure.Management.Sql
{
using Microsoft.Rest.Azure;
using System.Collections.Generic;
using System.Threading;
using System.Threading.Tasks;

public partial interface IDatabasesOperations
{
/// <summary>
/// Renames a database.
/// </summary>
/// <param name='resourceGroupName'>
/// The name of the resource group that contains the resource. You can
/// obtain this value from the Azure Resource Manager API or the
/// portal.
/// </param>
/// <param name='serverName'>
/// The name of the server.
/// </param>
/// <param name='databaseName'>
/// The name of the database to rename.
/// </param>
/// <param name='newName'>
/// The new name that the database should be renamed to.
/// </param>
/// <param name='customHeaders'>
/// The headers that will be added to request.
/// </param>
/// <param name='cancellationToken'>
/// The cancellation token.
/// </param>
/// <exception cref="Microsoft.Rest.Azure.CloudException">
/// Thrown when the operation returned an invalid status code
/// </exception>
/// <exception cref="Microsoft.Rest.ValidationException">
/// Thrown when a required parameter is null
/// </exception>
Task<AzureOperationResponse> RenameWithHttpMessagesAsync(
string resourceGroupName,
string serverName,
string databaseName,
string newName,
Dictionary<string, List<string>> customHeaders = null,
CancellationToken cancellationToken = default(CancellationToken));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,13 @@
<PackageId>Microsoft.Azure.Management.Sql</PackageId>
<Description>Azure SQL Management SDK library</Description>
<AssemblyName>Microsoft.Azure.Management.Sql</AssemblyName>
<Version>1.11.0-preview</Version>
<Version>1.12.0-preview</Version>
<PackageTags>Microsoft Azure SQL Management;SQL;SQL Management;</PackageTags>
<PackageReleaseNotes>
<![CDATA[
New features:
- Updated available service objectives & editions
- Added BackupLongTermRetentionVaults.ListByServer and BackupLongTermRetentionPolicies.ListByDatabase
- Added support for automatic tuning
- Added more convenient method to rename databases, `Databases.Rename(resourceGroupName, serverName, databaseName, newName)`.
]]>
</PackageReleaseNotes>
</PropertyGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
[assembly: AssemblyTitle("Microsoft Azure SQL Management Library")]
[assembly: AssemblyDescription("Provides management functionality for Microsoft Azure SQL.")]
[assembly: AssemblyVersion("1.0.0.0")]
[assembly: AssemblyFileVersion("1.11.0.0")]
[assembly: AssemblyFileVersion("1.12.0.0")]



Expand Down
16 changes: 13 additions & 3 deletions src/SDKs/SqlManagement/Sql.Tests/DatabaseCrudScenarioTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ public void TestRenameDatabase()
});
Assert.NotNull(db1);

// Rename
// Rename using id
string newSuffix = "_renamed";
string newName = db1.Name + newSuffix;
string newId = db1.Id + newSuffix;
Expand All @@ -136,8 +136,18 @@ public void TestRenameDatabase()
});

// Get database at its new id
Database db2 = sqlClient.Databases.Get(resourceGroup.Name, server.Name, newName);
Assert.Equal(newId, db2.Id);
Database newDb = sqlClient.Databases.Get(resourceGroup.Name, server.Name, newName);
Assert.Equal(newId, newDb.Id);

// Rename using new name
string newSuffix2 = "2";
string newName2 = newName + newSuffix2;
string newId2 = newId + newSuffix2;
sqlClient.Databases.Rename(resourceGroup.Name, server.Name, newName, newName2);

// Get database at its new id
Database newDb2 = sqlClient.Databases.Get(resourceGroup.Name, server.Name, newName2);
Assert.Equal(newId2, newDb2.Id);
}
}

Expand Down

0 comments on commit 6edac7e

Please sign in to comment.