-
Notifications
You must be signed in to change notification settings - Fork 90
/
Copy pathprepare_database.go
173 lines (137 loc) · 4.41 KB
/
prepare_database.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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
package embeddedpostgres
import (
"context"
"database/sql"
"errors"
"fmt"
"io"
"os"
"os/exec"
"path/filepath"
"github.com/lib/pq"
)
const (
fmtCloseDBConn = "unable to close database connection: %w"
fmtAfterError = "%v happened after error: %w"
)
type initDatabase func(binaryExtractLocation, runtimePath, pgDataDir, username, password, locale string, encoding string, logger *os.File) error
type createDatabase func(port uint32, username, password, database string) error
func defaultInitDatabase(binaryExtractLocation, runtimePath, pgDataDir, username, password, locale string, encoding string, logger *os.File) error {
passwordFile, err := createPasswordFile(runtimePath, password)
if err != nil {
return err
}
args := []string{
"-A", "password",
"-U", username,
"-D", pgDataDir,
fmt.Sprintf("--pwfile=%s", passwordFile),
}
if locale != "" {
args = append(args, fmt.Sprintf("--locale=%s", locale))
}
if encoding != "" {
args = append(args, fmt.Sprintf("--encoding=%s", encoding))
}
postgresInitDBBinary := filepath.Join(binaryExtractLocation, "bin/initdb")
postgresInitDBProcess := exec.Command(postgresInitDBBinary, args...)
postgresInitDBProcess.Stderr = logger
postgresInitDBProcess.Stdout = logger
if err = postgresInitDBProcess.Run(); err != nil {
logContent, readLogsErr := readLogsOrTimeout(logger) // we want to preserve the original error
if readLogsErr != nil {
logContent = []byte(string(logContent) + " - " + readLogsErr.Error())
}
return fmt.Errorf("unable to init database using '%s': %w\n%s", postgresInitDBProcess.String(), err, string(logContent))
}
if err = os.Remove(passwordFile); err != nil {
return fmt.Errorf("unable to remove password file '%v': %w", passwordFile, err)
}
return nil
}
func createPasswordFile(runtimePath, password string) (string, error) {
passwordFileLocation := filepath.Join(runtimePath, "pwfile")
if err := os.WriteFile(passwordFileLocation, []byte(password), 0600); err != nil {
return "", fmt.Errorf("unable to write password file to %s", passwordFileLocation)
}
return passwordFileLocation, nil
}
func defaultCreateDatabase(port uint32, username, password, database string) (err error) {
if database == "postgres" {
return nil
}
conn, err := openDatabaseConnection(port, username, password, "postgres")
if err != nil {
return errorCustomDatabase(database, err)
}
db := sql.OpenDB(conn)
defer func() {
err = connectionClose(db, err)
}()
if _, err := db.Exec(fmt.Sprintf("CREATE DATABASE \"%s\"", database)); err != nil {
return errorCustomDatabase(database, err)
}
return nil
}
// connectionClose closes the database connection and handles the error of the function that used the database connection
func connectionClose(db io.Closer, err error) error {
closeErr := db.Close()
if closeErr != nil {
closeErr = fmt.Errorf(fmtCloseDBConn, closeErr)
if err != nil {
err = fmt.Errorf(fmtAfterError, closeErr, err)
} else {
err = closeErr
}
}
return err
}
func healthCheckDatabaseOrTimeout(config Config) error {
healthCheckSignal := make(chan bool)
defer close(healthCheckSignal)
timeout, cancelFunc := context.WithTimeout(context.Background(), config.startTimeout)
defer cancelFunc()
go func() {
for timeout.Err() == nil {
if err := healthCheckDatabase(config.port, config.database, config.username, config.password); err != nil {
continue
}
healthCheckSignal <- true
break
}
}()
select {
case <-healthCheckSignal:
return nil
case <-timeout.Done():
return errors.New("timed out waiting for database to become available")
}
}
func healthCheckDatabase(port uint32, database, username, password string) (err error) {
conn, err := openDatabaseConnection(port, username, password, database)
if err != nil {
return err
}
db := sql.OpenDB(conn)
defer func() {
err = connectionClose(db, err)
}()
if _, err := db.Query("SELECT 1"); err != nil {
return err
}
return nil
}
func openDatabaseConnection(port uint32, username string, password string, database string) (*pq.Connector, error) {
conn, err := pq.NewConnector(fmt.Sprintf("host=localhost port=%d user=%s password=%s dbname=%s sslmode=disable",
port,
username,
password,
database))
if err != nil {
return nil, err
}
return conn, nil
}
func errorCustomDatabase(database string, err error) error {
return fmt.Errorf("unable to connect to create database with custom name %s with the following error: %s", database, err)
}