Skip to content

Commit

Permalink
fix schema for import (#5428) (#10521)
Browse files Browse the repository at this point in the history
Signed-off-by: Modular Magician <[email protected]>
  • Loading branch information
modular-magician authored Nov 8, 2021
1 parent 02984a0 commit 617d1b0
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 0 deletions.
3 changes: 3 additions & 0 deletions .changelog/5428.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:bug
bigquery: fixed a bug following import where schema is empty on `google_bigquery_table`
```
3 changes: 3 additions & 0 deletions google/resource_bigquery_table.go
Original file line number Diff line number Diff line change
Expand Up @@ -311,6 +311,9 @@ func resourceBigQueryTableSchemaCustomizeDiffFunc(d TerraformResourceDiff) error
// The API can return an empty schema which gets encoded to "null" during read.
oldSchemaText = "[]"
}
if newSchemaText == "null" {
newSchemaText = "[]"
}
var old, new interface{}
if err := json.Unmarshal([]byte(oldSchemaText), &old); err != nil {
// don't return error, its possible we are going from no schema to schema
Expand Down
65 changes: 65 additions & 0 deletions google/resource_bigquery_table_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -980,6 +980,39 @@ func TestAccBigQueryTable_allowDestroy(t *testing.T) {
})
}

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

datasetID := fmt.Sprintf("tf_test_%s", randString(t, 10))
tableID := fmt.Sprintf("tf_test_%s", randString(t, 10))

vcrTest(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckBigQueryTableDestroyProducer(t),
Steps: []resource.TestStep{
{
Config: testAccBigQueryTable_mimicCreateFromConsole(datasetID, tableID),
},
{
ResourceName: "google_bigquery_table.test",
ImportState: true,
ImportStateVerify: true,
ImportStateVerifyIgnore: []string{"deletion_protection"},
},
{
Config: testAccBigQueryTable_emptySchema(datasetID, tableID),
},
{
ResourceName: "google_bigquery_table.test",
ImportState: true,
ImportStateVerify: true,
ImportStateVerifyIgnore: []string{"deletion_protection"},
},
},
})
}

type testUnitBigQueryDataTableJSONChangeableTestCase struct {
name string
jsonOld string
Expand Down Expand Up @@ -2131,6 +2164,38 @@ resource "google_bigquery_table" "test" {
`, datasetID, tableID)
}

func testAccBigQueryTable_mimicCreateFromConsole(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
[
]
EOF
}
`, datasetID, tableID)
}

func testAccBigQueryTable_emptySchema(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
}
`, datasetID, tableID)
}

var TEST_CSV = `lifelock,LifeLock,,web,Tempe,AZ,1-May-07,6850000,USD,b
lifelock,LifeLock,,web,Tempe,AZ,1-Oct-06,6000000,USD,a
lifelock,LifeLock,,web,Tempe,AZ,1-Jan-08,25000000,USD,c
Expand Down

0 comments on commit 617d1b0

Please sign in to comment.