MCPcopy
hub / github.com/sqlc-dev/sqlc / postgreSQL

Function postgreSQL

internal/sqltest/local/postgres.go:33–136  ·  view source on GitHub ↗
(t *testing.T, migrations []string, rw bool)

Source from the content-addressed store, hash-verified

31}
32
33func postgreSQL(t *testing.T, migrations []string, rw bool) string {
34 ctx := context.Background()
35 t.Helper()
36
37 dburi := os.Getenv("POSTGRESQL_SERVER_URI")
38 if dburi == "" {
39 if ierr := docker.Installed(); ierr == nil {
40 u, err := docker.StartPostgreSQLServer(ctx)
41 if err != nil {
42 t.Fatal(err)
43 }
44 dburi = u
45 } else if ierr := native.Supported(); ierr == nil {
46 // Fall back to native installation when Docker is not available
47 u, err := native.StartPostgreSQLServer(ctx)
48 if err != nil {
49 t.Fatal(err)
50 }
51 dburi = u
52 } else {
53 t.Skip("POSTGRESQL_SERVER_URI is empty and neither Docker nor native installation is available")
54 }
55 }
56
57 postgresPool, err := cache.Open(ctx, dburi)
58 if err != nil {
59 t.Fatalf("PostgreSQL pool creation failed: %s", err)
60 }
61
62 var seed []string
63 files, err := sqlpath.Glob(migrations)
64 if err != nil {
65 t.Fatal(err)
66 }
67
68 h := fnv.New64()
69 for _, f := range files {
70 blob, err := os.ReadFile(f)
71 if err != nil {
72 t.Fatal(err)
73 }
74 h.Write(blob)
75 seed = append(seed, migrate.RemoveRollbackStatements(string(blob)))
76 }
77
78 var name string
79 if rw {
80 name = fmt.Sprintf("sqlc_test_%s", id())
81 } else {
82 name = fmt.Sprintf("sqlc_test_%x", h.Sum(nil))
83 }
84
85 uri, err := url.Parse(dburi)
86 if err != nil {
87 t.Fatal(err)
88 }
89 uri.Path = name
90 dropQuery := fmt.Sprintf(`DROP DATABASE IF EXISTS "%s" WITH (FORCE)`, name)

Callers 2

PostgreSQLFunction · 0.85
ReadOnlyPostgreSQLFunction · 0.85

Calls 13

InstalledFunction · 0.92
StartPostgreSQLServerFunction · 0.92
SupportedFunction · 0.92
StartPostgreSQLServerFunction · 0.92
GlobFunction · 0.92
OpenMethod · 0.80
idFunction · 0.70
ParseMethod · 0.65
QueryRowMethod · 0.65
ExecMethod · 0.65
CloseMethod · 0.65
StringMethod · 0.45

Tested by

no test coverage detected