diff --git a/.github/workflows/gen.yml b/.github/workflows/gen.yml new file mode 100644 index 0000000000..fa5fd3e295 --- /dev/null +++ b/.github/workflows/gen.yml @@ -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 + diff --git a/internal/tools/sqlc-pg-gen/main.go b/internal/tools/sqlc-pg-gen/main.go index f0c6de6cd3..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" @@ -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 } @@ -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"), }, } @@ -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) @@ -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", @@ -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 @@ -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", @@ -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",