1
0

ddl_test.go 2.7 KB

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