mirror of
https://github.com/coder/coder.git
synced 2026-06-02 20:48:20 +00:00
fix: bump sqlc fork to v1.31.1 merge, strip pg_dump meta-commands (#25105)
Closes https://github.com/coder/internal/issues/965 Recent `pg_dump` patch releases (13.22+ / 14.19+ / 15.14+ / 16.10+ / 17.6+) emit `\restrict` / `\unrestrict` psql meta-commands at the head and tail of schema dumps. These broke both `sqlc` and our `scripts/migrate-test` schema-equality check. PR #19696 worked around it by pinning `pg_dump` to a Docker image. This change unpins the workaround now that `sqlc` handles the meta-commands: * Bumps the coder/sqlc fork pin to [`337309b` on coder/sqlc:main](https://github.com/coder/sqlc/commit/337309bfb9524f38466a5090e310040fc7af0203), the merge of upstream v1.31.1 (coder/sqlc#6). v1.31.1 includes [sqlc-dev/sqlc#4390](https://github.com/sqlc-dev/sqlc/pull/4390), the upstream `\restrict` / `\unrestrict` parser fix. Updated in three places that pin the fork SHA: `flake.nix` (`sqlc-custom`), `.github/actions/setup-sqlc/action.yaml`, and the `dogfood/coder/ubuntu-{22,26}.04` Dockerfiles. The flake's `sha256` / `vendorHash` are reset to `pkgs.lib.fakeSha256`; Nix will surface the real hashes on first build, per the existing comment block. * Reverts #19696's Docker pin in `coderd/database/dbtestutil/db.go`. Local `pg_dump` (13+) and the `postgres:13` Docker fallback both work again. * Strips `\restrict` / `\unrestrict` lines in `normalizeDump` so `scripts/migrate-test`'s schema comparison is stable across `pg_dump` versions (the token in those lines is randomized per run). `TestNormalizeDumpStripsRestrict` locks the behavior in. * Regenerates with v1.31.1, picking up the version stamp and one upstream correctness fix in `DeleteLicense` ([sqlc-dev/sqlc#4383](https://github.com/sqlc-dev/sqlc/pull/4383): don't shadow the input parameter when scanning a single-column return).
This commit is contained in:
@@ -14,4 +14,4 @@ runs:
|
||||
# - https://github.com/sqlc-dev/sqlc/pull/4159
|
||||
shell: bash
|
||||
run: |
|
||||
./.github/scripts/retry.sh -- env CGO_ENABLED=1 go install github.com/coder/sqlc/cmd/sqlc@aab4e865a51df0c43e1839f81a9d349b41d14f05
|
||||
./.github/scripts/retry.sh -- env CGO_ENABLED=1 go install github.com/coder/sqlc/cmd/sqlc@337309bfb9524f38466a5090e310040fc7af0203
|
||||
|
||||
@@ -10,6 +10,7 @@ import (
|
||||
"os/exec"
|
||||
"path/filepath"
|
||||
"regexp"
|
||||
"strconv"
|
||||
"strings"
|
||||
"testing"
|
||||
"time"
|
||||
@@ -240,31 +241,26 @@ func PGDump(dbURL string) ([]byte, error) {
|
||||
return stdout.Bytes(), nil
|
||||
}
|
||||
|
||||
const (
|
||||
minimumPostgreSQLVersion = 13
|
||||
postgresImageSha = "sha256:467e7f2fb97b2f29d616e0be1d02218a7bbdfb94eb3cda7461fd80165edfd1f7"
|
||||
)
|
||||
const minimumPostgreSQLVersion = 13
|
||||
|
||||
// PGDumpSchemaOnly is for use by gen/dump only.
|
||||
// It runs pg_dump against dbURL and sets a consistent timezone and encoding.
|
||||
func PGDumpSchemaOnly(dbURL string) ([]byte, error) {
|
||||
hasPGDump := false
|
||||
// TODO: Temporarily pin pg_dump to the docker image until
|
||||
// https://github.com/sqlc-dev/sqlc/issues/4065 is resolved.
|
||||
// if _, err := exec.LookPath("pg_dump"); err == nil {
|
||||
// out, err := exec.Command("pg_dump", "--version").Output()
|
||||
// if err == nil {
|
||||
// // Parse output:
|
||||
// // pg_dump (PostgreSQL) 14.5 (Ubuntu 14.5-0ubuntu0.22.04.1)
|
||||
// parts := strings.Split(string(out), " ")
|
||||
// if len(parts) > 2 {
|
||||
// version, err := strconv.Atoi(strings.Split(parts[2], ".")[0])
|
||||
// if err == nil && version >= minimumPostgreSQLVersion {
|
||||
// hasPGDump = true
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
if _, err := exec.LookPath("pg_dump"); err == nil {
|
||||
out, err := exec.Command("pg_dump", "--version").Output()
|
||||
if err == nil {
|
||||
// Parse output:
|
||||
// pg_dump (PostgreSQL) 14.5 (Ubuntu 14.5-0ubuntu0.22.04.1)
|
||||
parts := strings.Split(string(out), " ")
|
||||
if len(parts) > 2 {
|
||||
version, err := strconv.Atoi(strings.Split(parts[2], ".")[0])
|
||||
if err == nil && version >= minimumPostgreSQLVersion {
|
||||
hasPGDump = true
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
cmdArgs := []string{
|
||||
"pg_dump",
|
||||
@@ -289,7 +285,7 @@ func PGDumpSchemaOnly(dbURL string) ([]byte, error) {
|
||||
"run",
|
||||
"--rm",
|
||||
"--network=host",
|
||||
fmt.Sprintf("%s:%d@%s", postgresImage, minimumPostgreSQLVersion, postgresImageSha),
|
||||
fmt.Sprintf("%s:%d", postgresImage, minimumPostgreSQLVersion),
|
||||
}, cmdArgs...)
|
||||
}
|
||||
cmd := exec.Command(cmdArgs[0], cmdArgs[1:]...) //#nosec
|
||||
@@ -310,6 +306,11 @@ func PGDumpSchemaOnly(dbURL string) ([]byte, error) {
|
||||
func normalizeDump(schema []byte) []byte {
|
||||
// Remove all comments.
|
||||
schema = regexp.MustCompile(`(?im)^(--.*)$`).ReplaceAll(schema, []byte{})
|
||||
// Strip psql meta-commands (\restrict / \unrestrict) emitted by pg_dump
|
||||
// 13.22+ / 14.19+ / 15.14+ / 16.10+ / 17.6+. The token in these lines is
|
||||
// randomized per run, so we drop them entirely. See
|
||||
// https://github.com/coder/internal/issues/965.
|
||||
schema = regexp.MustCompile(`(?im)^\\(restrict|unrestrict).*$`).ReplaceAll(schema, []byte{})
|
||||
// Public is implicit in the schema.
|
||||
schema = regexp.MustCompile(`(?im)( |::|'|\()public\.`).ReplaceAll(schema, []byte(`$1`))
|
||||
// Remove database settings.
|
||||
|
||||
@@ -0,0 +1,32 @@
|
||||
package dbtestutil
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
|
||||
// Recent pg_dump versions (13.22+ / 14.19+ / 15.14+ / 16.10+ / 17.6+) emit
|
||||
// psql meta-commands at the head and tail of the dump that aren't valid SQL.
|
||||
// normalizeDump is expected to strip them so downstream consumers (sqlc,
|
||||
// schema-equality checks in scripts/migrate-test) don't have to.
|
||||
//
|
||||
// See https://github.com/coder/internal/issues/965.
|
||||
func TestNormalizeDumpStripsRestrict(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
// Raw string literals (backticks) make backslashes literal, so the
|
||||
// meta-command here matches what pg_dump actually emits.
|
||||
input := []byte(`-- header
|
||||
\restrict XYZ
|
||||
|
||||
CREATE TABLE foo;
|
||||
|
||||
\unrestrict XYZ
|
||||
`)
|
||||
|
||||
out := string(normalizeDump(input))
|
||||
require.NotContains(t, out, `\restrict`, `normalizeDump must strip \restrict psql meta-command`)
|
||||
require.NotContains(t, out, `\unrestrict`, `normalizeDump must strip \unrestrict psql meta-command`)
|
||||
require.Contains(t, out, "CREATE TABLE foo;", "normalizeDump must preserve real SQL between the meta-commands")
|
||||
}
|
||||
@@ -1,6 +1,6 @@
|
||||
// Code generated by sqlc. DO NOT EDIT.
|
||||
// versions:
|
||||
// sqlc v1.30.0
|
||||
// sqlc v1.31.1
|
||||
|
||||
package database
|
||||
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
// Code generated by sqlc. DO NOT EDIT.
|
||||
// versions:
|
||||
// sqlc v1.30.0
|
||||
// sqlc v1.31.1
|
||||
|
||||
package database
|
||||
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
// Code generated by sqlc. DO NOT EDIT.
|
||||
// versions:
|
||||
// sqlc v1.30.0
|
||||
// sqlc v1.31.1
|
||||
|
||||
package database
|
||||
|
||||
@@ -12997,8 +12997,9 @@ RETURNING id
|
||||
|
||||
func (q *sqlQuerier) DeleteLicense(ctx context.Context, id int32) (int32, error) {
|
||||
row := q.db.QueryRowContext(ctx, deleteLicense, id)
|
||||
err := row.Scan(&id)
|
||||
return id, err
|
||||
var id_2 int32
|
||||
err := row.Scan(&id_2)
|
||||
return id_2, err
|
||||
}
|
||||
|
||||
const getLicenseByID = `-- name: GetLicenseByID :one
|
||||
|
||||
@@ -51,7 +51,7 @@ RUN apt-get update && \
|
||||
# Switched to coder/sqlc fork to fix ambiguous column bug, see:
|
||||
# - https://github.com/coder/sqlc/pull/1
|
||||
# - https://github.com/sqlc-dev/sqlc/pull/4159
|
||||
(CGO_ENABLED=1 go install github.com/coder/sqlc/cmd/sqlc@aab4e865a51df0c43e1839f81a9d349b41d14f05) && \
|
||||
(CGO_ENABLED=1 go install github.com/coder/sqlc/cmd/sqlc@337309bfb9524f38466a5090e310040fc7af0203) && \
|
||||
# ruleguard for checking custom rules, without needing to run all of
|
||||
# golangci-lint. Check the go.mod in the release of golangci-lint that
|
||||
# we're using for the version of go-critic that it embeds, then check
|
||||
|
||||
@@ -51,7 +51,7 @@ RUN apt-get update && \
|
||||
# Switched to coder/sqlc fork to fix ambiguous column bug, see:
|
||||
# - https://github.com/coder/sqlc/pull/1
|
||||
# - https://github.com/sqlc-dev/sqlc/pull/4159
|
||||
(CGO_ENABLED=1 go install github.com/coder/sqlc/cmd/sqlc@aab4e865a51df0c43e1839f81a9d349b41d14f05) && \
|
||||
(CGO_ENABLED=1 go install github.com/coder/sqlc/cmd/sqlc@337309bfb9524f38466a5090e310040fc7af0203) && \
|
||||
# ruleguard for checking custom rules, without needing to run all of
|
||||
# golangci-lint. Check the go.mod in the release of golangci-lint that
|
||||
# we're using for the version of go-critic that it embeds, then check
|
||||
|
||||
@@ -96,17 +96,17 @@
|
||||
# 5. Update the vendorHash
|
||||
sqlc-custom = unstablePkgs.buildGo126Module {
|
||||
pname = "sqlc";
|
||||
version = "coder-fork-aab4e865a51df0c43e1839f81a9d349b41d14f05";
|
||||
version = "coder-fork-337309bfb9524f38466a5090e310040fc7af0203";
|
||||
|
||||
src = pkgs.fetchFromGitHub {
|
||||
owner = "coder";
|
||||
repo = "sqlc";
|
||||
rev = "aab4e865a51df0c43e1839f81a9d349b41d14f05";
|
||||
sha256 = "sha256-zXjTypEFWDOkoZMKHMMRtAz2coNHSCkQ+nuZ8rOnzZ8=";
|
||||
rev = "337309bfb9524f38466a5090e310040fc7af0203";
|
||||
sha256 = "sha256-i8hZaaMlNJyW0hUWYcuNqUcwRdQU747055OknZsJ9Es=";
|
||||
};
|
||||
|
||||
subPackages = [ "cmd/sqlc" ];
|
||||
vendorHash = "sha256-69kg3qkvEWyCAzjaCSr3a73MNonub9sZTYyGaCW+UTI=";
|
||||
vendorHash = "sha256-4Cb15MhKyhRvYVKfMqBwuC3WBBIJE6AinJt02+TSMVY=";
|
||||
};
|
||||
|
||||
# Keep Terraform aligned with provisioner/terraform/testdata/version.txt
|
||||
|
||||
Reference in New Issue
Block a user