Google cloud platform Terraform Bigquery创建表替换表而不是编辑表

Google cloud platform Terraform Bigquery创建表替换表而不是编辑表,google-cloud-platform,google-bigquery,terraform,terraform-provider-gcp,Google Cloud Platform,Google Bigquery,Terraform,Terraform Provider Gcp,我添加了一个json文件,其中包含我要创建的所有表: tables.json “表格”:{ “表1”:{ “数据集id”:“数据集1”, “表id”:“表1”, “schema_path”:“folder/table1.json” }, “表2”:{ “数据集_id”:“数据集2”, “表id”:“表2”, “schema_path”:“folder/table2.json” } } 然后,使用Terraform资源上的foreach,我想动态创建这些表: local.tf文件 l

我添加了一个json文件,其中包含我要创建的所有表:

tables.json


“表格”:{
“表1”:{
“数据集id”:“数据集1”,
“表id”:“表1”,
“schema_path”:“folder/table1.json”
},
“表2”:{
“数据集_id”:“数据集2”,
“表id”:“表2”,
“schema_path”:“folder/table2.json”
}
}
然后,使用Terraform资源上的foreach,我想动态创建这些表:

local.tf文件


    locals {
      tables = jsondecode(file("${path.module}/resource/tables.json"))["tables"]
    }

variables.tf文件


    variable "project_id" {
      description = "Project ID, used to enforce providing a project id."
      type = string
    }
    
    variable "time_partitioning" {
      description = "Configures time-based partitioning for this table."
      type = map(string)
      default = {
        type = "DAY"
        field = "my_field"
      }
    }

main.tf文件


    resource "google_bigquery_table" "tables" {
      for_each = local.tables
      project = var.project_id
      dataset_id = each.value["dataset_id"]
      table_id = each.value["table_id"]
    
      dynamic "time_partitioning" {
        for_each = [
          var.time_partitioning
        ]
        content {
          type = try(each.value["partition_type"], time_partitioning.value["type"])
          field = try(each.value["partition_field"], time_partitioning.value["field"])
          expiration_ms = try(time_partitioning.value["expiration_ms"], null)
          require_partition_filter = try(time_partitioning.value["require_partition_filter"], null)
        }
      }
    
      schema = file("${path.module}/resource/schema/${each.value["schema_path"]}")
    }

架构文件包含经典的bigquery架构,例如:


[
{
“名称”:“字段”,
“类型”:“字符串”,
“模式”:“可为空”,
“说明”:“我的领域”
}
]
表的创建工作得很好,但当我在模式上添加一个新的可空字段时,Terraform建议“替换表”(销毁并重新创建),而不是“更新表”

本机Bigquery和Terraform在这种情况下的正常行为是更新表

当用户使用相同的Terraform资源但不使用forEach进行相同的测试时,Terraform具有预期的行为并建议更新表

带有“forEach”的地形日志示例:

Terraform显示并正确检测要为表添加的新列,但表示替换而不是版本

我重复一遍,使用相同的Terraform资源(不使用forEach)和单个Bigquery表进行完全相同的测试,效果很好(相同的模式,相同的更改)。我创建了这个表,当一个表添加了一个新的可空列时,Terraform会提出一个版本(预期的行为)

我查看了Terraform文档和web,没有看到使用Terraform管理表列表的示例

无法使用已配置的表和foreach创建和更新表吗


谢谢您的帮助。

这听起来像是提供商的错误。我在存储库中找到了这个,它似乎与您的问题有关。就在13个小时前(在撰写本文时),该公司刚刚合并。因此,也许您可以等待下一个版本(
v3.63.0
),看看它是否解决了您的问题

仅供参考:您可能希望验证下一版本中是否包含修复提交。我之前遇到过这样一件事,在发布之前被合并到master中的东西实际上并没有发布。

非常感谢@Alessandro,问题确实是由于Terraform提供的Google版本。 我使用了Google provider的v3.62.0版本,而你的目标是向好的方向发展

我也看到了这个链接:

“tpolekhin”有一个非常有用的评论(感谢他):

在这篇评论中,我们可以看到一些版本存在问题。 例如,这适用于v3.55.0,但不适用于v3.56.0

我暂时将版本降级到v3.55.0,当下一个版本解决这个问题时,我将升级它

provider.tf:


    provider "google" {
      version = "= 3.55.0"
    }

Hopefully im not beating a dead horse commenting on the closed issue, but I did some testing with various versions on the provider, and it behaves VERY differently each time.

So, our terraform code change was pretty simple: add 2 new columns to existing BigQuery table SCHEDULE
Nothing changes between runs - only provider version

v3.52.0
Plan: 0 to add, 19 to change, 0 to destroy.
Mostly adds + mode = "NULLABLE" to fields in bunch of tables, and adds 2 new fields in SCHEDULE table

v3.53.0
Plan: 0 to add, 2 to change, 0 to destroy.
Adds 2 new fields to SCHEDULE table, and moves one field in another table in a different place (sorting?)

v3.54.0
Plan: 1 to add, 1 to change, 1 to destroy.
Adds 2 new fields to SCHEDULE table, and moves one field in another table in a different place (sorting?) but now with table re-creation for some reason

v3.55.0
Plan: 0 to add, 2 to change, 0 to destroy.
Adds 2 new fields to SCHEDULE table, and moves one field in another table in a different place (sorting?)
behaves exactly like v3.53.0

v3.56.0
Plan: 1 to add, 0 to change, 1 to destroy.

    provider "google" {
      version = "= 3.55.0"
    }