31 lines
566 B
Go
31 lines
566 B
Go
|
package db
|
||
|
|
||
|
import (
|
||
|
"embed"
|
||
|
"fmt"
|
||
|
"github.com/jmoiron/sqlx"
|
||
|
)
|
||
|
|
||
|
//go:embed dev/*.sql
|
||
|
var DevMigrations embed.FS
|
||
|
|
||
|
//go:embed prod/*.sql
|
||
|
var ProdMigrations embed.FS
|
||
|
|
||
|
func ConnectToDbNamed(host string, port string, user string, password string, dbname string) (*sqlx.DB, error) {
|
||
|
psqlInfo := fmt.Sprintf("host=%s port=%s user=%s password=%s dbname=%s sslmode=disable",
|
||
|
host, port, user, password, dbname)
|
||
|
|
||
|
db, err := sqlx.Open("postgres", psqlInfo)
|
||
|
if err != nil {
|
||
|
return nil, err
|
||
|
}
|
||
|
|
||
|
err = db.Ping()
|
||
|
if err != nil {
|
||
|
return nil, err
|
||
|
}
|
||
|
|
||
|
return db, nil
|
||
|
}
|