Skip to content

Commit a85d11b

Browse files
authored
Support passing in a connection string for postgres databases (#325)
1 parent 87cce88 commit a85d11b

File tree

8 files changed

+30
-45
lines changed

8 files changed

+30
-45
lines changed

.github/workflows/mysql.yaml

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -47,15 +47,12 @@ jobs:
4747
WARRANT_CHECK_MAXCONCURRENCY: 1000
4848
WARRANT_CHECK_TIMEOUT: 1m
4949
WARRANT_DATASTORE: mysql
50-
WARRANT_DATASTORE_MYSQL_USERNAME: root
51-
WARRANT_DATASTORE_MYSQL_PASSWORD: root
52-
WARRANT_DATASTORE_MYSQL_HOSTNAME: 127.0.0.1
53-
WARRANT_DATASTORE_MYSQL_DATABASE: warrant
50+
WARRANT_DATASTORE_MYSQL_DSN: root:root@tcp(127.0.0.1:3306)/warrant?parseTime=true
5451
WARRANT_DATASTORE_MYSQL_MAXIDLECONNECTIONS: 5
5552
WARRANT_DATASTORE_MYSQL_MAXOPENCONNECTIONS: 5
5653
WARRANT_DATASTORE_MYSQL_CONNMAXIDLETIME: 4h
5754
WARRANT_DATASTORE_MYSQL_CONNMAXLIFETIME: 6h
58-
WARRANT_DATASTORE_MYSQL_READERHOSTNAME: 127.0.0.1
55+
WARRANT_DATASTORE_MYSQL_READERDSN: root:root@tcp(127.0.0.1:3306)/warrant?parseTime=true
5956
WARRANT_DATASTORE_MYSQL_READERMAXIDLECONNECTIONS: 5
6057
WARRANT_DATASTORE_MYSQL_READERMAXOPENCONNECTIONS: 5
6158
- name: Run apirunner tests

.github/workflows/postgres.yaml

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -52,16 +52,12 @@ jobs:
5252
WARRANT_CHECK_MAXCONCURRENCY: 1000
5353
WARRANT_CHECK_TIMEOUT: 1m
5454
WARRANT_DATASTORE: postgres
55-
WARRANT_DATASTORE_POSTGRES_USERNAME: warrant_user
56-
WARRANT_DATASTORE_POSTGRES_PASSWORD: db_password
57-
WARRANT_DATASTORE_POSTGRES_HOSTNAME: localhost
58-
WARRANT_DATASTORE_POSTGRES_DATABASE: warrant
59-
WARRANT_DATASTORE_POSTGRES_SSLMODE: disable
55+
WARRANT_DATASTORE_POSTGRES_DSN: postgresql://warrant_user:db_password@localhost:5432/warrant?sslmode=disable
6056
WARRANT_DATASTORE_POSTGRES_MAXIDLECONNECTIONS: 5
6157
WARRANT_DATASTORE_POSTGRES_MAXOPENCONNECTIONS: 5
6258
WARRANT_DATASTORE_POSTGRES_CONNMAXIDLETIME: 4h
6359
WARRANT_DATASTORE_POSTGRES_CONNMAXLIFETIME: 6h
64-
WARRANT_DATASTORE_POSTGRES_READERHOSTNAME: localhost
60+
WARRANT_DATASTORE_POSTGRES_READERDSN: postgresql://warrant_user:db_password@localhost:5432/warrant?sslmode=disable
6561
WARRANT_DATASTORE_POSTGRES_READERMAXIDLECONNECTIONS: 5
6662
WARRANT_DATASTORE_POSTGRES_READERMAXOPENCONNECTIONS: 5
6763
- name: Run apirunner tests

cmd/warrant/main.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ func (env *ServiceEnv) InitDB(cfg config.Config) error {
7474
return nil
7575
}
7676

77-
if cfg.GetDatastore().GetPostgres().Hostname != "" {
77+
if cfg.GetDatastore().GetPostgres().Hostname != "" || cfg.GetDatastore().GetPostgres().DSN != "" {
7878
db := database.NewPostgres(*cfg.GetDatastore().GetPostgres())
7979
err := db.Connect(ctx)
8080
if err != nil {

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ require (
1212
github.com/google/uuid v1.6.0
1313
github.com/gorilla/mux v1.8.1
1414
github.com/jmoiron/sqlx v1.4.0
15-
github.com/lib/pq v1.10.9
1615
github.com/mattn/go-sqlite3 v1.14.22
1716
github.com/pkg/errors v0.9.1
1817
github.com/rs/zerolog v1.32.0
@@ -33,6 +32,7 @@ require (
3332
github.com/hashicorp/go-multierror v1.1.1 // indirect
3433
github.com/hashicorp/hcl v1.0.0 // indirect
3534
github.com/leodido/go-urn v1.4.0 // indirect
35+
github.com/lib/pq v1.10.9 // indirect
3636
github.com/magiconair/properties v1.8.7 // indirect
3737
github.com/mattn/go-colorable v0.1.13 // indirect
3838
github.com/mattn/go-isatty v0.0.19 // indirect

pkg/config/config.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,8 @@ type PostgresConfig struct {
137137
ReaderHostname string `mapstructure:"readerHostname"`
138138
ReaderMaxIdleConnections int `mapstructure:"readerMaxIdleConnections"`
139139
ReaderMaxOpenConnections int `mapstructure:"readerMaxOpenConnections"`
140+
DSN string `mapstructure:"dsn"`
141+
ReaderDSN string `mapstructure:"readerDsn"`
140142
}
141143

142144
type SQLiteConfig struct {

pkg/database/mysql.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ func NewMySQL(config config.MySQLConfig) *MySQL {
4242
}
4343
}
4444

45-
func (ds MySQL) Type() string {
45+
func (ds *MySQL) Type() string {
4646
return TypeMySQL
4747
}
4848

@@ -122,7 +122,7 @@ func (ds *MySQL) Connect(ctx context.Context) error {
122122
return nil
123123
}
124124

125-
func (ds MySQL) Migrate(ctx context.Context, toVersion uint) error {
125+
func (ds *MySQL) Migrate(ctx context.Context, toVersion uint) error {
126126
log.Info().Msgf("init: migrating mysql database %s", ds.Config.Database)
127127
// migrate database to latest schema
128128
mig, err := migrate.New(
@@ -159,7 +159,7 @@ func (ds MySQL) Migrate(ctx context.Context, toVersion uint) error {
159159
return nil
160160
}
161161

162-
func (ds MySQL) Ping(ctx context.Context) error {
162+
func (ds *MySQL) Ping(ctx context.Context) error {
163163
err := ds.Writer.PingContext(ctx)
164164
if err != nil {
165165
return errors.Wrap(err, "Error while attempting to ping mysql database")

pkg/database/postgres.go

Lines changed: 16 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@ import (
2828
_ "github.com/golang-migrate/migrate/v4/database/postgres"
2929
_ "github.com/golang-migrate/migrate/v4/source/file"
3030
_ "github.com/golang-migrate/migrate/v4/source/github"
31-
"github.com/lib/pq"
3231
"github.com/warrant-dev/warrant/pkg/config"
3332
)
3433

@@ -44,34 +43,20 @@ func NewPostgres(config config.PostgresConfig) *Postgres {
4443
}
4544
}
4645

47-
func (ds Postgres) Type() string {
46+
func (ds *Postgres) Type() string {
4847
return TypePostgres
4948
}
5049

5150
func (ds *Postgres) Connect(ctx context.Context) error {
5251
var db *sqlx.DB
5352
var err error
5453

55-
// open new database connection without specifying the database name
56-
usernamePassword := url.UserPassword(ds.Config.Username, ds.Config.Password).String()
57-
db, err = sqlx.Open("postgres", fmt.Sprintf("postgres://%s@%s/?sslmode=%s", usernamePassword, ds.Config.Hostname, ds.Config.SSLMode))
58-
if err != nil {
59-
return errors.Wrap(err, fmt.Sprintf("Unable to establish connection to postgres database %s. Shutting down server.", ds.Config.Database))
60-
}
61-
62-
// create database if it does not already exist
63-
_, err = db.ExecContext(ctx, fmt.Sprintf("CREATE DATABASE %s", ds.Config.Database))
64-
if err != nil {
65-
pgErr, ok := err.(*pq.Error)
66-
if ok && pgErr.Code.Name() != "duplicate_database" {
67-
return errors.Wrap(err, fmt.Sprintf("Unable to create postgres database %s", ds.Config.Database))
68-
}
54+
if ds.Config.DSN != "" {
55+
db, err = sqlx.Open("postgres", ds.Config.DSN)
56+
} else {
57+
usernamePassword := url.UserPassword(ds.Config.Username, ds.Config.Password).String()
58+
db, err = sqlx.Open("postgres", fmt.Sprintf("postgres://%s@%s/%s?sslmode=%s", usernamePassword, ds.Config.Hostname, ds.Config.Database, ds.Config.SSLMode))
6959
}
70-
71-
db.Close()
72-
73-
// open new database connection, this time specifying the database name
74-
db, err = sqlx.Open("postgres", fmt.Sprintf("postgres://%s@%s/%s?sslmode=%s", usernamePassword, ds.Config.Hostname, ds.Config.Database, ds.Config.SSLMode))
7560
if err != nil {
7661
return errors.Wrap(err, fmt.Sprintf("Unable to establish connection to postgres database %s. Shutting down server.", ds.Config.Database))
7762
}
@@ -101,8 +86,14 @@ func (ds *Postgres) Connect(ctx context.Context) error {
10186
ds.Config.Database, ds.Config.MaxIdleConnections, ds.Config.ConnMaxIdleTime, ds.Config.MaxOpenConnections, ds.Config.ConnMaxLifetime)
10287

10388
// connect to reader if provided
104-
if ds.Config.ReaderHostname != "" {
105-
reader, err := sqlx.Open("postgres", fmt.Sprintf("postgres://%s@%s/%s?sslmode=%s", usernamePassword, ds.Config.ReaderHostname, ds.Config.Database, ds.Config.SSLMode))
89+
if ds.Config.ReaderHostname != "" || ds.Config.ReaderDSN != "" {
90+
var reader *sqlx.DB
91+
if ds.Config.ReaderDSN != "" {
92+
reader, err = sqlx.Open("postgres", ds.Config.ReaderDSN)
93+
} else {
94+
usernamePassword := url.UserPassword(ds.Config.Username, ds.Config.Password).String()
95+
reader, err = sqlx.Open("postgres", fmt.Sprintf("postgres://%s@%s/%s?sslmode=%s", usernamePassword, ds.Config.ReaderHostname, ds.Config.Database, ds.Config.SSLMode))
96+
}
10697
if err != nil {
10798
return errors.Wrap(err, fmt.Sprintf("Unable to establish connection to postgres reader %s. Shutting down server.", ds.Config.Database))
10899
}
@@ -126,7 +117,6 @@ func (ds *Postgres) Connect(ctx context.Context) error {
126117

127118
// map struct attributes to db column names
128119
reader.Mapper = reflectx.NewMapperFunc("postgres", func(s string) string { return s })
129-
130120
ds.Reader = reader
131121
log.Info().Msgf("init: connected to postgres reader database %s [maxIdleConns: %d, connMaxIdleTime: %s, maxOpenConns: %d, connMaxLifetime: %s]",
132122
ds.Config.Database, ds.Config.ReaderMaxIdleConnections, ds.Config.ConnMaxIdleTime, ds.Config.ReaderMaxOpenConnections, ds.Config.ConnMaxLifetime)
@@ -135,7 +125,7 @@ func (ds *Postgres) Connect(ctx context.Context) error {
135125
return nil
136126
}
137127

138-
func (ds Postgres) Migrate(ctx context.Context, toVersion uint) error {
128+
func (ds *Postgres) Migrate(ctx context.Context, toVersion uint) error {
139129
log.Info().Msgf("init: migrating postgres database %s", ds.Config.Database)
140130
// migrate database to latest schema
141131
usernamePassword := url.UserPassword(ds.Config.Username, ds.Config.Password).String()
@@ -173,7 +163,7 @@ func (ds Postgres) Migrate(ctx context.Context, toVersion uint) error {
173163
return nil
174164
}
175165

176-
func (ds Postgres) Ping(ctx context.Context) error {
166+
func (ds *Postgres) Ping(ctx context.Context) error {
177167
err := ds.Writer.PingContext(ctx)
178168
if err != nil {
179169
return errors.Wrap(err, "Error while attempting to ping postgres database")

pkg/database/sqlite.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ func NewSQLite(config config.SQLiteConfig) *SQLite {
4646
}
4747
}
4848

49-
func (ds SQLite) Type() string {
49+
func (ds *SQLite) Type() string {
5050
return TypeSQLite
5151
}
5252

@@ -94,7 +94,7 @@ func (ds *SQLite) Connect(ctx context.Context) error {
9494
return nil
9595
}
9696

97-
func (ds SQLite) Migrate(ctx context.Context, toVersion uint) error {
97+
func (ds *SQLite) Migrate(ctx context.Context, toVersion uint) error {
9898
log.Info().Msgf("init: migrating sqlite database %s", ds.Config.Database)
9999
// migrate database to latest schema
100100
instance, err := sqlite3.WithInstance(ds.Writer.DB, &sqlite3.Config{})
@@ -136,6 +136,6 @@ func (ds SQLite) Migrate(ctx context.Context, toVersion uint) error {
136136
return nil
137137
}
138138

139-
func (ds SQLite) Ping(ctx context.Context) error {
139+
func (ds *SQLite) Ping(ctx context.Context) error {
140140
return ds.Writer.PingContext(ctx)
141141
}

0 commit comments

Comments
 (0)