This commit is contained in:
Jan Bader 2021-12-02 10:44:14 +00:00
parent cdc767a497
commit e465b961a5
5 changed files with 25 additions and 11 deletions

View File

@ -1,10 +1,12 @@
package postgres
import (
"context"
"database/sql"
"embed"
"fmt"
"github.com/jackc/pgx/v4"
_ "github.com/jackc/pgx/v4/stdlib"
"github.com/pressly/goose/v3"
)
@ -25,5 +27,15 @@ func Connect(server string, user string, password string, database string) (*Que
return nil, err
}
return New(conn), nil
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
}

View File

@ -4,14 +4,15 @@ package postgres
import (
"context"
"database/sql"
"github.com/jackc/pgconn"
"github.com/jackc/pgx/v4"
)
type DBTX interface {
ExecContext(context.Context, string, ...interface{}) (sql.Result, error)
PrepareContext(context.Context, string) (*sql.Stmt, error)
QueryContext(context.Context, string, ...interface{}) (*sql.Rows, error)
QueryRowContext(context.Context, string, ...interface{}) *sql.Row
Exec(context.Context, string, ...interface{}) (pgconn.CommandTag, error)
Query(context.Context, string, ...interface{}) (pgx.Rows, error)
QueryRow(context.Context, string, ...interface{}) pgx.Row
}
func New(db DBTX) *Queries {
@ -22,7 +23,7 @@ type Queries struct {
db DBTX
}
func (q *Queries) WithTx(tx *sql.Tx) *Queries {
func (q *Queries) WithTx(tx pgx.Tx) *Queries {
return &Queries{
db: tx,
}

View File

@ -7,6 +7,7 @@ import (
"time"
"github.com/google/uuid"
"github.com/jackc/pgtype"
)
type Account struct {
@ -30,8 +31,8 @@ type Payee struct {
type Transaction struct {
ID uuid.UUID
Date time.Time
Memo sql.NullString
Amount string
Memo string
Amount pgtype.Numeric
AccountID uuid.UUID
PayeeID uuid.NullUUID
}

View File

@ -37,7 +37,7 @@ CREATE TABLE payees (
CREATE TABLE transactions (
id uuid DEFAULT uuid_generate_v4() PRIMARY KEY,
date date NOT NULL,
memo text NULL,
memo text NOT NULL,
amount decimal(12,2) NOT NULL,
account_id uuid NOT NULL,
payee_id uuid

View File

@ -5,4 +5,4 @@ packages:
engine: "postgresql"
schema: "postgres/schema/"
queries: "postgres/queries/"
#sql_package: "pgx/v4"
sql_package: "pgx/v4"