Skip to content

Commit b85ae3f

Browse files
committed
Added a query status check to account for race conditions creating the table.
1 parent c294ad8 commit b85ae3f

File tree

3 files changed

+36
-4
lines changed

3 files changed

+36
-4
lines changed

gov2/redshift/actions/redshiftdata_actions.go

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ func (actor RedshiftDataActions) ListDatabases(ctx context.Context, clusterId st
8787
// snippet-start:[gov2.redshift.CreateTable]
8888

8989
// CreateTable creates a table named <tableName> in the <databaseName> database with the given arguments.
90-
func (actor RedshiftDataActions) CreateTable(ctx context.Context, clusterId string, databaseName string, tableName string, userName string, args []string) error {
90+
func (actor RedshiftDataActions) CreateTable(ctx context.Context, clusterId string, databaseName string, tableName string, userName string, pauser demotools.IPausable, args []string) (*redshiftdata.ExecuteStatementOutput, error) {
9191
sql := "CREATE TABLE " + tableName + " (" +
9292
"id bigint identity(1, 1), " +
9393
"PRIMARY KEY (id)"
@@ -108,11 +108,25 @@ func (actor RedshiftDataActions) CreateTable(ctx context.Context, clusterId stri
108108
log.Printf("Could not connect to the database.")
109109
} else if err != nil {
110110
log.Printf("Failed to create table: %v\n", err)
111-
return err
111+
return nil, err
112+
}
113+
114+
describeStatementInput := &redshiftdata.DescribeStatementInput{Id: output.Id}
115+
query := RedshiftQuery{
116+
Context: ctx,
117+
Input: *describeStatementInput,
118+
Result: output,
112119
}
113120

121+
err = actor.WaitForQueryStatus(query, pauser, true)
122+
if err != nil {
123+
log.Printf("Failed to execute query: %v\n", err)
124+
panic(err)
125+
}
126+
log.Printf("Successfully executed query\n")
127+
114128
log.Println("Table created:", *output.Id)
115-
return nil
129+
return output, nil
116130
}
117131

118132
// snippet-end:[gov2.redshift.CreateTable]

gov2/redshift/scenarios/redshift_basics.go

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -195,12 +195,28 @@ func (runner *RedshiftBasicsScenario) Run() {
195195
// Create the "Movies" table
196196
log.Println("Now you will create a table named " + tableName + ".")
197197
runner.questioner.Ask("Press Enter to continue...")
198-
err = runner.redshiftDataActor.CreateTable(ctx, clusterId, databaseName, tableName, user.Username, []string{"title VARCHAR(256)", "year INT"})
198+
err = nil
199+
result, err := runner.redshiftDataActor.CreateTable(ctx, clusterId, databaseName, tableName, user.Username, runner.pauser, []string{"title VARCHAR(256)", "year INT"})
199200
if err != nil {
200201
log.Printf("Failed to create table: %v\n", err)
201202
panic(err)
202203
}
203204

205+
describeInput := redshiftdata.DescribeStatementInput{
206+
Id: result.Id,
207+
}
208+
query := actions.RedshiftQuery{
209+
Context: ctx,
210+
Input: describeInput,
211+
Result: result,
212+
}
213+
err = runner.redshiftDataActor.WaitForQueryStatus(query, runner.pauser, true)
214+
if err != nil {
215+
log.Printf("Failed to execute query: %v\n", err)
216+
panic(err)
217+
}
218+
log.Printf("Successfully executed query\n")
219+
204220
// Populate the "Movies" table
205221
runner.PopulateMoviesTable(ctx, clusterId, databaseName, tableName, user.Username, fileName)
206222

gov2/redshift/scenarios/redshift_basics_test.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,8 @@ func (scenarioTest *BasicsScenarioTest) SetupDataAndStubs() []testtools.Stub {
102102
stubList = append(stubList, stubs.StubDescribeClusters(clusterId, nil))
103103
stubList = append(stubList, stubs.StubListDatabases(clusterId, databaseName, user.Username, nil))
104104
stubList = append(stubList, stubs.StubExecuteStatement(clusterId, databaseName, user.Username, sql, testId, nil))
105+
stubList = append(stubList, stubs.StubDescribeStatement(testId, nil))
106+
stubList = append(stubList, stubs.StubDescribeStatement(testId, nil))
105107
stubList = append(stubList, stubs.StubBatchExecuteStatement(clusterId, databaseName, user.Username, sqls, testId, nil))
106108
stubList = append(stubList, stubs.StubDescribeStatement(testId, nil))
107109
stubList = append(stubList, stubs.StubExecuteStatement(clusterId, databaseName, user.Username, sql, testId, nil))

0 commit comments

Comments
 (0)