-
Notifications
You must be signed in to change notification settings - Fork 1.7k
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
Allow in-place column drop for bigquery table #10170
Changes from 5 commits
e5f2d41
c6f1e95
e8790e2
3e84fac
f58baf1
4c19ade
6f41b8e
68f9b14
93ca7f5
4c5d641
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -256,19 +256,16 @@ func bigQueryTableNormalizePolicyTags(val interface{}) interface{} { | |
|
||
// Compares two existing schema implementations and decides if | ||
// it is changeable.. pairs with a force new on not changeable | ||
func resourceBigQueryTableSchemaIsChangeable(old, new interface{}) (bool, error) { | ||
func resourceBigQueryTableSchemaIsChangeable(old, new interface{}, topLevel bool) (bool, error) { | ||
switch old.(type) { | ||
case []interface{}: | ||
arrayOld := old.([]interface{}) | ||
arrayNew, ok := new.([]interface{}) | ||
droppedColumns := 0 | ||
if !ok { | ||
// if not both arrays not changeable | ||
return false, nil | ||
} | ||
if len(arrayOld) > len(arrayNew) { | ||
// if not growing not changeable | ||
return false, nil | ||
} | ||
if err := bigQueryTablecheckNameExists(arrayOld); err != nil { | ||
return false, err | ||
} | ||
|
@@ -289,16 +286,21 @@ func resourceBigQueryTableSchemaIsChangeable(old, new interface{}) (bool, error) | |
} | ||
} | ||
for key := range mapOld { | ||
// all old keys should be represented in the new config | ||
// dropping top level columns can happen in-place | ||
if _, ok := mapNew[key]; !ok { | ||
return false, nil | ||
if !topLevel { | ||
return false, nil | ||
} | ||
droppedColumns += 1 | ||
continue | ||
} | ||
if isChangable, err := | ||
resourceBigQueryTableSchemaIsChangeable(mapOld[key], mapNew[key]); err != nil || !isChangable { | ||
resourceBigQueryTableSchemaIsChangeable(mapOld[key], mapNew[key], false); err != nil || !isChangable { | ||
return false, err | ||
} | ||
} | ||
return true, nil | ||
// only pure column drops allowed | ||
return (droppedColumns == 0) || (len(arrayOld) == len(arrayNew)+droppedColumns), nil | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Do you think this logic would be more self-descriptive if we also tracked There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think "changeableColumns" would be more precise since changing a column's mode would be allowed alongside column drops, let me know if you think we should change this to only column drops without other in-place changes (besides adding columns)
hmm not sure if I get it, if the condition is something like
then it wouldn't work correctly, for example when changing I think we can do something like:
what do you think? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Let's still include other in-place changes. Naming is hard though - the columns would have the same name but maybe different other attributes. "SameNameColumns"? Your updated logic looks good. |
||
case map[string]interface{}: | ||
objectOld := old.(map[string]interface{}) | ||
objectNew, ok := new.(map[string]interface{}) | ||
|
@@ -337,7 +339,7 @@ func resourceBigQueryTableSchemaIsChangeable(old, new interface{}) (bool, error) | |
return false, nil | ||
} | ||
case "fields": | ||
return resourceBigQueryTableSchemaIsChangeable(valOld, valNew) | ||
return resourceBigQueryTableSchemaIsChangeable(valOld, valNew, false) | ||
|
||
// other parameters: description, policyTags and | ||
// policyTags.names[] are changeable | ||
|
@@ -376,7 +378,7 @@ func resourceBigQueryTableSchemaCustomizeDiffFunc(d tpgresource.TerraformResourc | |
// same as above | ||
log.Printf("[DEBUG] unable to unmarshal json customized diff - %v", err) | ||
} | ||
isChangeable, err := resourceBigQueryTableSchemaIsChangeable(old, new) | ||
isChangeable, err := resourceBigQueryTableSchemaIsChangeable(old, new, true) | ||
if err != nil { | ||
return err | ||
} | ||
|
@@ -1703,6 +1705,12 @@ func resourceBigQueryTableRead(d *schema.ResourceData, meta interface{}) error { | |
return nil | ||
} | ||
|
||
type TableInfo struct { | ||
obada-ab marked this conversation as resolved.
Show resolved
Hide resolved
|
||
project string | ||
datasetID string | ||
tableID string | ||
} | ||
|
||
func resourceBigQueryTableUpdate(d *schema.ResourceData, meta interface{}) error { | ||
config := meta.(*transport_tpg.Config) | ||
userAgent, err := tpgresource.GenerateUserAgentString(d, config.UserAgent) | ||
|
@@ -1725,13 +1733,62 @@ func resourceBigQueryTableUpdate(d *schema.ResourceData, meta interface{}) error | |
datasetID := d.Get("dataset_id").(string) | ||
tableID := d.Get("table_id").(string) | ||
|
||
tableInfo := &TableInfo{ | ||
project: project, | ||
datasetID: datasetID, | ||
tableID: tableID, | ||
} | ||
|
||
if err = resourceBigQueryTableColumnDrop(config, userAgent, table, tableInfo); err != nil { | ||
return err | ||
} | ||
|
||
if _, err = config.NewBigQueryClient(userAgent).Tables.Update(project, datasetID, tableID, table).Do(); err != nil { | ||
return err | ||
} | ||
|
||
return resourceBigQueryTableRead(d, meta) | ||
} | ||
|
||
func resourceBigQueryTableColumnDrop(config *transport_tpg.Config, userAgent string, table *bigquery.Table, tableInfo *TableInfo) error { | ||
oldTable, err := config.NewBigQueryClient(userAgent).Tables.Get(tableInfo.project, tableInfo.datasetID, tableInfo.tableID).Do() | ||
if err != nil { | ||
return err | ||
} | ||
|
||
newTableFields := map[string]bool{} | ||
for _, field := range table.Schema.Fields { | ||
newTableFields[field.Name] = true | ||
} | ||
|
||
droppedColumns := []string{} | ||
for _, field := range oldTable.Schema.Fields { | ||
if !newTableFields[field.Name] { | ||
droppedColumns = append(droppedColumns, field.Name) | ||
} | ||
} | ||
|
||
if len(droppedColumns) > 0 { | ||
droppedColumnsString := strings.Join(droppedColumns, ", DROP COLUMN ") | ||
|
||
dropColumnsDDL := fmt.Sprintf("ALTER TABLE `%s.%s.%s` DROP COLUMN %s", tableInfo.project, tableInfo.datasetID, tableInfo.tableID, droppedColumnsString) | ||
log.Printf("[INFO] Dropping columns in-place: %s", dropColumnsDDL) | ||
|
||
useLegacySQL := false | ||
req := &bigquery.QueryRequest{ | ||
Query: dropColumnsDDL, | ||
UseLegacySql: &useLegacySQL, | ||
} | ||
|
||
_, err = config.NewBigQueryClient(userAgent).Jobs.Query(tableInfo.project, req).Do() | ||
if err != nil { | ||
return err | ||
} | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func resourceBigQueryTableDelete(d *schema.ResourceData, meta interface{}) error { | ||
if d.Get("deletion_protection").(bool) { | ||
return fmt.Errorf("cannot destroy instance without setting deletion_protection=false and running `terraform apply`") | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I thought we'd be updating this as we look through
mapOld
and comparing key tomapNew
- could you explain how this is working as intended?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
not sure I understand the question correctly, we are going through the keys in
mapOld
and comparing tomapNew
, but we're doing it recursively because of the nature of the schema (nested interfaces). We immediately return false (not changeable) if we encounter an unchangeable case, for example a column that had it's type changed.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I was referring to this block:
At a glance, in a single given level, top or nested,
sameNameColumns
will be 0 or 1. Additional comments on the variables and the logic here would help.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
sameNameColumns
is always 0 for any nested level, and could be 0 or more at the top level. I refactored this part of the code, it should still be the same logic but I think It's more readable now and less prone to accidental logic-breaking changes.