51 lines
943 B
Go
51 lines
943 B
Go
package postgres
|
|
|
|
import (
|
|
"context"
|
|
"database/sql"
|
|
"embed"
|
|
"fmt"
|
|
|
|
"github.com/jackc/pgx/v4"
|
|
_ "github.com/jackc/pgx/v4/stdlib"
|
|
"github.com/pressly/goose/v3"
|
|
)
|
|
|
|
//go:embed schema/*.sql
|
|
var migrations embed.FS
|
|
|
|
// Connect to a database
|
|
func Connect(server string, user string, password string, database string) (*Queries, error) {
|
|
connString := fmt.Sprintf("postgres://%s:%s@%s/%s", user, password, server, database)
|
|
conn, err := sql.Open("pgx", connString)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
goose.SetBaseFS(migrations)
|
|
if err = goose.Up(conn, "schema"); err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
err = conn.Close()
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
connPG, err := pgx.Connect(context.Background(), connString)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return New(connPG), nil
|
|
}
|
|
|
|
func (tx Transaction) GetAmount() float64 {
|
|
var amount float64
|
|
err := tx.Amount.AssignTo(&amount)
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
return amount
|
|
}
|