Use goose for migrations

This commit is contained in:
2021-11-08 22:24:21 +00:00
parent cf1bc70103
commit 5de7d32c30
25 changed files with 134 additions and 145 deletions

View File

@ -1,17 +1,29 @@
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) {
conn, err := pgx.Connect(context.Background(), fmt.Sprintf("postgresql://%s:%s@%s/%s", user, password, server, database))
connString := fmt.Sprintf("pgx://%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
}
return New(conn), nil
}