0

Creating database and adding to failover group

I have an Azure elastic pool and I have created a failover group with another elastic pool (with the same name) on a different Azure region, during the creation of the failover group I selected the elastic pool and it went through and added all the databases in the elastic pool to the elastic pool on the secondary server.

I have a script setup to automatically create new databases using the following TSQL:

CREATE DATABASE databaseName ( SERVICE_OBJECTIVE = ELASTIC_POOL ( name = "Elastic pool name" ) );

However, the above script does not add the database to the failover group and therefore it does not get added to the other SQL server in my other Azure region. I don't want to have to manually add any new databases to the failover group via the Azure portal each time so is there a TSQL script I can use at the point of creation to add the database to the failover group?

Deleting database, removing from failover group and from secondary server

Following on from the above I also have the following TQL which deletes a database:

DROP DATABASE databaseName;

Running the above deletes the database from the primary server and it removes it from the failover group but the database still exists on the secondary server. Is there a way to remove it from the secondary server using TSQL, is it as simple as running the above script again but pointing to the secondary server or is there a better way of doing this?

EDIT

As it seems there is no way to do this with TSQL then is this possible in C# using the Azure API with something like the following?

var dbResponse = client.FailoverGroups.Update("resourceGroupName", 
            "serverName", 
            "failoverGroupName", 
            new Microsoft.Azure.Management.Sql.Models.FailoverGroupUpdate() { 
                    Databases = new List<string>() { "databaseName" } 
                }
            );

2 Answers 2

1

I'm afraid no, there isn't a TSQL script which you can use at the point of creation to add the database to the failover group.

Only the two ways Azure provides for us can manage the failover group: Portal and Powershell.

If you want to delete the secondary database, remove it from the failover group before deleting it. Ref here: enter image description here

We still need do these database adding or deleting options with Portal or PowerShell.

4
  • Is this possible then using the Azure API in C#, I have updated my question with an example of what I mean, this needs to be done automatically within the code at the point of creating a database, it cannot be a manual process. Regarding the delete, from what I can tell it already removes it from the failover group when you delete it from the primary but it then leaves the secondary database on the failover server (which will need to be removed as well).
    – Bonio
    Commented May 17, 2021 at 10:14
  • Hi @WebFletch, please don't add or edit more questions in on one question. Just for now, haven't seen any code to manage the failover group. As I said, Azure only provide two ways for us, Portal and Powershell. By the way, it means that you can execute Powershell script in C# to achieve it. Example: foxlearn.com/windows-forms/…
    – Leon Yue
    Commented May 18, 2021 at 0:59
  • I wasn't adding more questions, the fundamental question is the same (i.e. how to programmatically add a database in an elastic pool to a failover group) possible the title of the question should have been worded better and instead said "programmatically". From what I have read powershell scripts cannot be used within Azure web apps and the powershell cmdlets essentially just wrap the Service Management Rest API. The Azure website might only provide two ways but I have seen plenty of times when things like this are possible even though not specified on Microsoft's website.
    – Bonio
    Commented May 18, 2021 at 9:06
  • @WebFletch Yes, Microsoft documents didn't say much more about this. Many blog or tutorials are based on the experience and test. It's hard to say possible or impossible. May be just for now, it's impossible. The document I shared to you which run PowerShell in C#, I hope that could be helpful. Really hope you're doing well. If my answer is helpful for you, hope you can accept it as answer. This can be beneficial to other community members. Thank you.
    – Leon Yue
    Commented May 20, 2021 at 1:02
1

Running some tests it seems you can use the Azure Rest API to do this and I have written the following method to test this, although it looks like you have to always pass through all the database names because this does not append what is already there but actually replaces the list of databases in the failover group.

I have not tested this in production to see if this would cause any issues such as effectively removing and re-adding the databases each time.

Hopefully someone else might be able to provide a better solution, my ideal solution would still be to use TSQL if possible because it means not having to setup a user account to access the REST API.

 public async Task AddDatabasesToFailoverGroupAsync(List<string> databaseNames)
    {
        // Variables
        string domainName = "XXXX"; /* Tenant ID or AAD domain */
        string username = "XXXX";
        string password = "XXXX";
        string resourceGroupName = "XXXXX";
        string serverName = "XXXX";
        string failoverGroupName = "XXXXX";
        string clientId = "XXXXX"; /* Active Directory APP Client ID */
        string subscriptionId = "XXXXXX";
        string databasePrefix = $"/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.Sql/servers/{serverName}/databases/";
        List<string> databases = new List<string>();

        // Add prefix to database names
        foreach (var dbName in databaseNames)
            databases.Add($"{databasePrefix}{dbName}");

        // Login to Azure
        var credentials = await UserTokenProvider.LoginSilentAsync(clientId, domainName, username, password);

        // Create client
        SqlManagementClient client = new SqlManagementClient(credentials)
        {
            SubscriptionId = subscriptionId
        };

        // Set parameters
        var parameters = new FailoverGroupUpdate()
        {
            Databases = databases
        };

        // Update failover group
        client.FailoverGroups.Update(resourceGroupName, serverName, failoverGroupName, parameters);
    }
1
  • thanks, although MS has dropped the ball on this one, typical Commented May 4, 2023 at 18:49

Not the answer you're looking for? Browse other questions tagged or ask your own question.