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

Function MySQL

internal/sqltest/local/mysql.go:23–109  ·  view source on GitHub ↗
(t *testing.T, migrations []string)

Source from the content-addressed store, hash-verified

21var mysqlPool *sql.DB
22
23func MySQL(t *testing.T, migrations []string) string {
24 ctx := context.Background()
25 t.Helper()
26
27 dburi := os.Getenv("MYSQL_SERVER_URI")
28 if dburi == "" {
29 if ierr := docker.Installed(); ierr == nil {
30 u, err := docker.StartMySQLServer(ctx)
31 if err != nil {
32 t.Fatal(err)
33 }
34 dburi = u
35 } else if ierr := native.Supported(); ierr == nil {
36 // Fall back to native installation when Docker is not available
37 u, err := native.StartMySQLServer(ctx)
38 if err != nil {
39 t.Fatal(err)
40 }
41 dburi = u
42 } else {
43 t.Skip("MYSQL_SERVER_URI is empty and neither Docker nor native installation is available")
44 }
45 }
46
47 mysqlSync.Do(func() {
48 db, err := sql.Open("mysql", dburi)
49 if err != nil {
50 t.Fatal(err)
51 }
52 mysqlPool = db
53 })
54
55 if mysqlPool == nil {
56 t.Fatalf("MySQL pool creation failed")
57 }
58
59 var seed []string
60 files, err := sqlpath.Glob(migrations)
61 if err != nil {
62 t.Fatal(err)
63 }
64 for _, f := range files {
65 blob, err := os.ReadFile(f)
66 if err != nil {
67 t.Fatal(err)
68 }
69 seed = append(seed, migrate.RemoveRollbackStatements(string(blob)))
70 }
71
72 cfg, err := mysql.ParseDSN(dburi)
73 if err != nil {
74 t.Fatal(err)
75 }
76
77 name := fmt.Sprintf("sqlc_test_%s", id())
78
79 if _, err := mysqlPool.ExecContext(ctx, fmt.Sprintf("CREATE DATABASE `%s`", name)); err != nil {
80 t.Fatal(err)

Callers 6

TestExamplesVetFunction · 0.92
TestValidSchemaFunction · 0.92
TestBooksFunction · 0.92
TestAuthorsFunction · 0.92
TestPreparedFunction · 0.92
TestQueriesFunction · 0.92

Calls 9

InstalledFunction · 0.92
StartMySQLServerFunction · 0.92
SupportedFunction · 0.92
StartMySQLServerFunction · 0.92
GlobFunction · 0.92
OpenMethod · 0.80
idFunction · 0.70
ExecContextMethod · 0.65
CloseMethod · 0.65

Tested by 6

TestExamplesVetFunction · 0.74
TestValidSchemaFunction · 0.74
TestBooksFunction · 0.74
TestAuthorsFunction · 0.74
TestPreparedFunction · 0.74
TestQueriesFunction · 0.74