Caching (?) issue with Set-AzSqlDatabase to rename a database

73 Views Asked by At

We have an Azure SQL Server that contains several databases, in groups of two: Each database has a "master" database, and a "current" database.

We use an automated process that performs each night the following steps:

  1. Make a copy of the "master" database using New-AzSqlDatabaseCopy with a temporary name.
  2. Delete the "current" database using Remove-AzSqlDatabase.
  3. Rename the temporary database to the "current" database using Set-AzSqlDatabase with the parameter "NewName".

The order of these steps are chosen in such way that the time the "current" database is not available is as short as possible.

This process worked perfectly for over a year. But since a few days we experience an issue with the Set-AzSqlDatabase that it fails with an error that a database with the new name already exists. The full error message is:

Resource with identifier '/subscriptions/<subscriptionid>/resourceGroups/<resourcegroup>/providers/Microsoft.Sql/servers/<servername>/databases/DBCurrent' already exists.

This seems a caching issue in the Set-AzSqlDatabase? Because when viewing the list of databases in SQL Server Management Studio, or when retrieving them with Get-AzSqlDatabase the database is not returned.

We use version 4.14.0 of the Az.Sql module, which is currently the latest version.

We already tried the following:

  • Checking after deleting the database and before renaming the database whether the database is really deleted, via Get-AzSqlDatabase. This is the case; the Get-AzSqlDatabase returns that the database is really deleted.
  • Implementing a retry mechanism in the "Rename" step: if the "Already exists" error occurs we wait 30 seconds and try renaming again. This sometimes helps after a few retries, but this also often fails even after 10 retries (5 minutes).
  • Changing the Set-AzSqlDatabase into using the Azure CLI command az sql db rename, but this doesn't help; the same error occurs about a database already existing with the new name.
  • The process runs on a scaleset agent. When the run has failed (after 10 retries) and we rerun it on a different agent, the rename succeeds most of the time.

Any help with this is really appreciated.

UPDATE - March 21st, 2024:

As a workaround we tried using Azure's REST API directly, as described in this article: https://learn.microsoft.com/en-us/rest/api/sql/databases/rename

This appears to work successfully every time without the "Resource already exists" error, even without retries. So it seems that the PowerShell (and CLI) commands do cache something. Is there a way to clear this cache? I tried calling a Clear-AzConfig before calling the Set-AzSqlDatabase, but that doesn't help.

0

There are 0 best solutions below