Skip to content

build: Run sqlc-pg-gen via GitHub Actions #1944

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 9 commits into from
Nov 10, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 38 additions & 0 deletions .github/workflows/gen.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
name: sqlc-pg-gen
on:
workflow_dispatch:
jobs:
gen:
name: sqlc-pg-gen
runs-on: ubuntu-22.04
services:
postgres:
image: postgres:15.0-alpine
env:
POSTGRES_USER: postgres
POSTGRES_PASSWORD: postgres
POSTGRES_DB: postgres
ports:
- 5432:5432
# needed because the postgres container does not provide a healthcheck
options: --health-cmd pg_isready --health-interval 10s --health-timeout 5s --health-retries 5
steps:
- uses: actions/checkout@v3
- uses: actions/setup-go@v3
with:
go-version: '1.19'
- run: go build -o sqlc-pg-gen ./internal/tools/sqlc-pg-gen
- run: mkdir -p gen/contrib
- run: ./sqlc-pg-gen gen
env:
PG_USER: postgres
PG_HOST: localhost
PG_DATABASE: postgres
PG_PASSWORD: postgres
PG_PORT: ${{ job.services.postgres.ports['5432'] }}
- name: Save results
uses: actions/upload-artifact@v3
with:
name: sqlc-pg-gen-results
path: gen

77 changes: 56 additions & 21 deletions internal/tools/sqlc-pg-gen/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package main
import (
"bytes"
"context"
"flag"
"fmt"
"go/format"
"log"
Expand Down Expand Up @@ -209,12 +210,47 @@ func preserveLegacyCatalogBehavior(allProcs []Proc) []Proc {
return procs
}

func databaseURL() string {
dburl := os.Getenv("DATABASE_URL")
if dburl != "" {
return dburl
}
pgUser := os.Getenv("PG_USER")
pgHost := os.Getenv("PG_HOST")
pgPort := os.Getenv("PG_PORT")
pgPass := os.Getenv("PG_PASSWORD")
pgDB := os.Getenv("PG_DATABASE")
if pgUser == "" {
pgUser = "postgres"
}
if pgPass == "" {
pgPass = "mysecretpassword"
}
if pgPort == "" {
pgPort = "5432"
}
if pgHost == "" {
pgHost = "127.0.0.1"
}
if pgDB == "" {
pgDB = "dinotest"
}
return fmt.Sprintf("postgres://%s:%s@%s:%s/%s?sslmode=disable", pgUser, pgPass, pgHost, pgPort, pgDB)
}

func run(ctx context.Context) error {
flag.Parse()

dir := flag.Arg(0)
if dir == "" {
dir = filepath.Join("internal", "engine", "postgresql")
}

tmpl, err := template.New("").Parse(catalogTmpl)
if err != nil {
return err
}
conn, err := pgx.Connect(ctx, os.Getenv("DATABASE_URL"))
conn, err := pgx.Connect(ctx, databaseURL())
if err != nil {
return err
}
Expand All @@ -224,12 +260,12 @@ func run(ctx context.Context) error {
{
Name: "pg_catalog",
GenFnName: "genPGCatalog",
DestPath: filepath.Join("internal", "engine", "postgresql", "pg_catalog.go"),
DestPath: filepath.Join(dir, "pg_catalog.go"),
},
{
Name: "information_schema",
GenFnName: "genInformationSchema",
DestPath: filepath.Join("internal", "engine", "postgresql", "information_schema.go"),
DestPath: filepath.Join(dir, "information_schema.go"),
},
}

Expand Down Expand Up @@ -272,8 +308,7 @@ func run(ctx context.Context) error {

_, err := conn.Exec(ctx, fmt.Sprintf("CREATE EXTENSION IF NOT EXISTS \"%s\"", extension))
if err != nil {
log.Printf("error creating %s: %s", extension, err)
continue
return fmt.Errorf("error creating %s: %s", extension, err)
}

rows, err := conn.Query(ctx, extensionFuncs, extension)
Expand Down Expand Up @@ -303,7 +338,7 @@ func run(ctx context.Context) error {
return false
})

extensionPath := filepath.Join("internal", "engine", "postgresql", "contrib", name+".go")
extensionPath := filepath.Join(dir, "contrib", name+".go")
err = writeFormattedGo(tmpl, tmplCtx{
Pkg: "contrib",
SchemaName: "pg_catalog",
Expand All @@ -322,7 +357,7 @@ func run(ctx context.Context) error {
return err
}

extensionLoaderPath := filepath.Join("internal", "engine", "postgresql", "extension.go")
extensionLoaderPath := filepath.Join(dir, "extension.go")
err = writeFormattedGo(extensionTmpl, loaded, extensionLoaderPath)
if err != nil {
return err
Expand All @@ -349,16 +384,16 @@ type extensionPair struct {
var extensions = []string{
"adminpack",
"amcheck",
"auth_delay",
"auto_explain",
"bloom",
// "auth_delay",
// "auto_explain",
// "bloom",
"btree_gin",
"btree_gist",
"citext",
"cube",
"dblink",
"dict_int",
"dict_xsyn",
// "dict_int",
// "dict_xsyn",
"earthdistance",
"file_fdw",
"fuzzystrmatch",
Expand All @@ -369,26 +404,26 @@ var extensions = []string{
"lo",
"ltree",
"pageinspect",
"passwordcheck",
// "passwordcheck",
"pg_buffercache",
"pgcrypto",
"pg_freespacemap",
"pg_prewarm",
"pgrowlocks",
"pg_stat_statements",
"pgstattuple",
"pg_trgm",
"pg_visibility",
"pgcrypto",
"pgrowlocks",
"pgstattuple",
"postgres_fdw",
"seg",
"sepgsql",
"spi",
// "sepgsql",
// "spi",
"sslinfo",
"tablefunc",
"tcn",
"test_decoding",
"tsm_system_rows",
"tsm_system_time",
// "test_decoding",
// "tsm_system_rows",
// "tsm_system_time",
"unaccent",
"uuid-ossp",
"xml2",
Expand Down