mirror of
https://github.com/nkanaev/yarr.git
synced 2026-06-24 09:05:16 +00:00
35 lines
514 B
Go
35 lines
514 B
Go
package postgres
|
|
|
|
import (
|
|
"database/sql"
|
|
"log"
|
|
|
|
_ "github.com/lib/pq"
|
|
)
|
|
|
|
type PostgresStorage struct {
|
|
db *sql.DB
|
|
}
|
|
|
|
func New(connStr string) (*PostgresStorage, error) {
|
|
db, err := sql.Open("postgres", connStr)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
if err := db.Ping(); err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
if err := migrate(db); err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
log.Print("connected to postgres")
|
|
return &PostgresStorage{db: db}, nil
|
|
}
|
|
|
|
func (s *PostgresStorage) Close() error {
|
|
return s.db.Close()
|
|
}
|