Dropping an adx table with the azurerm terraform provider

198 Views Asked by At

How does one drop an adx table using the azurerm terraform provider? It seems like an intentional exclusion, so I wonder if I'm missing something fundamental here.

I tried doing the following:

resource "azurerm_kusto_script" "drop_old_table" {
  name                       = "drop_old_table"
  database_id                = azurerm_kusto_database.mydb.id
  continue_on_errors_enabled = false
  script_content             = ".drop tables ( Table1, Table2) ifexists"
}

But got an error:

Code="ScriptContainsUnsupportedCommand" Message="[BadRequest] The provided script contains unsupported command. The commands must start with the following verbs: '.create, .create-or-alter, .create-merge, .alter, .alter-merge, .enable'"

1

There are 1 best solutions below

2
On

I tried to reproduce the same in my environment to drop the table if exists:

code:

resource "azurerm_kusto_script" "example" {
  name                               = "example"
  database_id                        = azurerm_kusto_database.mydb.id
  url                                = azurerm_storage_blob.newexample.id
  sas_token                          = data.azurerm_storage_account_blob_container_sas.example.sas
  continue_on_errors_enabled         = true
  force_an_update_when_value_changed = "first"
}

Using

source_content= ".drop table MyTable ifexists"

Received the same error :

Scripts/drop_old_table" already exists - to be managed via Terraform this resource needs to be imported into the State. Please see the resource documentation for "azurerm_kusto_script" for more information.

error:

enter image description here

Terraform may not work while cheking using ifexists query .

I used .drop table MyTable excluding the ifexists When I checked the portal , I could not find anytable.

.drop table MyTable

Code:

resource "azurerm_storage_blob" "newexample" {
  name                   = "script.txt"
  storage_account_name   = azurerm_storage_account.newexample.name
  storage_container_name = azurerm_storage_container.conexample.name
  type                   = "Block"
  source_content         = ".drop table MyTable "
}

As drop requies to check the resource first, gave error:

To query for pre existing resource, you may have to use variables to store data and use Boolean state.

or try use external data_source ,if can be done using sdk or powershell cli terraform-check-if-resource-exists-before-creating-it | Stack Overflow

Reference : .drop table and .drop tables - Azure Data Explorer | Microsoft Learn