Skip to content

Commit

Permalink
Fix bigquery table replace hitting "already exists" issue (#2498)
Browse files Browse the repository at this point in the history
Bigquery materialized views on tables seem to be structural, so due to
pulumi/pulumi#918 the normal pulumi replace of
creating and then deleting does not work.

This PR enables DeleteBeforeRelace by default for the resource, fixing
the issue.

fixes #2491
  • Loading branch information
VenelinMartinov authored Oct 3, 2024
1 parent 9ec1b4c commit 8a2ca11
Show file tree
Hide file tree
Showing 3 changed files with 79 additions and 2 deletions.
25 changes: 23 additions & 2 deletions provider/provider_yaml_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -384,7 +384,7 @@ func writeStackExport(path string, snapshot *apitype.UntypedDeployment, overwrit
return fmt.Errorf("stack export must not be nil")
}
dir := filepath.Dir(path)
err := os.MkdirAll(dir, 0755)
err := os.MkdirAll(dir, 0o755)
if err != nil {
return err
}
Expand All @@ -399,8 +399,9 @@ func writeStackExport(path string, snapshot *apitype.UntypedDeployment, overwrit
if pathExists && !overwrite {
return fmt.Errorf("stack export already exists at %s", path)
}
return os.WriteFile(path, stackBytes, 0600)
return os.WriteFile(path, stackBytes, 0o600)
}

func exists(filePath string) (bool, error) {
_, err := os.Stat(filePath)
switch {
Expand Down Expand Up @@ -1314,3 +1315,23 @@ Resources:
// We should expect that we refresh cleanly
pt.Preview(t, optpreview.ExpectNoChanges())
}

func TestBigqueryMaterializedViewReplace(t *testing.T) {
pt := pulumiTest(t, "test-programs/bigquery-table-materialized-view")
pt.SetConfig(t, "gcpProj", getProject())
query1 := "SELECT * FROM ${defaultTable.datasetId}.${defaultTable.tableId}"
query2 := "SELECT * FROM ${defaultTable.datasetId}.${defaultTable.tableId} WHERE " +
"${defaultTable.tableId}.state = 'value'"

pulumiYAMLContents := pt.ReadPulumiYaml(t)

// replace query manually to avoid issues with variable interpolation
pulumiYAMLContents1 := strings.ReplaceAll(pulumiYAMLContents, "<QUERY>", query1)
pt.WritePulumiYaml(t, pulumiYAMLContents1)
pt.Up(t)

// replace query manually to avoid issues with variable interpolation
pulumiYAMLContents2 := strings.ReplaceAll(pulumiYAMLContents, "<QUERY>", query2)
pt.WritePulumiYaml(t, pulumiYAMLContents2)
pt.Up(t)
}
3 changes: 3 additions & 0 deletions provider/resources.go
Original file line number Diff line number Diff line change
Expand Up @@ -2095,6 +2095,9 @@ func Provider() tfbridge.ProviderInfo {
Source: "privileged_access_manager_entitlement.html.markdown",
},
},
"google_bigquery_table": {
DeleteBeforeReplace: true,
},
},
DataSources: map[string]*tfbridge.DataSourceInfo{
// Access Approval
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
name: bigquery-table-materialized-view
runtime: yaml
resources:
randomName:
type: random:RandomString
properties:
length: 16
special: false
upper: false
number: false
default:
type: gcp:bigquery:Dataset
properties:
datasetId: ${randomName.id}
friendlyName: test
description: This is a test description
location: EU
defaultTableExpirationMs: 3.6e+06
labels:
env: default
defaultTable:
type: gcp:bigquery:Table
properties:
deletionProtection: false
datasetId: ${default.datasetId}
tableId: bar
timePartitioning:
type: DAY
labels:
env: default
schema: |
[
{
"name": "permalink",
"type": "STRING",
"mode": "NULLABLE",
"description": "The Permalink"
},
{
"name": "state",
"type": "STRING",
"mode": "NULLABLE",
"description": "State where the head office is located"
}
]
sheet:
type: gcp:bigquery:Table
properties:
deletionProtection: false
datasetId: ${default.datasetId}
tableId: sheet
materializedView:
query: <QUERY>

0 comments on commit 8a2ca11

Please sign in to comment.