hashicorp / terraform-provider-google

Terraform Provider for Google Cloud Platform
https://registry.terraform.io/providers/hashicorp/google/latest/docs
Mozilla Public License 2.0
2.36k stars 1.75k forks source link

Add support for `schema` in bigquery_job.load #8736

Open colincadams opened 3 years ago

colincadams commented 3 years ago

Community Note

Description

Add support for the schema field for bigquery load jobs.

https://cloud.google.com/bigquery/docs/reference/rest/v2/Job#JobConfigurationLoad

This field is helpful if you cannot rely on the schema being autodetected properly. Without this field, there is no way to override the schema for WRITE_TRUNCATE jobs. Setting the schema on the table itself doesn't help, as it is overwritten by the job.

New or Affected Resource(s)

Potential Terraform Configuration

resource "google_storage_bucket_object" "table_data" {
  name   = "${var.table_name}.csv"
  bucket = var.bucket_name
  source = "${var.table_name}.csv"
}

resource "google_bigquery_table" "table" {
  dataset_id = var.dataset_id
  table_id   = var.table_name

  schema = var.schema
}

resource "google_bigquery_job" "load" {
  job_id = "${var.table_name}_load_${md5(google_storage_bucket_object.table_data.crc32c)}"

  load {
    source_uris = [
      "gs://${var.bucket_name}/${google_storage_bucket_object.table_data.output_name}"
    ]

    schema = var.schema

    destination_table {
      project_id = google_bigquery_table.table.project
      dataset_id = google_bigquery_table.table.dataset_id
      table_id   = google_bigquery_table.table.table_id
    }

    skip_leading_rows = 1

    write_disposition = "WRITE_TRUNCATE"
  }
}

References

As a start, I pushed what I think would be the necessary change here: https://github.com/GoogleCloudPlatform/magic-modules/compare/master...colincadams:colincadams/add-schema-bq-load

The configuration is copied from google_bigquery_table, save for the description which I pulled from the API docs.

b/374161354

ggtisc commented 3 months ago

Hi @colincadams!

Currently there are many options to manage this like: