31 lines
385 B
Go
31 lines
385 B
Go
|
package db
|
||
|
|
||
|
import (
|
||
|
"database/sql"
|
||
|
|
||
|
_ "github.com/lib/pq"
|
||
|
)
|
||
|
|
||
|
type DB struct {
|
||
|
*sql.DB
|
||
|
}
|
||
|
|
||
|
func New(dbUrl string) (*DB, error) {
|
||
|
// open connection
|
||
|
db_, err := sql.Open("postgres", dbUrl)
|
||
|
if err != nil {
|
||
|
return nil, err
|
||
|
}
|
||
|
|
||
|
// test connection
|
||
|
_, err = db_.Exec("SELECT 1")
|
||
|
if err != nil {
|
||
|
return nil, err
|
||
|
}
|
||
|
|
||
|
// TODO: run migrations
|
||
|
db := &DB{DB: db_}
|
||
|
|
||
|
return db, nil
|
||
|
}
|