11package main
22
33import (
4- "context"
54 "fmt"
65 "os"
76 "path/filepath"
8- "runtime"
9- "slices"
10- "strings"
117 "testing"
128
13- "github.com/jackc/pgx/v5"
14-
159 "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"
2011)
2112
2213func 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" ) {
4115 replay := replay // https://golang.org/doc/faq#closures_and_goroutines
4216
43- if len (replay .Stderr ) > 0 {
44- continue
45- }
46-
4717 if replay .Exec != nil {
48- if ! slices . Contains ( replay .Exec .Contexts , "managed-db" ) {
18+ if replay .Exec .Meta . InvalidSchema {
4919 continue
5020 }
5121 }
@@ -63,60 +33,28 @@ func TestValidSchema(t *testing.T) {
6333
6434 for j , pkg := range conf .SQL {
6535 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 :
6742 continue
6843 }
6944 t .Run (fmt .Sprintf ("endtoend-%s-%d" , file , j ), func (t * testing.T ) {
7045 t .Parallel ()
7146
72- if strings .Contains (file , "pg_dump" ) {
73- t .Skip ("loading pg_dump not supported" )
74- }
75-
7647 var schema []string
7748 for _ , path := range pkg .Schema {
7849 schema = append (schema , filepath .Join (filepath .Dir (file ), path ))
7950 }
8051
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 )
11857 }
119- defer conn .Close (ctx )
12058 })
12159 }
12260 }
0 commit comments