aboutsummaryrefslogtreecommitdiffstats
path: root/internal/db/db.go
blob: df5934cf73307e7a501a48b4d29a9a50915270dd (plain) (blame)
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
package db

import (
	"database/sql"
	"fmt"

	"github.com/eugenetriguba/bolt/internal/config"
	_ "github.com/lib/pq"
)

var db *sql.DB

// Connect to the database using the driver and connection
// parameter that are specified in the bolt configuration file.
//
// Note that subsequent calls to Connect return the same database
// handle.
func Connect() (*sql.DB, error) {
	if db == nil {
		config, err := config.NewConfig()
		if err != nil {
			return nil, err
		}

		connInfo := fmt.Sprintf(
			"host=%s port=%d user=%s password=%s dbname=%s sslmode=disable",
			config.Connection.Host, config.Connection.Port, config.Connection.User,
			config.Connection.Password, config.Connection.DBName,
		)
		newDb, err := sql.Open(config.Connection.Driver, connInfo)
		if err != nil {
			return nil, err
		}

		// Note: `sql.Open` only validates the connection string we provided
		// it is sane. It doesn't actually open up a connection to the database.
		// For that, we ping the database to ensure the connection string is fully
		// valid.
		err = newDb.Ping()
		if err != nil {
			return nil, err
		}

		db = newDb
	}

	return db, nil
}