-
Notifications
You must be signed in to change notification settings - Fork 544
feat: Support table name changes on basic transformer. #18833
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
Merged
kodiakhq
merged 10 commits into
main
from
mariano/add-table-name-changes-to-basic-transformer
Aug 2, 2024
Merged
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
6ea7afc
feat: Support table name changes on basic transformer.
marianogappa 35e4034
Merge branch 'main' into mariano/add-table-name-changes-to-basic-tran…
marianogappa 08239ad
Fix lint.
marianogappa a068337
Merge branch 'main' into mariano/add-table-name-changes-to-basic-tran…
marianogappa d49613f
Make vale happy.
marianogappa b0303dd
Merge branch 'main' into mariano/add-table-name-changes-to-basic-tran…
marianogappa 8ce54d3
Fix lint.
marianogappa 32789b5
Clarify templating capabilities in the docs.
marianogappa be50463
Revert to switch/case syntax for spec validation.
marianogappa 1c605b6
Merge branch 'main' into mariano/add-table-name-changes-to-basic-tran…
marianogappa File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,62 @@ | ||
| package tablenamechanger | ||
|
|
||
| import ( | ||
| "github.com/cloudquery/cloudquery/cli/internal/specs/v0" | ||
| "github.com/cloudquery/plugin-pb-go/pb/plugin/v3" | ||
| "github.com/cloudquery/plugin-sdk/v4/schema" | ||
| ) | ||
|
|
||
| // TableNameChanger manages table name changes due to transformers, on a per-destination basis. | ||
| // It can learn about table name changes and apply those changes on further steps. | ||
| type TableNameChanger struct { | ||
| tableNameChanges map[string]map[string]string | ||
| } | ||
|
|
||
| func New(destinationSpecs []specs.Destination) *TableNameChanger { | ||
| tnc := &TableNameChanger{ | ||
| tableNameChanges: make(map[string]map[string]string), | ||
| } | ||
| for _, destinationSpec := range destinationSpecs { | ||
| tnc.tableNameChanges[destinationSpec.Name] = make(map[string]string) | ||
| } | ||
| return tnc | ||
| } | ||
|
|
||
| func (c TableNameChanger) UpdateTableNames(destinationName string, tables map[string]bool) map[string]bool { | ||
| newTables := make(map[string]bool, len(tables)) | ||
| for oldTableName := range tables { | ||
| if newTableName, ok := c.tableNameChanges[destinationName][oldTableName]; ok { | ||
| delete(tables, oldTableName) | ||
| newTables[newTableName] = true | ||
| } else { | ||
| newTables[oldTableName] = true | ||
| } | ||
| } | ||
| return newTables | ||
| } | ||
|
|
||
| func (c TableNameChanger) UpdateTableName(destinationName string, oldTableName string) string { | ||
| if newTableName, ok := c.tableNameChanges[destinationName][oldTableName]; ok { | ||
| return newTableName | ||
| } | ||
| return oldTableName | ||
| } | ||
|
|
||
| func (c *TableNameChanger) LearnTableNameChange(destinationName, oldTableName string, schemaBytes []byte) error { | ||
| // Implicit assumption that table name changes are deterministic. | ||
| // Therefore, once we learned about a table name change, we don't need to learn it again. | ||
| if c.tableNameChanges[destinationName][oldTableName] != "" { | ||
| return nil | ||
| } | ||
|
|
||
| sc, err := plugin.NewSchemaFromBytes(schemaBytes) | ||
| if err != nil { | ||
| return err | ||
| } | ||
| newTableName, ok := sc.Metadata().GetValue(schema.MetadataTableName) | ||
| if !ok { | ||
| return nil // this would be an error, but let it fail at a more relevant step | ||
| } | ||
| c.tableNameChanges[destinationName][oldTableName] = newTableName | ||
| return nil | ||
| } | ||
149 changes: 149 additions & 0 deletions
149
cli/internal/tablenamechanger/table_name_changer_test.go
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,149 @@ | ||
| package tablenamechanger | ||
|
|
||
| import ( | ||
| "testing" | ||
|
|
||
| "github.com/apache/arrow/go/v17/arrow" | ||
| "github.com/cloudquery/cloudquery/cli/internal/specs/v0" | ||
| "github.com/cloudquery/plugin-pb-go/pb/plugin/v3" | ||
| "github.com/cloudquery/plugin-sdk/v4/schema" | ||
| "github.com/stretchr/testify/require" | ||
| ) | ||
|
|
||
| func TestTableNameChanger(t *testing.T) { | ||
| destinationSpecs := []specs.Destination{ | ||
| {Metadata: specs.Metadata{Name: "dest1"}}, | ||
| {Metadata: specs.Metadata{Name: "dest2"}}, | ||
| } | ||
|
|
||
| t.Run("UpdateTableNames", func(t *testing.T) { | ||
| tests := []struct { | ||
| name string | ||
| destinationName string | ||
| tables map[string]bool | ||
| expectedNewTables map[string]bool | ||
| }{ | ||
| { | ||
| name: "NoChanges", | ||
| destinationName: "dest1", | ||
| tables: map[string]bool{"table1": true, "table2": true}, | ||
| expectedNewTables: map[string]bool{"table1": true, "table2": true}, | ||
| }, | ||
| { | ||
| name: "WithChanges", | ||
| destinationName: "dest1", | ||
| tables: map[string]bool{"old_table1": true, "table2": true}, | ||
| expectedNewTables: map[string]bool{"new_table1": true, "table2": true}, | ||
| }, | ||
| } | ||
|
|
||
| tnc := New(destinationSpecs) | ||
| err := tnc.LearnTableNameChange("dest1", "old_table1", createSchemaBytes(t, "new_table1")) | ||
| require.NoError(t, err) | ||
|
|
||
| for _, tt := range tests { | ||
| t.Run(tt.name, func(t *testing.T) { | ||
| newTables := tnc.UpdateTableNames(tt.destinationName, tt.tables) | ||
| require.Equal(t, tt.expectedNewTables, newTables) | ||
| }) | ||
| } | ||
| }) | ||
|
|
||
| t.Run("UpdateTableName", func(t *testing.T) { | ||
| tests := []struct { | ||
| name string | ||
| destinationName string | ||
| oldTableName string | ||
| expectedNewName string | ||
| }{ | ||
| { | ||
| name: "NoChange", | ||
| destinationName: "dest1", | ||
| oldTableName: "table1", | ||
| expectedNewName: "table1", | ||
| }, | ||
| { | ||
| name: "WithChange", | ||
| destinationName: "dest1", | ||
| oldTableName: "old_table1", | ||
| expectedNewName: "new_table1", | ||
| }, | ||
| } | ||
|
|
||
| for _, tt := range tests { | ||
| t.Run(tt.name, func(t *testing.T) { | ||
| tnc := New(destinationSpecs) | ||
| err := tnc.LearnTableNameChange("dest1", "old_table1", createSchemaBytes(t, "new_table1")) | ||
| require.NoError(t, err) | ||
|
|
||
| newName := tnc.UpdateTableName(tt.destinationName, tt.oldTableName) | ||
| require.Equal(t, tt.expectedNewName, newName) | ||
| }) | ||
| } | ||
| }) | ||
|
|
||
| t.Run("LearnTableNameChange", func(t *testing.T) { | ||
| tests := []struct { | ||
| name string | ||
| learnDestName string | ||
| learnOldTableName string | ||
| learnNewTableName string | ||
| updateDestName string | ||
| updateOldTableName string | ||
| updateNewTableName string | ||
| }{ | ||
| { | ||
| name: "ValidChange", | ||
| learnDestName: "dest1", | ||
| learnOldTableName: "old_table", | ||
| learnNewTableName: "new_table", | ||
| updateDestName: "dest1", | ||
| updateOldTableName: "old_table", | ||
| updateNewTableName: "new_table", | ||
| }, | ||
| { | ||
| name: "NoChangeOnSameDestination", | ||
| learnDestName: "dest1", | ||
| learnOldTableName: "old_table", | ||
| learnNewTableName: "old_table", | ||
| updateDestName: "dest1", | ||
| updateOldTableName: "old_table", | ||
| updateNewTableName: "old_table", | ||
| }, | ||
| { | ||
| name: "ValidChangeButDoesntUpdateOnDifferentDestination", | ||
| learnDestName: "dest1", | ||
| learnOldTableName: "old_table", | ||
| learnNewTableName: "new_table", | ||
| updateDestName: "dest2", | ||
| updateOldTableName: "old_table", | ||
| updateNewTableName: "old_table", | ||
| }, | ||
| } | ||
|
|
||
| for _, tt := range tests { | ||
| t.Run(tt.name, func(t *testing.T) { | ||
| tnc := New(destinationSpecs) | ||
|
|
||
| err := tnc.LearnTableNameChange(tt.learnDestName, tt.learnOldTableName, createSchemaBytes(t, tt.learnNewTableName)) | ||
| require.NoError(t, err) | ||
| newName := tnc.UpdateTableName(tt.updateDestName, tt.updateOldTableName) | ||
| require.Equal(t, tt.updateNewTableName, newName) | ||
| }) | ||
| } | ||
| }) | ||
| } | ||
|
|
||
| func createSchemaBytes(t *testing.T, tableName string) []byte { | ||
| md := arrow.NewMetadata([]string{schema.MetadataTableName}, []string{tableName}) | ||
| sc := arrow.NewSchema( | ||
| []arrow.Field{ | ||
| {Name: "col1", Type: arrow.BinaryTypes.String, Nullable: true}, | ||
| {Name: "col2", Type: arrow.BinaryTypes.String, Nullable: true}, | ||
| }, | ||
| &md, | ||
| ) | ||
| bytes, err := plugin.SchemaToBytes(sc) | ||
| require.NoError(t, err) | ||
| return bytes | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.