1
1
package main
2
2
3
3
import (
4
- "context"
5
4
"fmt"
6
5
"os"
7
6
"path/filepath"
8
- "runtime"
9
- "slices"
10
- "strings"
11
7
"testing"
12
8
13
- "github.com/jackc/pgx/v5"
14
-
15
9
"github.com/sqlc-dev/sqlc/internal/config"
16
- "github.com/sqlc-dev/sqlc/internal/migrations"
17
- "github.com/sqlc-dev/sqlc/internal/quickdb"
18
- pb "github.com/sqlc-dev/sqlc/internal/quickdb/v1"
19
- "github.com/sqlc-dev/sqlc/internal/sql/sqlpath"
10
+ "github.com/sqlc-dev/sqlc/internal/sqltest/local"
20
11
)
21
12
22
13
func TestValidSchema (t * testing.T ) {
23
- if os .Getenv ("CI" ) != "" && runtime .GOOS != "linux" {
24
- t .Skipf ("only run these tests in CI on linux: %s %s" , os .Getenv ("CI" ), runtime .GOOS )
25
- }
26
-
27
- ctx := context .Background ()
28
-
29
- projectID := os .Getenv ("CI_SQLC_PROJECT_ID" )
30
- authToken := os .Getenv ("CI_SQLC_AUTH_TOKEN" )
31
- if projectID == "" || authToken == "" {
32
- t .Skip ("missing project id or auth token" )
33
- }
34
-
35
- client , err := quickdb .NewClient (projectID , authToken )
36
- if err != nil {
37
- t .Fatal (err )
38
- }
39
-
40
- for _ , replay := range FindTests (t , "testdata" , "managed-db" ) {
14
+ for _ , replay := range FindTests (t , "testdata" , "base" ) {
41
15
replay := replay // https://golang.org/doc/faq#closures_and_goroutines
42
16
43
- if len (replay .Stderr ) > 0 {
44
- continue
45
- }
46
-
47
17
if replay .Exec != nil {
48
- if ! slices . Contains ( replay .Exec .Contexts , "managed-db" ) {
18
+ if replay .Exec .Meta . InvalidSchema {
49
19
continue
50
20
}
51
21
}
@@ -63,60 +33,28 @@ func TestValidSchema(t *testing.T) {
63
33
64
34
for j , pkg := range conf .SQL {
65
35
j , pkg := j , pkg
66
- if pkg .Engine != config .EnginePostgreSQL {
36
+ switch pkg .Engine {
37
+ case config .EnginePostgreSQL :
38
+ // pass
39
+ case config .EngineMySQL :
40
+ // pass
41
+ default :
67
42
continue
68
43
}
69
44
t .Run (fmt .Sprintf ("endtoend-%s-%d" , file , j ), func (t * testing.T ) {
70
45
t .Parallel ()
71
46
72
- if strings .Contains (file , "pg_dump" ) {
73
- t .Skip ("loading pg_dump not supported" )
74
- }
75
-
76
47
var schema []string
77
48
for _ , path := range pkg .Schema {
78
49
schema = append (schema , filepath .Join (filepath .Dir (file ), path ))
79
50
}
80
51
81
- files , err := sqlpath .Glob (schema )
82
- if err != nil {
83
- t .Fatal (err )
84
- }
85
-
86
- var sqls []string
87
- for _ , f := range files {
88
- contents , err := os .ReadFile (f )
89
- if err != nil {
90
- t .Fatalf ("%s: %s" , f , err )
91
- }
92
- // Support loading pg_dump SQL files
93
- before := strings .ReplaceAll (string (contents ), "CREATE SCHEMA public;" , "CREATE SCHEMA IF NOT EXISTS public;" )
94
- sqls = append (sqls , migrations .RemoveRollbackStatements (before ))
95
- }
96
-
97
- resp , err := client .CreateEphemeralDatabase (ctx , & pb.CreateEphemeralDatabaseRequest {
98
- Engine : "postgresql" ,
99
- Region : quickdb .GetClosestRegion (),
100
- Migrations : sqls ,
101
- })
102
- if err != nil {
103
- t .Fatalf ("region %s: %s" , quickdb .GetClosestRegion (), err )
104
- }
105
-
106
- t .Cleanup (func () {
107
- _ , err = client .DropEphemeralDatabase (ctx , & pb.DropEphemeralDatabaseRequest {
108
- DatabaseId : resp .DatabaseId ,
109
- })
110
- if err != nil {
111
- t .Fatal (err )
112
- }
113
- })
114
-
115
- conn , err := pgx .Connect (ctx , resp .Uri )
116
- if err != nil {
117
- t .Fatalf ("connect %s: %s" , resp .Uri , err )
52
+ switch pkg .Engine {
53
+ case config .EnginePostgreSQL :
54
+ local .PostgreSQL (t , schema )
55
+ case config .EngineMySQL :
56
+ local .MySQL (t , schema )
118
57
}
119
- defer conn .Close (ctx )
120
58
})
121
59
}
122
60
}
0 commit comments