From acc8a2a4bc23d435700acbcf75df8d3f69f9c23d Mon Sep 17 00:00:00 2001 From: Kyle Conroy Date: Wed, 9 Nov 2022 22:28:03 -0800 Subject: [PATCH 1/9] build: Run sqlc-pg-gen via GitHub Actions --- .github/workflows/gen.yml | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) create mode 100644 .github/workflows/gen.yml diff --git a/.github/workflows/gen.yml b/.github/workflows/gen.yml new file mode 100644 index 0000000000..61cbcfb717 --- /dev/null +++ b/.github/workflows/gen.yml @@ -0,0 +1,24 @@ +name: sqlc-pg-gen +on: workflow_dispatch +jobs: + gen: + name: sqlc-pg-gen + runs-on: ubuntu-22.04 + services: + postgres: + image: postgres:15.0 + 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: ./sqlc-pg-gen From 739f429d25c652ae14ccce12ff03057a3500755d Mon Sep 17 00:00:00 2001 From: Kyle Conroy Date: Wed, 9 Nov 2022 22:30:21 -0800 Subject: [PATCH 2/9] Run on PRs --- .github/workflows/gen.yml | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/.github/workflows/gen.yml b/.github/workflows/gen.yml index 61cbcfb717..545ee4758b 100644 --- a/.github/workflows/gen.yml +++ b/.github/workflows/gen.yml @@ -1,5 +1,7 @@ name: sqlc-pg-gen -on: workflow_dispatch +on: + workflow_dispatch: + pull_request: jobs: gen: name: sqlc-pg-gen From e46bb0da8bea22eeb0f0fbd55fa7bcfe525e1cf8 Mon Sep 17 00:00:00 2001 From: Kyle Conroy Date: Wed, 9 Nov 2022 22:38:12 -0800 Subject: [PATCH 3/9] Use different environment variables --- .github/workflows/gen.yml | 7 +++++++ internal/tools/sqlc-pg-gen/main.go | 30 +++++++++++++++++++++++++++++- 2 files changed, 36 insertions(+), 1 deletion(-) diff --git a/.github/workflows/gen.yml b/.github/workflows/gen.yml index 545ee4758b..ba510ff22a 100644 --- a/.github/workflows/gen.yml +++ b/.github/workflows/gen.yml @@ -24,3 +24,10 @@ jobs: go-version: '1.19' - run: go build -o sqlc-pg-gen ./internal/tools/sqlc-pg-gen - run: ./sqlc-pg-gen + env: + PG_USER: postgres + PG_HOST: localhost + PG_DATABASE: postgres + PG_PASSWORD: postgres + PG_PORT: ${{ job.services.postgres.ports['5432'] }} + diff --git a/internal/tools/sqlc-pg-gen/main.go b/internal/tools/sqlc-pg-gen/main.go index f0c6de6cd3..e6d6d73ba4 100644 --- a/internal/tools/sqlc-pg-gen/main.go +++ b/internal/tools/sqlc-pg-gen/main.go @@ -209,12 +209,40 @@ 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 { 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 } From b0eea0bf75717d67d8ed5fd4a1d129c0a331e75e Mon Sep 17 00:00:00 2001 From: Kyle Conroy Date: Wed, 9 Nov 2022 22:42:17 -0800 Subject: [PATCH 4/9] Switch to Alpine --- .github/workflows/gen.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/gen.yml b/.github/workflows/gen.yml index ba510ff22a..bfd95adb32 100644 --- a/.github/workflows/gen.yml +++ b/.github/workflows/gen.yml @@ -8,7 +8,7 @@ jobs: runs-on: ubuntu-22.04 services: postgres: - image: postgres:15.0 + image: postgres:15.0-alpine env: POSTGRES_USER: postgres POSTGRES_PASSWORD: postgres From da9517c3fc278f8d537c327ab94cef2c93b37729 Mon Sep 17 00:00:00 2001 From: Kyle Conroy Date: Wed, 9 Nov 2022 22:52:28 -0800 Subject: [PATCH 5/9] Include fewer extensions --- internal/tools/sqlc-pg-gen/main.go | 31 +++++++++++++++--------------- 1 file changed, 15 insertions(+), 16 deletions(-) diff --git a/internal/tools/sqlc-pg-gen/main.go b/internal/tools/sqlc-pg-gen/main.go index e6d6d73ba4..7e25b14dc2 100644 --- a/internal/tools/sqlc-pg-gen/main.go +++ b/internal/tools/sqlc-pg-gen/main.go @@ -300,8 +300,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) @@ -377,16 +376,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", @@ -397,26 +396,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", From 23c1070623741d91dcd98e3e956afc7ee7edab56 Mon Sep 17 00:00:00 2001 From: Kyle Conroy Date: Wed, 9 Nov 2022 22:57:40 -0800 Subject: [PATCH 6/9] Output files in a specific directory --- .github/workflows/gen.yml | 3 ++- internal/tools/sqlc-pg-gen/main.go | 16 ++++++++++++---- 2 files changed, 14 insertions(+), 5 deletions(-) diff --git a/.github/workflows/gen.yml b/.github/workflows/gen.yml index bfd95adb32..d5a4b96bbd 100644 --- a/.github/workflows/gen.yml +++ b/.github/workflows/gen.yml @@ -23,7 +23,8 @@ jobs: with: go-version: '1.19' - run: go build -o sqlc-pg-gen ./internal/tools/sqlc-pg-gen - - run: ./sqlc-pg-gen + - run: mkdir -p gen/contrib + - run: ./sqlc-pg-gen gen env: PG_USER: postgres PG_HOST: localhost diff --git a/internal/tools/sqlc-pg-gen/main.go b/internal/tools/sqlc-pg-gen/main.go index 7e25b14dc2..631a55aaf8 100644 --- a/internal/tools/sqlc-pg-gen/main.go +++ b/internal/tools/sqlc-pg-gen/main.go @@ -3,6 +3,7 @@ package main import ( "bytes" "context" + "flag" "fmt" "go/format" "log" @@ -238,6 +239,13 @@ func databaseURL() string { } 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 @@ -252,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"), }, } @@ -330,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", @@ -349,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 From 5bee3ad423a404f3a963fcdce99584b5776cf669 Mon Sep 17 00:00:00 2001 From: Kyle Conroy Date: Wed, 9 Nov 2022 22:57:55 -0800 Subject: [PATCH 7/9] Find to check --- .github/workflows/gen.yml | 1 + 1 file changed, 1 insertion(+) diff --git a/.github/workflows/gen.yml b/.github/workflows/gen.yml index d5a4b96bbd..23a1be9a4f 100644 --- a/.github/workflows/gen.yml +++ b/.github/workflows/gen.yml @@ -31,4 +31,5 @@ jobs: PG_DATABASE: postgres PG_PASSWORD: postgres PG_PORT: ${{ job.services.postgres.ports['5432'] }} + - run: find ./gen From 84b2a4499c62a487f76e03f312483bf6b1a3581e Mon Sep 17 00:00:00 2001 From: Kyle Conroy Date: Wed, 9 Nov 2022 22:59:55 -0800 Subject: [PATCH 8/9] Upload output --- .github/workflows/gen.yml | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/.github/workflows/gen.yml b/.github/workflows/gen.yml index 23a1be9a4f..b1146aa028 100644 --- a/.github/workflows/gen.yml +++ b/.github/workflows/gen.yml @@ -31,5 +31,9 @@ jobs: PG_DATABASE: postgres PG_PASSWORD: postgres PG_PORT: ${{ job.services.postgres.ports['5432'] }} - - run: find ./gen + - name: Save results + uses: actions/upload-artifact@v3 + with: + name: sqlc-pg-gen-results + path: gen From 743bdab5f1b13a9b989e1af7e8de073d2fc64438 Mon Sep 17 00:00:00 2001 From: Kyle Conroy Date: Wed, 9 Nov 2022 23:04:25 -0800 Subject: [PATCH 9/9] Only run workflow manually --- .github/workflows/gen.yml | 1 - 1 file changed, 1 deletion(-) diff --git a/.github/workflows/gen.yml b/.github/workflows/gen.yml index b1146aa028..fa5fd3e295 100644 --- a/.github/workflows/gen.yml +++ b/.github/workflows/gen.yml @@ -1,7 +1,6 @@ name: sqlc-pg-gen on: workflow_dispatch: - pull_request: jobs: gen: name: sqlc-pg-gen