31 lines
624 B
Go
31 lines
624 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, sslMode string, schema string) (*sqlx.DB, error) {
|
|
psqlInfo := fmt.Sprintf("host=%s port=%s user=%s password=%s dbname=%s sslmode=%s search_path=%s",
|
|
host, port, user, password, dbname, sslMode, schema)
|
|
|
|
db, err := sqlx.Open("postgres", psqlInfo)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
err = db.Ping()
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return db, nil
|
|
}
|