Skip to content

Commit

Permalink
Multi-database support (#869)
Browse files Browse the repository at this point in the history
* Multi-database support

Initial version of multi-database support
* Github workflow: create multiple test databases
* `adapters/postgres/postgres_test.go`: Use dedicated `TestCase` table for
tests, to avoid `controllers/tables_test.go` to fail on unexpected data
(secondary database will only get some INSERTs those come from
context-aware tests, other data will end up in default database)
* `adapters/postgres/internal/connection/conn.go`: Removed unused varaible
* Updated comments explaining the logic around adding database
  connections to the pool
* Recently changed files re-formatted with `go fmt`
* + Added comments around tests for multi-database support
  • Loading branch information
hostcc committed Mar 11, 2024
1 parent ad0dde4 commit 366719f
Show file tree
Hide file tree
Showing 8 changed files with 250 additions and 198 deletions.
10 changes: 6 additions & 4 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -54,13 +54,15 @@ jobs:
apt-get update
apt-get install -y postgresql-client
- name: Setup Database
- name: Setup Databases
env:
PGPASSWORD: postgres
run: |
psql -h $PREST_PG_HOST -p $PREST_PG_PORT -U $PREST_PG_USER -c "DROP DATABASE IF EXISTS \"$PREST_PG_DATABASE\";"
psql -h $PREST_PG_HOST -p $PREST_PG_PORT -U $PREST_PG_USER -c "CREATE DATABASE \"$PREST_PG_DATABASE\";"
psql -h $PREST_PG_HOST -p $PREST_PG_PORT -U $PREST_PG_USER -d $PREST_PG_DATABASE -f ./testdata/schema.sql
for db in $PREST_PG_DATABASE secondary-db; do
psql -h $PREST_PG_HOST -p $PREST_PG_PORT -U $PREST_PG_USER -c "DROP DATABASE IF EXISTS \"$db\";"
psql -h $PREST_PG_HOST -p $PREST_PG_PORT -U $PREST_PG_USER -c "CREATE DATABASE \"$db\";"
psql -h $PREST_PG_HOST -p $PREST_PG_PORT -U $PREST_PG_USER -d $db -f ./testdata/schema.sql
done
- name: Set up Go
uses: actions/setup-go@v4
Expand Down
4 changes: 2 additions & 2 deletions adapters/postgres/connection.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ func GetPool() *connection.Pool {
}

// AddDatabaseToPool add connection to pool
func AddDatabaseToPool(name string, DB *sqlx.DB) {
connection.AddDatabaseToPool(name, DB)
func AddDatabaseToPool(name string) (*sqlx.DB, error) {
return connection.AddDatabaseToPool(name)
}

// MustGet get postgres connection
Expand Down
32 changes: 16 additions & 16 deletions adapters/postgres/internal/connection/conn.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ import (
)

var (
err error
pool *Pool
currDatabase string
)
Expand Down Expand Up @@ -55,25 +54,18 @@ func GetURI(DBName string) string {
return dbURI
}

// Get get postgres connection
// Get get Postgres connection adding it to the pool if needed
func Get() (*sqlx.DB, error) {
var DB *sqlx.DB

DB = getDatabaseFromPool(GetDatabase())
DB := getDatabaseFromPool(GetDatabase())
// Connection is already in the pool
if DB != nil {
return DB, nil
}

DB, err = sqlx.Connect("postgres", GetURI(GetDatabase()))
if err != nil {
return nil, err
}
DB.SetMaxIdleConns(config.PrestConf.PGMaxIdleConn)
DB.SetMaxOpenConns(config.PrestConf.PGMaxOpenConn)

AddDatabaseToPool(GetDatabase(), DB)
// Connection is not in the pool, add it
DB, err := AddDatabaseToPool(GetDatabase())

return DB, nil
return DB, err
}

// GetFromPool tries to get the db name from the db pool
Expand Down Expand Up @@ -108,13 +100,21 @@ func getDatabaseFromPool(name string) *sqlx.DB {
return DB
}

// AddDatabaseToPool add connection to pool
func AddDatabaseToPool(name string, DB *sqlx.DB) {
// AddDatabaseToPool create and add connection to the pool
func AddDatabaseToPool(name string) (*sqlx.DB, error) {
DB, err := sqlx.Connect("postgres", GetURI(name))
if err != nil {
return nil, err
}
DB.SetMaxIdleConns(config.PrestConf.PGMaxIdleConn)
DB.SetMaxOpenConns(config.PrestConf.PGMaxOpenConn)

p := GetPool()

p.Mtx.Lock()
p.DB[GetURI(name)] = DB
p.Mtx.Unlock()
return DB, nil
}

// MustGet get postgres connection
Expand Down
11 changes: 8 additions & 3 deletions adapters/postgres/postgres.go
Original file line number Diff line number Diff line change
Expand Up @@ -1690,12 +1690,17 @@ func (adapter *Postgres) GetDatabase() string {
return connection.GetDatabase()
}

// getDBFromCtx tries to get the db from context if not present it will
// fallback to the current setted db
// getDBFromCtx tries to get the DB from context adding it to the pool if not
// present, unless DB name is unset in the context - it will then fallback to
// the current DB has been set via `SetDatabase(...)`
func getDBFromCtx(ctx context.Context) (db *sqlx.DB, err error) {
dbName, ok := ctx.Value(pctx.DBNameKey).(string)
if ok {
return connection.GetFromPool(dbName)
DB, err := connection.GetFromPool(dbName)
if err == nil {
return DB, err
}
return connection.AddDatabaseToPool(dbName)
}
return connection.Get()
}
Loading

0 comments on commit 366719f

Please sign in to comment.