forked from testcontainers/testcontainers-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsql.go
118 lines (100 loc) · 2.72 KB
/
sql.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
package wait
import (
"context"
"database/sql"
"fmt"
"time"
"github.com/docker/go-connections/nat"
)
var (
_ Strategy = (*waitForSql)(nil)
_ StrategyTimeout = (*waitForSql)(nil)
)
const defaultForSqlQuery = "SELECT 1"
// ForSQL constructs a new waitForSql strategy for the given driver
func ForSQL(port nat.Port, driver string, url func(host string, port nat.Port) string) *waitForSql {
return &waitForSql{
Port: port,
URL: url,
Driver: driver,
startupTimeout: defaultStartupTimeout(),
PollInterval: defaultPollInterval(),
query: defaultForSqlQuery,
}
}
type waitForSql struct {
timeout *time.Duration
URL func(host string, port nat.Port) string
Driver string
Port nat.Port
startupTimeout time.Duration
PollInterval time.Duration
query string
}
// WithStartupTimeout can be used to change the default startup timeout
func (w *waitForSql) WithStartupTimeout(timeout time.Duration) *waitForSql {
w.timeout = &timeout
return w
}
// WithPollInterval can be used to override the default polling interval of 100 milliseconds
func (w *waitForSql) WithPollInterval(pollInterval time.Duration) *waitForSql {
w.PollInterval = pollInterval
return w
}
// WithQuery can be used to override the default query used in the strategy.
func (w *waitForSql) WithQuery(query string) *waitForSql {
w.query = query
return w
}
func (w *waitForSql) Timeout() *time.Duration {
return w.timeout
}
// WaitUntilReady repeatedly tries to run "SELECT 1" or user defined query on the given port using sql and driver.
//
// If it doesn't succeed until the timeout value which defaults to 60 seconds, it will return an error.
func (w *waitForSql) WaitUntilReady(ctx context.Context, target StrategyTarget) error {
timeout := defaultStartupTimeout()
if w.timeout != nil {
timeout = *w.timeout
}
ctx, cancel := context.WithTimeout(ctx, timeout)
defer cancel()
host, err := target.Host(ctx)
if err != nil {
return err
}
ticker := time.NewTicker(w.PollInterval)
defer ticker.Stop()
var port nat.Port
port, err = target.MappedPort(ctx, w.Port)
for port == "" {
select {
case <-ctx.Done():
return fmt.Errorf("%w: %w", ctx.Err(), err)
case <-ticker.C:
if err := checkTarget(ctx, target); err != nil {
return err
}
port, err = target.MappedPort(ctx, w.Port)
}
}
db, err := sql.Open(w.Driver, w.URL(host, port))
if err != nil {
return fmt.Errorf("sql.Open: %w", err)
}
defer db.Close()
for {
select {
case <-ctx.Done():
return ctx.Err()
case <-ticker.C:
if err := checkTarget(ctx, target); err != nil {
return err
}
if _, err := db.ExecContext(ctx, w.query); err != nil {
continue
}
return nil
}
}
}