-
Notifications
You must be signed in to change notification settings - Fork 1k
Expand file tree
/
Copy pathddl_test.go
More file actions
126 lines (108 loc) · 2.94 KB
/
ddl_test.go
File metadata and controls
126 lines (108 loc) · 2.94 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
package main
import (
"context"
"fmt"
"io/fs"
"os"
"path/filepath"
"strings"
"testing"
"github.com/jackc/pgx/v5"
"github.com/sqlc-dev/sqlc/internal/config"
"github.com/sqlc-dev/sqlc/internal/migrations"
"github.com/sqlc-dev/sqlc/internal/quickdb"
pb "github.com/sqlc-dev/sqlc/internal/quickdb/v1"
"github.com/sqlc-dev/sqlc/internal/sql/sqlpath"
)
func TestValidSchema(t *testing.T) {
ctx := context.Background()
projectID := os.Getenv("CI_SQLC_PROJECT_ID")
authToken := os.Getenv("CI_SQLC_AUTH_TOKEN")
if projectID == "" || authToken == "" {
t.Skip("missing project id or auth token")
}
client, err := quickdb.NewClient(projectID, authToken)
if err != nil {
t.Fatal(err)
}
files := []string{}
// Find all tests that do not have a stderr.txt file
err = filepath.Walk("testdata", func(path string, info fs.FileInfo, err error) error {
if err != nil {
return err
}
if filepath.Base(path) == "sqlc.json" || filepath.Base(path) == "sqlc.yaml" {
stderr := filepath.Join(filepath.Dir(path), "stderr.txt")
if _, err := os.Stat(stderr); !os.IsNotExist(err) {
return nil
}
files = append(files, path)
}
return nil
})
if err != nil {
t.Fatal(err)
}
for _, file := range files {
file := file // https://golang.org/doc/faq#closures_and_goroutines
rd, err := os.Open(file)
if err != nil {
t.Fatal(err)
}
conf, err := config.ParseConfig(rd)
if err != nil {
t.Fatal(err)
}
for j, pkg := range conf.SQL {
j, pkg := j, pkg
if pkg.Engine != config.EnginePostgreSQL {
continue
}
t.Run(fmt.Sprintf("endtoend-%s-%d", file, j), func(t *testing.T) {
t.Parallel()
if strings.Contains(file, "pg_dump") {
t.Skip("loading pg_dump not supported")
}
var schema []string
for _, path := range pkg.Schema {
schema = append(schema, filepath.Join(filepath.Dir(file), path))
}
files, err := sqlpath.Glob(schema)
if err != nil {
t.Fatal(err)
}
var sqls []string
for _, f := range files {
contents, err := os.ReadFile(f)
if err != nil {
t.Fatalf("%s: %s", f, err)
}
// Support loading pg_dump SQL files
before := strings.ReplaceAll(string(contents), "CREATE SCHEMA public;", "CREATE SCHEMA IF NOT EXISTS public;")
sqls = append(sqls, migrations.RemoveRollbackStatements(before))
}
resp, err := client.CreateEphemeralDatabase(ctx, &pb.CreateEphemeralDatabaseRequest{
Engine: "postgresql",
Region: quickdb.GetClosestRegion(),
Migrations: sqls,
})
if err != nil {
t.Fatalf("region %s: %s", quickdb.GetClosestRegion(), err)
}
t.Cleanup(func() {
_, err = client.DropEphemeralDatabase(ctx, &pb.DropEphemeralDatabaseRequest{
DatabaseId: resp.DatabaseId,
})
if err != nil {
t.Fatal(err)
}
})
conn, err := pgx.Connect(ctx, resp.Uri)
if err != nil {
t.Fatalf("connect %s: %s", resp.Uri, err)
}
defer conn.Close(ctx)
})
}
}
}