Skip to content

Commit

Permalink
add validation to prevent BigQuery view from creation when input sche…
Browse files Browse the repository at this point in the history
…ma contains required fields
  • Loading branch information
wj-chen committed Jul 2, 2024
1 parent 0c8818e commit 5b647ab
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1598,6 +1598,9 @@ func resourceBigQueryTableCreate(d *schema.ResourceData, meta interface{}) error
}

if table.View != nil && table.Schema != nil {
if schemaHasRequiredFields(table.Schema) {
return errors.New("Schema cannot contain required fields when creating a view")
}

log.Printf("[INFO] Removing schema from table definition because BigQuery does not support setting schema on view creation")
schemaBack := table.Schema
Expand Down Expand Up @@ -2560,6 +2563,15 @@ func setEmptyPolicyTagsInSchema(field *bigquery.TableFieldSchema) {
}
}

func schemaHasRequiredFields(schema *bigquery.TableSchema) bool {
for _, field := range schema.Fields {
if "REQUIRED" == field.Mode {
return true
}
}
return false
}

func expandTimePartitioning(configured interface{}) *bigquery.TimePartitioning {
raw := configured.([]interface{})[0].(map[string]interface{})
tp := &bigquery.TimePartitioning{Type: raw["type"].(string)}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1496,6 +1496,23 @@ func TestAccBigQueryTable_invalidSchemas(t *testing.T) {
})
}

func TestAccBigQueryTable_schemaWithRequiredFieldAndView(t *testing.T) {
datasetID := fmt.Sprintf("tf_test_%s", acctest.RandString(t, 10))
tableID := fmt.Sprintf("tf_test_%s", acctest.RandString(t, 10))

acctest.VcrTest(t, resource.TestCase{
PreCheck: func() { acctest.AccTestPreCheck(t) },
ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactories(t),
CheckDestroy: testAccCheckBigQueryTableDestroyProducer(t),
Steps: []resource.TestStep{
{
Config: testAccBigQueryTableWithSchemaWithRequiredFieldAndView(datasetID, tableID),
ExpectError: regexp.MustCompile("Schema cannot contain required fields when creating a view"),
},
},
})
}

func TestAccBigQueryTable_TableReplicationInfo_ConflictsWithView(t *testing.T) {
t.Parallel()

Expand Down Expand Up @@ -3960,6 +3977,42 @@ resource "google_bigquery_table" "test" {
`, datasetID, tableID, schema)
}

func testAccBigQueryTableWithSchemaWithRequiredFieldAndView(datasetID, tableID string) string {
return fmt.Sprintf(`
resource "google_bigquery_dataset" "test" {
dataset_id = "%s"
}

resource "google_bigquery_table" "test" {
deletion_protection = false
table_id = "%s"
dataset_id = google_bigquery_dataset.test.dataset_id
schema = <<EOF
[
{
"name": "requiredField",
"type": "STRING",
"mode": "REQUIRED",
"description": "requiredField"
},
{
"name": "optionalField",
"type": "STRING",
"mode": "NULLABLE",
"description": "optionalField"
}
]
EOF
view {
query = <<EOF
SELECT 'a' AS requiredField, 'b' AS optionalField
EOF
use_legacy_sql = false
}
}
`, datasetID, tableID)
}

func testAccBigQueryTableWithReplicationInfoAndView(datasetID, tableID string) string {
return fmt.Sprintf(`
resource "google_bigquery_dataset" "test" {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -108,14 +108,20 @@ Description of the change and how users should adjust their configuration (if ne

Description of the change and how users should adjust their configuration (if needed).

## Resource: `google_sql_database_instance`
## Resource: `google_bigquery_table`

### `settings.ip_configuration.require_ssl` is now removed
### View creation now validates `schema`

Removed in favor of field `settings.ip_configuration.ssl_mode`.
A `view` can no longer be created when `schema` contains required fields

## Resource: `google_pubsub_topic`

### `schema_settings` no longer has a default value

An empty value means the setting should be cleared.
An empty value means the setting should be cleared.

## Resource: `google_sql_database_instance`

### `settings.ip_configuration.require_ssl` is now removed

Removed in favor of field `settings.ip_configuration.ssl_mode`.

0 comments on commit 5b647ab

Please sign in to comment.