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

Function CreatePostgreSQLDatabase

internal/sqltest/postgres.go:39–117  ·  view source on GitHub ↗
(t *testing.T, name string, schema bool, migrations []string)

Source from the content-addressed store, hash-verified

37}
38
39func CreatePostgreSQLDatabase(t *testing.T, name string, schema bool, migrations []string) (*sql.DB, func()) {
40 t.Helper()
41
42 pgUser := os.Getenv("PG_USER")
43 pgHost := os.Getenv("PG_HOST")
44 pgPort := os.Getenv("PG_PORT")
45 pgPass := os.Getenv("PG_PASSWORD")
46 pgDB := os.Getenv("PG_DATABASE")
47
48 if pgUser == "" {
49 pgUser = "postgres"
50 }
51
52 if pgPass == "" {
53 pgPass = "mysecretpassword"
54 }
55
56 if pgPort == "" {
57 pgPort = "5432"
58 }
59
60 if pgHost == "" {
61 pgHost = "127.0.0.1"
62 }
63
64 if pgDB == "" {
65 pgDB = "dinotest"
66 }
67
68 source := fmt.Sprintf("postgres://%s:%s@%s:%s/%s?sslmode=disable", pgUser, pgPass, pgHost, pgPort, pgDB)
69 t.Logf("db: %s", source)
70
71 db, err := sql.Open("postgres", source)
72 if err != nil {
73 t.Fatal(err)
74 }
75
76 // For each test, pick a new schema name at random.
77 var newsource, dropQuery string
78 if schema {
79 if _, err := db.Exec("CREATE SCHEMA " + name); err != nil {
80 t.Fatal(err)
81 }
82 newsource = source + "&search_path=" + name
83 dropQuery = "DROP SCHEMA " + name + " CASCADE"
84 } else {
85 if _, err := db.Exec("CREATE DATABASE " + name); err != nil {
86 t.Fatal(err)
87 }
88 newsource = fmt.Sprintf("postgres://%s:%s@%s:%s/%s?sslmode=disable", pgUser, pgPass, pgHost, pgPort, name)
89 dropQuery = "DROP DATABASE IF EXISTS " + name + " WITH (FORCE)"
90 }
91
92 sdb, err := sql.Open("postgres", newsource)
93 if err != nil {
94 t.Fatal(err)
95 }
96

Callers 1

PostgreSQLFunction · 0.85

Calls 4

GlobFunction · 0.92
OpenMethod · 0.80
ExecMethod · 0.65
CloseMethod · 0.65

Tested by

no test coverage detected