Skip to content

Commit 8613492

Browse files
authored
Add custom timeout for tmpdb's database drops (#214)
1 parent 2f42325 commit 8613492

File tree

1 file changed

+13
-0
lines changed

1 file changed

+13
-0
lines changed

pkg/tempdb/factory.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ type (
6262
metadataTable string
6363
logger log.Logger
6464
rootDatabase string
65+
dropTimeout time.Duration
6566
}
6667

6768
OnInstanceFactoryOpt func(*onInstanceFactoryOptions)
@@ -102,6 +103,13 @@ func WithRootDatabase(db string) OnInstanceFactoryOpt {
102103
}
103104
}
104105

106+
// WithDropTimeout sets the timeout used when dropping database
107+
func WithDropTimeout(d time.Duration) OnInstanceFactoryOpt {
108+
return func(opts *onInstanceFactoryOptions) {
109+
opts.dropTimeout = d
110+
}
111+
}
112+
105113
type (
106114
CreateConnPoolForDbFn func(ctx context.Context, dbName string) (*sql.DB, error)
107115

@@ -129,6 +137,7 @@ func NewOnInstanceFactory(ctx context.Context, createConnPoolForDb CreateConnPoo
129137
dbPrefix: DefaultOnInstanceDbPrefix,
130138
metadataSchema: DefaultOnInstanceMetadataSchema,
131139
metadataTable: DefaultOnInstanceMetadataTable,
140+
dropTimeout: DefaultStatementTimeout,
132141
rootDatabase: "postgres",
133142
logger: log.SimpleLogger(),
134143
}
@@ -259,6 +268,10 @@ func (o *onInstanceFactory) dropTempDatabase(ctx context.Context, dbName string)
259268
}
260269
defer rootConn.Close()
261270

271+
if _, err := rootConn.ExecContext(ctx, fmt.Sprintf("SET SESSION statement_timeout = %d;", o.options.dropTimeout.Milliseconds())); err != nil {
272+
return fmt.Errorf("setting statement timeout: %w", err)
273+
}
274+
262275
_, err = rootConn.ExecContext(ctx, fmt.Sprintf("DROP DATABASE %s;", dbName))
263276
if err != nil {
264277
return fmt.Errorf("dropping temporary database: %w", err)

0 commit comments

Comments
 (0)