-
Notifications
You must be signed in to change notification settings - Fork 30
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
[DDL] Cleaning up primary key creation #1026
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
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 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 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 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 |
---|---|---|
@@ -1,123 +1,41 @@ | ||
package ddl_test | ||
package ddl | ||
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. Note the pkg name change. I moved all the other tests out so that we can have this be Once this PR merges, I'm going to try to remove the need for us to specify a different package name. |
||
|
||
import ( | ||
"fmt" | ||
"testing" | ||
|
||
"github.com/artie-labs/transfer/lib/config/constants" | ||
"github.com/artie-labs/transfer/lib/destination" | ||
"github.com/artie-labs/transfer/lib/destination/ddl" | ||
"github.com/artie-labs/transfer/lib/kafkalib" | ||
"github.com/artie-labs/transfer/lib/mocks" | ||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func (d *DDLTestSuite) Test_DropTemporaryTableCaseSensitive() { | ||
tablesToDrop := []string{ | ||
"foo", | ||
"abcdef", | ||
"gghh", | ||
} | ||
"github.com/artie-labs/transfer/lib/config" | ||
"github.com/artie-labs/transfer/lib/typing" | ||
"github.com/artie-labs/transfer/lib/typing/columns" | ||
) | ||
|
||
for i, dest := range []destination.DataWarehouse{d.bigQueryStore, d.snowflakeStagesStore} { | ||
var fakeStore *mocks.FakeStore | ||
if i == 0 { | ||
fakeStore = d.fakeBigQueryStore | ||
} else { | ||
fakeStore = d.fakeSnowflakeStagesStore | ||
func TestShouldCreatePrimaryKey(t *testing.T) { | ||
pk := columns.NewColumn("foo", typing.String) | ||
pk.SetPrimaryKeyForTest(true) | ||
{ | ||
// Primary key check | ||
{ | ||
// Column is not a primary key | ||
col := columns.NewColumn("foo", typing.String) | ||
assert.False(t, shouldCreatePrimaryKey(col, config.Replication, true)) | ||
} | ||
|
||
for tableIndex, table := range tablesToDrop { | ||
tableIdentifier := dest.IdentifierFor(kafkalib.TopicConfig{}, fmt.Sprintf("%s_%s", table, constants.ArtiePrefix)) | ||
_ = ddl.DropTemporaryTable(dest, tableIdentifier, false) | ||
|
||
// There should be the same number of DROP table calls as the number of tables to drop. | ||
assert.Equal(d.T(), tableIndex+1, fakeStore.ExecCallCount()) | ||
query, _ := fakeStore.ExecArgsForCall(tableIndex) | ||
assert.Equal(d.T(), fmt.Sprintf("DROP TABLE IF EXISTS %s", tableIdentifier.FullyQualifiedName()), query) | ||
{ | ||
// Column is a primary key | ||
assert.True(t, shouldCreatePrimaryKey(pk, config.Replication, true)) | ||
} | ||
} | ||
} | ||
|
||
func (d *DDLTestSuite) Test_DropTemporaryTable() { | ||
doNotDropTables := []string{ | ||
"foo", | ||
"bar", | ||
"abcd", | ||
"customers.customers", | ||
} | ||
|
||
// Should not drop since these do not have Artie prefix in the name. | ||
for _, table := range doNotDropTables { | ||
tableID := d.bigQueryStore.IdentifierFor(kafkalib.TopicConfig{}, table) | ||
_ = ddl.DropTemporaryTable(d.snowflakeStagesStore, tableID, false) | ||
assert.Equal(d.T(), 0, d.fakeSnowflakeStagesStore.ExecCallCount()) | ||
} | ||
|
||
for i, _dwh := range []destination.DataWarehouse{d.bigQueryStore, d.snowflakeStagesStore} { | ||
var fakeStore *mocks.FakeStore | ||
if i == 0 { | ||
fakeStore = d.fakeBigQueryStore | ||
} else { | ||
fakeStore = d.fakeSnowflakeStagesStore | ||
|
||
} | ||
|
||
for _, doNotDropTable := range doNotDropTables { | ||
doNotDropTableID := d.bigQueryStore.IdentifierFor(kafkalib.TopicConfig{}, doNotDropTable) | ||
_ = ddl.DropTemporaryTable(_dwh, doNotDropTableID, false) | ||
|
||
assert.Equal(d.T(), 0, fakeStore.ExecCallCount()) | ||
} | ||
|
||
for index, table := range doNotDropTables { | ||
fullTableID := d.bigQueryStore.IdentifierFor(kafkalib.TopicConfig{}, fmt.Sprintf("%s_%s", table, constants.ArtiePrefix)) | ||
_ = ddl.DropTemporaryTable(_dwh, fullTableID, false) | ||
|
||
count := index + 1 | ||
assert.Equal(d.T(), count, fakeStore.ExecCallCount()) | ||
|
||
query, _ := fakeStore.ExecArgsForCall(index) | ||
assert.Equal(d.T(), fmt.Sprintf("DROP TABLE IF EXISTS %s", fullTableID.FullyQualifiedName()), query) | ||
} | ||
{ | ||
// False because it's history mode | ||
// It should be false because we are appending rows to this table. | ||
assert.False(t, shouldCreatePrimaryKey(pk, config.History, true)) | ||
} | ||
} | ||
|
||
func (d *DDLTestSuite) Test_DropTemporaryTable_Errors() { | ||
tablesToDrop := []string{ | ||
"foo", | ||
"bar", | ||
"abcd", | ||
"customers.customers", | ||
{ | ||
// False because it's not a create table operation | ||
assert.False(t, shouldCreatePrimaryKey(pk, config.Replication, false)) | ||
} | ||
|
||
randomErr := fmt.Errorf("random err") | ||
for i, _dwh := range []destination.DataWarehouse{d.bigQueryStore, d.snowflakeStagesStore} { | ||
var fakeStore *mocks.FakeStore | ||
if i == 0 { | ||
fakeStore = d.fakeBigQueryStore | ||
d.fakeBigQueryStore.ExecReturns(nil, randomErr) | ||
} else { | ||
fakeStore = d.fakeSnowflakeStagesStore | ||
d.fakeSnowflakeStagesStore.ExecReturns(nil, randomErr) | ||
} | ||
|
||
var count int | ||
for _, shouldReturnErr := range []bool{true, false} { | ||
for _, table := range tablesToDrop { | ||
tableID := d.bigQueryStore.IdentifierFor(kafkalib.TopicConfig{}, fmt.Sprintf("%s_%s", table, constants.ArtiePrefix)) | ||
err := ddl.DropTemporaryTable(_dwh, tableID, shouldReturnErr) | ||
if shouldReturnErr { | ||
assert.ErrorContains(d.T(), err, randomErr.Error()) | ||
} else { | ||
assert.NoError(d.T(), err) | ||
} | ||
|
||
count += 1 | ||
assert.Equal(d.T(), count, fakeStore.ExecCallCount()) | ||
query, _ := fakeStore.ExecArgsForCall(count - 1) | ||
assert.Equal(d.T(), fmt.Sprintf("DROP TABLE IF EXISTS %s", tableID.FullyQualifiedName()), query) | ||
} | ||
} | ||
|
||
{ | ||
// True because it's a primary key, replication mode, and create table operation | ||
assert.True(t, shouldCreatePrimaryKey(pk, config.Replication, true)) | ||
} | ||
} |
This file contains 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
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.
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.
No changes to these tests. I just moved them from
ddl_test.go