diff --git a/README.md b/README.md index a8c5e06c..6ba5926d 100644 --- a/README.md +++ b/README.md @@ -47,6 +47,24 @@ module "bigquery" { require_partition_filter = false, expiration_ms = null, }, + table_constraints { + + primary_key { + columns = ["id_1", "id_2"] + } + foreign_keys { + name = "foreign_key_1" + referenced_table { + project_id = "" + dataset_id = "foo" + table_id = "table_1" + } + column_references { + referencing_column = "id_1" + referenced_column = "id" + } + } + }, range_partitioning = null, expiration_time = null, clustering = ["fullVisitorId", "visitId"], diff --git a/main.tf b/main.tf index a7ba03dc..0120690b 100644 --- a/main.tf +++ b/main.tf @@ -102,6 +102,36 @@ resource "google_bigquery_table" "main" { } } + dynamic "table_constraints" { + for_each = each.value["table_constraints"] != null ? [each.value["table_constraints"]] : [] + content { + primary_key { + columns = table_constraints.value["primary_key"].columns + } + + dynamic "foreign_keys" { + for_each = table_constraints.value["foreign_keys"] != null ? table_constraints.value["foreign_keys"] : [] + content { + name = foreign_keys.value["name"] + + referenced_table { + project_id = foreign_keys.value["referenced_table"].project_id + dataset_id = foreign_keys.value["referenced_table"].dataset_id + table_id = foreign_keys.value["referenced_table"].table_id + } + + dynamic "column_references" { + for_each = foreign_keys.value["column_references"] + content { + referencing_column = column_references.value["referencing_column"] + referenced_column = column_references.value["referenced_column"] + } + } + } + } + } + } + lifecycle { ignore_changes = [ encryption_configuration # managed by google_bigquery_dataset.main.default_encryption_configuration diff --git a/metadata.yaml b/metadata.yaml index c7faca02..71937ce8 100644 --- a/metadata.yaml +++ b/metadata.yaml @@ -107,7 +107,7 @@ spec: - role: roles/bigquery.dataOwner special_group: projectOwners - name: tables - description: A list of objects which include table_id, table_name, schema, clustering, time_partitioning, range_partitioning, expiration_time and labels. + description: A list of objects which include table_id, table_name, schema, clustering, table_constraints, time_partitioning, range_partitioning, expiration_time, and labels. varType: |- list(object({ table_id = string, @@ -116,6 +116,23 @@ spec: schema = string, clustering = optional(list(string), []), require_partition_filter = optional(bool), + table_constraints = optional(object({ + primary_key = optional(object({ + columns = list(string) + }), null), + foreign_keys = optional(list(object({ + name = string + referenced_table = object({ + project_id = string, + dataset_id = string, + table_id = string, + }) + column_references = list(object({ + referencing_column = string, + referenced_column = string, + })) + })), null), + }), null), time_partitioning = optional(object({ expiration_ms = string, field = string, diff --git a/variables.tf b/variables.tf index 28ca7a8b..6358d3dc 100644 --- a/variables.tf +++ b/variables.tf @@ -118,7 +118,7 @@ variable "access" { } variable "tables" { - description = "A list of objects which include table_id, table_name, schema, clustering, time_partitioning, range_partitioning, expiration_time and labels." + description = "A list of objects which include table_id, table_name, schema, clustering, table_constraints, time_partitioning, range_partitioning, expiration_time, and labels." default = [] type = list(object({ table_id = string, @@ -127,6 +127,23 @@ variable "tables" { schema = string, clustering = optional(list(string), []), require_partition_filter = optional(bool), + table_constraints = optional(object({ + primary_key = optional(object({ + columns = list(string) + }), null), + foreign_keys = optional(list(object({ + name = string + referenced_table = object({ + project_id = string, + dataset_id = string, + table_id = string, + }) + column_references = list(object({ + referencing_column = string, + referenced_column = string, + })) + })), null), + }), null), time_partitioning = optional(object({ expiration_ms = string, field = string,