Skip to content

Feature: add table_constraints dynamic block for primary_key and foreign_keys #388

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 = "<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"],
Expand Down
30 changes: 30 additions & 0 deletions main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
19 changes: 18 additions & 1 deletion metadata.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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,
Expand Down
19 changes: 18 additions & 1 deletion variables.tf
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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,
Expand Down