Skip to content

Commit 2d28ea3

Browse files
authored
build: Run sqlc-pg-gen via GitHub Actions (#1944)
* build: Run sqlc-pg-gen via GitHub Actions * Only run workflow manually
1 parent 1f3aec6 commit 2d28ea3

File tree

2 files changed

+94
-21
lines changed

2 files changed

+94
-21
lines changed

.github/workflows/gen.yml

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
name: sqlc-pg-gen
2+
on:
3+
workflow_dispatch:
4+
jobs:
5+
gen:
6+
name: sqlc-pg-gen
7+
runs-on: ubuntu-22.04
8+
services:
9+
postgres:
10+
image: postgres:15.0-alpine
11+
env:
12+
POSTGRES_USER: postgres
13+
POSTGRES_PASSWORD: postgres
14+
POSTGRES_DB: postgres
15+
ports:
16+
- 5432:5432
17+
# needed because the postgres container does not provide a healthcheck
18+
options: --health-cmd pg_isready --health-interval 10s --health-timeout 5s --health-retries 5
19+
steps:
20+
- uses: actions/checkout@v3
21+
- uses: actions/setup-go@v3
22+
with:
23+
go-version: '1.19'
24+
- run: go build -o sqlc-pg-gen ./internal/tools/sqlc-pg-gen
25+
- run: mkdir -p gen/contrib
26+
- run: ./sqlc-pg-gen gen
27+
env:
28+
PG_USER: postgres
29+
PG_HOST: localhost
30+
PG_DATABASE: postgres
31+
PG_PASSWORD: postgres
32+
PG_PORT: ${{ job.services.postgres.ports['5432'] }}
33+
- name: Save results
34+
uses: actions/upload-artifact@v3
35+
with:
36+
name: sqlc-pg-gen-results
37+
path: gen
38+

internal/tools/sqlc-pg-gen/main.go

Lines changed: 56 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package main
33
import (
44
"bytes"
55
"context"
6+
"flag"
67
"fmt"
78
"go/format"
89
"log"
@@ -209,12 +210,47 @@ func preserveLegacyCatalogBehavior(allProcs []Proc) []Proc {
209210
return procs
210211
}
211212

213+
func databaseURL() string {
214+
dburl := os.Getenv("DATABASE_URL")
215+
if dburl != "" {
216+
return dburl
217+
}
218+
pgUser := os.Getenv("PG_USER")
219+
pgHost := os.Getenv("PG_HOST")
220+
pgPort := os.Getenv("PG_PORT")
221+
pgPass := os.Getenv("PG_PASSWORD")
222+
pgDB := os.Getenv("PG_DATABASE")
223+
if pgUser == "" {
224+
pgUser = "postgres"
225+
}
226+
if pgPass == "" {
227+
pgPass = "mysecretpassword"
228+
}
229+
if pgPort == "" {
230+
pgPort = "5432"
231+
}
232+
if pgHost == "" {
233+
pgHost = "127.0.0.1"
234+
}
235+
if pgDB == "" {
236+
pgDB = "dinotest"
237+
}
238+
return fmt.Sprintf("postgres://%s:%s@%s:%s/%s?sslmode=disable", pgUser, pgPass, pgHost, pgPort, pgDB)
239+
}
240+
212241
func run(ctx context.Context) error {
242+
flag.Parse()
243+
244+
dir := flag.Arg(0)
245+
if dir == "" {
246+
dir = filepath.Join("internal", "engine", "postgresql")
247+
}
248+
213249
tmpl, err := template.New("").Parse(catalogTmpl)
214250
if err != nil {
215251
return err
216252
}
217-
conn, err := pgx.Connect(ctx, os.Getenv("DATABASE_URL"))
253+
conn, err := pgx.Connect(ctx, databaseURL())
218254
if err != nil {
219255
return err
220256
}
@@ -224,12 +260,12 @@ func run(ctx context.Context) error {
224260
{
225261
Name: "pg_catalog",
226262
GenFnName: "genPGCatalog",
227-
DestPath: filepath.Join("internal", "engine", "postgresql", "pg_catalog.go"),
263+
DestPath: filepath.Join(dir, "pg_catalog.go"),
228264
},
229265
{
230266
Name: "information_schema",
231267
GenFnName: "genInformationSchema",
232-
DestPath: filepath.Join("internal", "engine", "postgresql", "information_schema.go"),
268+
DestPath: filepath.Join(dir, "information_schema.go"),
233269
},
234270
}
235271

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

273309
_, err := conn.Exec(ctx, fmt.Sprintf("CREATE EXTENSION IF NOT EXISTS \"%s\"", extension))
274310
if err != nil {
275-
log.Printf("error creating %s: %s", extension, err)
276-
continue
311+
return fmt.Errorf("error creating %s: %s", extension, err)
277312
}
278313

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

306-
extensionPath := filepath.Join("internal", "engine", "postgresql", "contrib", name+".go")
341+
extensionPath := filepath.Join(dir, "contrib", name+".go")
307342
err = writeFormattedGo(tmpl, tmplCtx{
308343
Pkg: "contrib",
309344
SchemaName: "pg_catalog",
@@ -322,7 +357,7 @@ func run(ctx context.Context) error {
322357
return err
323358
}
324359

325-
extensionLoaderPath := filepath.Join("internal", "engine", "postgresql", "extension.go")
360+
extensionLoaderPath := filepath.Join(dir, "extension.go")
326361
err = writeFormattedGo(extensionTmpl, loaded, extensionLoaderPath)
327362
if err != nil {
328363
return err
@@ -349,16 +384,16 @@ type extensionPair struct {
349384
var extensions = []string{
350385
"adminpack",
351386
"amcheck",
352-
"auth_delay",
353-
"auto_explain",
354-
"bloom",
387+
// "auth_delay",
388+
// "auto_explain",
389+
// "bloom",
355390
"btree_gin",
356391
"btree_gist",
357392
"citext",
358393
"cube",
359394
"dblink",
360-
"dict_int",
361-
"dict_xsyn",
395+
// "dict_int",
396+
// "dict_xsyn",
362397
"earthdistance",
363398
"file_fdw",
364399
"fuzzystrmatch",
@@ -369,26 +404,26 @@ var extensions = []string{
369404
"lo",
370405
"ltree",
371406
"pageinspect",
372-
"passwordcheck",
407+
// "passwordcheck",
373408
"pg_buffercache",
374-
"pgcrypto",
375409
"pg_freespacemap",
376410
"pg_prewarm",
377-
"pgrowlocks",
378411
"pg_stat_statements",
379-
"pgstattuple",
380412
"pg_trgm",
381413
"pg_visibility",
414+
"pgcrypto",
415+
"pgrowlocks",
416+
"pgstattuple",
382417
"postgres_fdw",
383418
"seg",
384-
"sepgsql",
385-
"spi",
419+
// "sepgsql",
420+
// "spi",
386421
"sslinfo",
387422
"tablefunc",
388423
"tcn",
389-
"test_decoding",
390-
"tsm_system_rows",
391-
"tsm_system_time",
424+
// "test_decoding",
425+
// "tsm_system_rows",
426+
// "tsm_system_time",
392427
"unaccent",
393428
"uuid-ossp",
394429
"xml2",

0 commit comments

Comments
 (0)