Owain Esau
Owain Esau

Reputation: 1922

Check whether Azure SQL database has been fully removed

I am creating a simple Azure logic app that uses a function to:

  1. Delete a slave database
  2. Restore a copy of a master database (with the same name as the removed slave)

Remove Database

# Remove slave database
Remove-AzSqlDatabase `
    -DatabaseName $RestoreDatabaseName `
    -ServerName $ServerName `
    -ResourceGroupName $ResourceGroupName

Write-Host "Removed slave database"

Restore PIT Backup of Master

# Restore database
Restore-AzSqlDatabase `
      -FromPointInTimeBackup `
      -PointInTime (Get-Date).AddMinutes(-2) `
      -ResourceGroupName $ResourceGroupName `
      -ServerName $ServerName `
      -TargetDatabaseName $RestoreDatabaseName `
      -ResourceId $Database.ResourceID `
      -ElasticPoolName $ElasticPoolName

The issue i am having is that after removing the database, Azure still sees the database on the server and so i get the following error when restoring:

The destination database name 'Slave' already exists on the server 'server address'.

I cant find any way to check if this has been fully removed before starting the next function. Any help on how to achieve this would be greatly appreciated.

Upvotes: 1

Views: 1282

Answers (1)

Murray Foxcroft
Murray Foxcroft

Reputation: 13745

You can use Get-AzSqlDatabase to check if the DB is still in play.

Get-AzSqlDatabase -ResourceGroupName "ResourceGroup01" -ServerName "Server01" -DatabaseName "Database02"

Placing this in a loop with a sleep will give you a poll to check when the DB is finally gone for good and you can then resume your processing.

Start-Sleep -s 15

Make sure you have a circuit breaker in your logic to prevent and endless loop in the case of a failed deletion.

It may be easier to restore your DB with a new name to avoid the delay e.g. MyDb<yyyymmdd>

Or alternatively, use the Azure REST API from SQL DB delete.

DELETE https://management.azure.com/subscriptions/00000000-1111-2222-3333-444444444444/resourceGroups/Default-SQL-SouthEastAsia/providers/Microsoft.Sql/servers/testsvr/databases/testdb?api-version=2017-10-01-preview

and monitor the location response of the 204 Accepted to determine when the database has been completely removed. Azure Durable Functions give you a great monitor pattern you can use.

Upvotes: 2

Related Questions