aidanmelen / terraform-provider-snowsql

Terraform SnowSQL provider
https://registry.terraform.io/providers/aidanmelen/snowsql/latest
Other
21 stars 10 forks source link

subsequent tf plan and apply dont work #98

Closed vijayrgopu closed 1 year ago

vijayrgopu commented 1 year ago

Hi there,

Thank you for opening an issue. Please note that we try to keep the Terraform issue tracker reserved for bug reports and feature requests. For general usage questions, please see: https://www.terraform.io/community.html.

Terraform Version

Terraform v1.5.0

Affected Resource(s)

"snowsql_exec"

I am using this to insert configuration into specific snowflake tables which works for the first time but subsequent terraform plan and apply commands fail complaining about "Cannot perform SELECT. This session does not have a current database. Call 'USE DATABASE', or use a qualified name."

in my commands i have provided what database to use and what schema to use using the commands USE DATABASE etc. however it doesnt recognize these values and fails.

provider "snowsql" { username = account = password = warehouse = role = }

resource "snowsql_exec" "insert_config" { create { statements = <<-EOT USE DATABASE ${snowflake_database.database.name}; USE SCHEMA ${snowflake_schema.schema.name}; INSERT INTO XYZ.ABC_TBL (COL) VALUES ('COL1'); EOT } read { statements = <<-EOSTMT USE DATABASE ${snowflake_database.database.name}; USE SCHEMA ${snowflake_schema.schema.name}; SELECT COUNT(*) FROM XYZ.ABC_TBL; EOSTMT } delete { statements = <<-EOD USE DATABASE ${snowflake_database.database.name}; USE SCHEMA ${snowflake_schema.schema.name}; DELETE FROM XYZ.ABC_TBL WHERE 1=2; EOD } depends_on = [ snowflake_table.table_config ] }

aidanmelen commented 1 year ago

Thanks for opening an issue. The read.statements from the snowsql_exec resource must be fully qualified. For example:

resource "snowsql_exec" "insert_shard_config" {
  create { ... }

  read {
    statements = "SELECT COUNT(*) FROM ${snowflake_database.database.name}.${snowflake_schema.schema.name}.ABC_TBL;"
  }

  delete { ... }
}

The create, update, and delete statements support multiple USE because they ignore the rows returned.