2018-08-26 13:15:48 +02:00
|
|
|
//
|
|
|
|
// Copyright (c) 2018
|
|
|
|
// Mainflux
|
|
|
|
//
|
|
|
|
// SPDX-License-Identifier: Apache-2.0
|
|
|
|
//
|
|
|
|
|
2018-05-10 23:53:25 +02:00
|
|
|
package postgres
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
|
2019-04-16 14:58:56 +02:00
|
|
|
"github.com/jmoiron/sqlx"
|
|
|
|
|
2018-05-11 01:00:10 +02:00
|
|
|
_ "github.com/lib/pq" // required for SQL access
|
|
|
|
migrate "github.com/rubenv/sql-migrate"
|
2018-05-10 23:53:25 +02:00
|
|
|
)
|
|
|
|
|
2018-12-15 17:28:22 -06:00
|
|
|
// Config defines the options that are used when connecting to a PostgreSQL instance
|
|
|
|
type Config struct {
|
|
|
|
Host string
|
|
|
|
Port string
|
|
|
|
User string
|
|
|
|
Pass string
|
|
|
|
Name string
|
|
|
|
SSLMode string
|
|
|
|
SSLCert string
|
|
|
|
SSLKey string
|
|
|
|
SSLRootCert string
|
|
|
|
}
|
|
|
|
|
2018-05-11 01:00:10 +02:00
|
|
|
// Connect creates a connection to the PostgreSQL instance and applies any
|
|
|
|
// unapplied database migrations. A non-nil error is returned to indicate
|
|
|
|
// failure.
|
2019-04-16 14:58:56 +02:00
|
|
|
func Connect(cfg Config) (*sqlx.DB, error) {
|
2018-12-15 17:28:22 -06:00
|
|
|
url := fmt.Sprintf("host=%s port=%s user=%s dbname=%s password=%s sslmode=%s sslcert=%s sslkey=%s sslrootcert=%s", cfg.Host, cfg.Port, cfg.User, cfg.Name, cfg.Pass, cfg.SSLMode, cfg.SSLCert, cfg.SSLKey, cfg.SSLRootCert)
|
2018-05-10 23:53:25 +02:00
|
|
|
|
2019-04-16 14:58:56 +02:00
|
|
|
db, err := sqlx.Open("postgres", url)
|
2018-05-10 23:53:25 +02:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2018-05-11 01:00:10 +02:00
|
|
|
if err := migrateDB(db); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return db, nil
|
|
|
|
}
|
|
|
|
|
2019-04-16 14:58:56 +02:00
|
|
|
func migrateDB(db *sqlx.DB) error {
|
2018-05-11 01:00:10 +02:00
|
|
|
migrations := &migrate.MemoryMigrationSource{
|
|
|
|
Migrations: []*migrate.Migration{
|
2018-05-21 12:51:46 +02:00
|
|
|
{
|
2018-05-11 01:00:10 +02:00
|
|
|
Id: "users_1",
|
|
|
|
Up: []string{
|
2018-08-27 12:17:41 +02:00
|
|
|
`CREATE TABLE IF NOT EXISTS users (
|
2018-05-11 01:00:10 +02:00
|
|
|
email VARCHAR(254) PRIMARY KEY,
|
|
|
|
password CHAR(60) NOT NULL
|
|
|
|
)`,
|
|
|
|
},
|
|
|
|
Down: []string{"DROP TABLE users"},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
2018-05-10 23:53:25 +02:00
|
|
|
|
2019-04-16 14:58:56 +02:00
|
|
|
_, err := migrate.Exec(db.DB, "postgres", migrations, migrate.Up)
|
2018-05-11 01:00:10 +02:00
|
|
|
return err
|
2018-05-10 23:53:25 +02:00
|
|
|
}
|