Skip to content
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

Add validation to prevent BigQuery view from creation when input schema contains required fields #7755

Merged
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
3 changes: 3 additions & 0 deletions .changelog/11101.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:breaking-change
bigquery: added validation to prevent table view creation if schema contains required fields for `google_bigquery_table` resource
```
12 changes: 12 additions & 0 deletions google-beta/services/bigquery/resource_bigquery_table.go
Original file line number Diff line number Diff line change
Expand Up @@ -1593,6 +1593,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 @@ -2551,6 +2554,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
53 changes: 53 additions & 0 deletions google-beta/services/bigquery/resource_bigquery_table_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1497,6 +1497,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 @@ -3982,6 +3999,42 @@ resource "google_bigquery_table" "test" {
`, datasetID, tableID)
}

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 testAccBigQueryTableWithReplicationInfo(projectID, sourceDatasetID, sourceTableID, sourceMVID, replicaDatasetID, replicaMVID, sourceMVJobID, dropMVJobID, replicationIntervalExpr string) string {
return fmt.Sprintf(`
resource "google_bigquery_dataset" "source" {
Expand Down
6 changes: 6 additions & 0 deletions website/docs/guides/version_6_upgrade.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,12 @@ Description of the change and how users should adjust their configuration (if ne

## Resources

## Resource: `google_bigquery_table`

### View creation now validates `schema`

A `view` can no longer be created when `schema` contains required fields

## Resource: `google_sql_database_instance`

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