forked from djui/await
-
Notifications
You must be signed in to change notification settings - Fork 0
/
postgresql.go
122 lines (102 loc) · 2.48 KB
/
postgresql.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
119
120
121
122
package main
import (
"context"
"database/sql"
"errors"
"fmt"
"net/url"
"strings"
_ "github.com/lib/pq" // Register Postgres driver
)
type postgresqlResource struct {
url.URL
}
func (r *postgresqlResource) Await(ctx context.Context) error {
opts := parseFragment(r.URL.Fragment)
database := strings.TrimPrefix(r.URL.Path, "/")
if strings.Contains(database, "/") {
return fmt.Errorf("invalid database name: %s", database)
}
if database == "" {
if _, ok := opts["tables"]; ok {
return fmt.Errorf("database name required for awaiting tables")
}
// Special database default which usually exists.
database = "information_schema"
}
// Disable TLS/SSL by default
query, err := url.ParseQuery(r.URL.RawQuery)
if err != nil {
return err
}
if query.Get("sslmode") == "" {
query.Set("sslmode", "disable")
}
dsnURL := r.URL
dsnURL.Fragment = ""
dsnURL.Path = database
dsnURL.RawQuery = query.Encode()
dsn := dsnURL.String()
db, err := sql.Open(dsnURL.Scheme, dsn)
if err != nil {
return err
}
defer func() { _ = db.Close() }()
if err := db.Ping(); err != nil {
return &unavailabilityError{err}
}
if val, ok := opts["tables"]; ok {
var tables []string
if len(val) > 0 && val[0] != "" {
tables = strings.Split(val[0], ",")
}
if err := awaitPostgreSQLTables(db, database, tables); err != nil {
return err
}
}
return nil
}
func awaitPostgreSQLTables(db *sql.DB, dbName string, tables []string) error {
if len(tables) == 0 {
const stmt = `SELECT count(*) FROM information_schema.tables WHERE table_catalog=$1 AND table_schema='public'`
var tableCnt int
if err := db.QueryRow(stmt, dbName).Scan(&tableCnt); err != nil {
return err
}
if tableCnt == 0 {
return &unavailabilityError{errors.New("no tables found")}
}
return nil
}
const stmt = `SELECT table_name FROM information_schema.tables WHERE table_catalog=$1 AND table_schema='public'`
rows, err := db.Query(stmt, dbName)
if err != nil {
return err
}
defer func() { _ = rows.Close() }()
var actualTables []string
for rows.Next() {
var t string
if err := rows.Scan(&t); err != nil {
return err
}
actualTables = append(actualTables, t)
}
if err := rows.Err(); err != nil {
return err
}
contains := func(l []string, s string) bool {
for _, i := range l {
if i == s {
return true
}
}
return false
}
for _, t := range tables {
if !contains(actualTables, t) {
return &unavailabilityError{fmt.Errorf("table not found: %s", t)}
}
}
return nil
}