Skip to content

Commit

Permalink
Support passing in a connection string for postgres databases (#325)
Browse files Browse the repository at this point in the history
  • Loading branch information
kkajla12 authored May 22, 2024
1 parent 87cce88 commit a85d11b
Show file tree
Hide file tree
Showing 8 changed files with 30 additions and 45 deletions.
7 changes: 2 additions & 5 deletions .github/workflows/mysql.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -47,15 +47,12 @@ jobs:
WARRANT_CHECK_MAXCONCURRENCY: 1000
WARRANT_CHECK_TIMEOUT: 1m
WARRANT_DATASTORE: mysql
WARRANT_DATASTORE_MYSQL_USERNAME: root
WARRANT_DATASTORE_MYSQL_PASSWORD: root
WARRANT_DATASTORE_MYSQL_HOSTNAME: 127.0.0.1
WARRANT_DATASTORE_MYSQL_DATABASE: warrant
WARRANT_DATASTORE_MYSQL_DSN: root:root@tcp(127.0.0.1:3306)/warrant?parseTime=true
WARRANT_DATASTORE_MYSQL_MAXIDLECONNECTIONS: 5
WARRANT_DATASTORE_MYSQL_MAXOPENCONNECTIONS: 5
WARRANT_DATASTORE_MYSQL_CONNMAXIDLETIME: 4h
WARRANT_DATASTORE_MYSQL_CONNMAXLIFETIME: 6h
WARRANT_DATASTORE_MYSQL_READERHOSTNAME: 127.0.0.1
WARRANT_DATASTORE_MYSQL_READERDSN: root:root@tcp(127.0.0.1:3306)/warrant?parseTime=true
WARRANT_DATASTORE_MYSQL_READERMAXIDLECONNECTIONS: 5
WARRANT_DATASTORE_MYSQL_READERMAXOPENCONNECTIONS: 5
- name: Run apirunner tests
Expand Down
8 changes: 2 additions & 6 deletions .github/workflows/postgres.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -52,16 +52,12 @@ jobs:
WARRANT_CHECK_MAXCONCURRENCY: 1000
WARRANT_CHECK_TIMEOUT: 1m
WARRANT_DATASTORE: postgres
WARRANT_DATASTORE_POSTGRES_USERNAME: warrant_user
WARRANT_DATASTORE_POSTGRES_PASSWORD: db_password
WARRANT_DATASTORE_POSTGRES_HOSTNAME: localhost
WARRANT_DATASTORE_POSTGRES_DATABASE: warrant
WARRANT_DATASTORE_POSTGRES_SSLMODE: disable
WARRANT_DATASTORE_POSTGRES_DSN: postgresql://warrant_user:db_password@localhost:5432/warrant?sslmode=disable
WARRANT_DATASTORE_POSTGRES_MAXIDLECONNECTIONS: 5
WARRANT_DATASTORE_POSTGRES_MAXOPENCONNECTIONS: 5
WARRANT_DATASTORE_POSTGRES_CONNMAXIDLETIME: 4h
WARRANT_DATASTORE_POSTGRES_CONNMAXLIFETIME: 6h
WARRANT_DATASTORE_POSTGRES_READERHOSTNAME: localhost
WARRANT_DATASTORE_POSTGRES_READERDSN: postgresql://warrant_user:db_password@localhost:5432/warrant?sslmode=disable
WARRANT_DATASTORE_POSTGRES_READERMAXIDLECONNECTIONS: 5
WARRANT_DATASTORE_POSTGRES_READERMAXOPENCONNECTIONS: 5
- name: Run apirunner tests
Expand Down
2 changes: 1 addition & 1 deletion cmd/warrant/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ func (env *ServiceEnv) InitDB(cfg config.Config) error {
return nil
}

if cfg.GetDatastore().GetPostgres().Hostname != "" {
if cfg.GetDatastore().GetPostgres().Hostname != "" || cfg.GetDatastore().GetPostgres().DSN != "" {
db := database.NewPostgres(*cfg.GetDatastore().GetPostgres())
err := db.Connect(ctx)
if err != nil {
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ require (
github.com/google/uuid v1.6.0
github.com/gorilla/mux v1.8.1
github.com/jmoiron/sqlx v1.4.0
github.com/lib/pq v1.10.9
github.com/mattn/go-sqlite3 v1.14.22
github.com/pkg/errors v0.9.1
github.com/rs/zerolog v1.32.0
Expand All @@ -33,6 +32,7 @@ require (
github.com/hashicorp/go-multierror v1.1.1 // indirect
github.com/hashicorp/hcl v1.0.0 // indirect
github.com/leodido/go-urn v1.4.0 // indirect
github.com/lib/pq v1.10.9 // indirect
github.com/magiconair/properties v1.8.7 // indirect
github.com/mattn/go-colorable v0.1.13 // indirect
github.com/mattn/go-isatty v0.0.19 // indirect
Expand Down
2 changes: 2 additions & 0 deletions pkg/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,8 @@ type PostgresConfig struct {
ReaderHostname string `mapstructure:"readerHostname"`
ReaderMaxIdleConnections int `mapstructure:"readerMaxIdleConnections"`
ReaderMaxOpenConnections int `mapstructure:"readerMaxOpenConnections"`
DSN string `mapstructure:"dsn"`
ReaderDSN string `mapstructure:"readerDsn"`
}

type SQLiteConfig struct {
Expand Down
6 changes: 3 additions & 3 deletions pkg/database/mysql.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ func NewMySQL(config config.MySQLConfig) *MySQL {
}
}

func (ds MySQL) Type() string {
func (ds *MySQL) Type() string {
return TypeMySQL
}

Expand Down Expand Up @@ -122,7 +122,7 @@ func (ds *MySQL) Connect(ctx context.Context) error {
return nil
}

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

func (ds MySQL) Ping(ctx context.Context) error {
func (ds *MySQL) Ping(ctx context.Context) error {
err := ds.Writer.PingContext(ctx)
if err != nil {
return errors.Wrap(err, "Error while attempting to ping mysql database")
Expand Down
42 changes: 16 additions & 26 deletions pkg/database/postgres.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ import (
_ "github.com/golang-migrate/migrate/v4/database/postgres"
_ "github.com/golang-migrate/migrate/v4/source/file"
_ "github.com/golang-migrate/migrate/v4/source/github"
"github.com/lib/pq"
"github.com/warrant-dev/warrant/pkg/config"
)

Expand All @@ -44,34 +43,20 @@ func NewPostgres(config config.PostgresConfig) *Postgres {
}
}

func (ds Postgres) Type() string {
func (ds *Postgres) Type() string {
return TypePostgres
}

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

// open new database connection without specifying the database name
usernamePassword := url.UserPassword(ds.Config.Username, ds.Config.Password).String()
db, err = sqlx.Open("postgres", fmt.Sprintf("postgres://%s@%s/?sslmode=%s", usernamePassword, ds.Config.Hostname, ds.Config.SSLMode))
if err != nil {
return errors.Wrap(err, fmt.Sprintf("Unable to establish connection to postgres database %s. Shutting down server.", ds.Config.Database))
}

// create database if it does not already exist
_, err = db.ExecContext(ctx, fmt.Sprintf("CREATE DATABASE %s", ds.Config.Database))
if err != nil {
pgErr, ok := err.(*pq.Error)
if ok && pgErr.Code.Name() != "duplicate_database" {
return errors.Wrap(err, fmt.Sprintf("Unable to create postgres database %s", ds.Config.Database))
}
if ds.Config.DSN != "" {
db, err = sqlx.Open("postgres", ds.Config.DSN)
} else {
usernamePassword := url.UserPassword(ds.Config.Username, ds.Config.Password).String()
db, err = sqlx.Open("postgres", fmt.Sprintf("postgres://%s@%s/%s?sslmode=%s", usernamePassword, ds.Config.Hostname, ds.Config.Database, ds.Config.SSLMode))
}

db.Close()

// open new database connection, this time specifying the database name
db, err = sqlx.Open("postgres", fmt.Sprintf("postgres://%s@%s/%s?sslmode=%s", usernamePassword, ds.Config.Hostname, ds.Config.Database, ds.Config.SSLMode))
if err != nil {
return errors.Wrap(err, fmt.Sprintf("Unable to establish connection to postgres database %s. Shutting down server.", ds.Config.Database))
}
Expand Down Expand Up @@ -101,8 +86,14 @@ func (ds *Postgres) Connect(ctx context.Context) error {
ds.Config.Database, ds.Config.MaxIdleConnections, ds.Config.ConnMaxIdleTime, ds.Config.MaxOpenConnections, ds.Config.ConnMaxLifetime)

// connect to reader if provided
if ds.Config.ReaderHostname != "" {
reader, err := sqlx.Open("postgres", fmt.Sprintf("postgres://%s@%s/%s?sslmode=%s", usernamePassword, ds.Config.ReaderHostname, ds.Config.Database, ds.Config.SSLMode))
if ds.Config.ReaderHostname != "" || ds.Config.ReaderDSN != "" {
var reader *sqlx.DB
if ds.Config.ReaderDSN != "" {
reader, err = sqlx.Open("postgres", ds.Config.ReaderDSN)
} else {
usernamePassword := url.UserPassword(ds.Config.Username, ds.Config.Password).String()
reader, err = sqlx.Open("postgres", fmt.Sprintf("postgres://%s@%s/%s?sslmode=%s", usernamePassword, ds.Config.ReaderHostname, ds.Config.Database, ds.Config.SSLMode))
}
if err != nil {
return errors.Wrap(err, fmt.Sprintf("Unable to establish connection to postgres reader %s. Shutting down server.", ds.Config.Database))
}
Expand All @@ -126,7 +117,6 @@ func (ds *Postgres) Connect(ctx context.Context) error {

// map struct attributes to db column names
reader.Mapper = reflectx.NewMapperFunc("postgres", func(s string) string { return s })

ds.Reader = reader
log.Info().Msgf("init: connected to postgres reader database %s [maxIdleConns: %d, connMaxIdleTime: %s, maxOpenConns: %d, connMaxLifetime: %s]",
ds.Config.Database, ds.Config.ReaderMaxIdleConnections, ds.Config.ConnMaxIdleTime, ds.Config.ReaderMaxOpenConnections, ds.Config.ConnMaxLifetime)
Expand All @@ -135,7 +125,7 @@ func (ds *Postgres) Connect(ctx context.Context) error {
return nil
}

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

func (ds Postgres) Ping(ctx context.Context) error {
func (ds *Postgres) Ping(ctx context.Context) error {
err := ds.Writer.PingContext(ctx)
if err != nil {
return errors.Wrap(err, "Error while attempting to ping postgres database")
Expand Down
6 changes: 3 additions & 3 deletions pkg/database/sqlite.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ func NewSQLite(config config.SQLiteConfig) *SQLite {
}
}

func (ds SQLite) Type() string {
func (ds *SQLite) Type() string {
return TypeSQLite
}

Expand Down Expand Up @@ -94,7 +94,7 @@ func (ds *SQLite) Connect(ctx context.Context) error {
return nil
}

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

func (ds SQLite) Ping(ctx context.Context) error {
func (ds *SQLite) Ping(ctx context.Context) error {
return ds.Writer.PingContext(ctx)
}

0 comments on commit a85d11b

Please sign in to comment.