-
Notifications
You must be signed in to change notification settings - Fork 1k
Expand file tree
/
Copy pathdb.go
More file actions
78 lines (67 loc) · 1.49 KB
/
db.go
File metadata and controls
78 lines (67 loc) · 1.49 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
package hosted
import (
"context"
"fmt"
"os"
"sync"
"testing"
"github.com/sqlc-dev/sqlc/internal/quickdb"
pb "github.com/sqlc-dev/sqlc/internal/quickdb/v1"
"github.com/sqlc-dev/sqlc/internal/sql/sqlpath"
)
var client pb.QuickClient
var once sync.Once
func initClient() error {
projectID := os.Getenv("CI_SQLC_PROJECT_ID")
authToken := os.Getenv("CI_SQLC_AUTH_TOKEN")
if projectID == "" || authToken == "" {
return fmt.Errorf("missing project id or auth token")
}
c, err := quickdb.NewClient(projectID, authToken)
if err != nil {
return err
}
client = c
return nil
}
func PostgreSQL(t *testing.T, migrations []string) string {
ctx := context.Background()
t.Helper()
once.Do(func() {
if err := initClient(); err != nil {
t.Fatal(err)
}
})
if client == nil {
t.Fatalf("client init failed")
}
var seed []string
files, err := sqlpath.Glob(migrations)
if err != nil {
t.Fatal(err)
}
for _, f := range files {
blob, err := os.ReadFile(f)
if err != nil {
t.Fatal(err)
}
seed = append(seed, string(blob))
}
resp, err := client.CreateEphemeralDatabase(ctx, &pb.CreateEphemeralDatabaseRequest{
Engine: "postgresql",
Region: quickdb.GetClosestRegion(),
Migrations: seed,
})
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)
}
})
return resp.Uri
}