@@ -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
5150func (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" )
0 commit comments