|
| 1 | +package dbmanager |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "fmt" |
| 6 | + "hash/fnv" |
| 7 | + "io" |
| 8 | + "net/url" |
| 9 | + "strings" |
| 10 | + |
| 11 | + "github.com/jackc/pgx/v5" |
| 12 | + "golang.org/x/sync/singleflight" |
| 13 | + |
| 14 | + "github.com/sqlc-dev/sqlc/internal/config" |
| 15 | + "github.com/sqlc-dev/sqlc/internal/pgx/poolcache" |
| 16 | + "github.com/sqlc-dev/sqlc/internal/shfmt" |
| 17 | +) |
| 18 | + |
| 19 | +type CreateDatabaseRequest struct { |
| 20 | + Engine string |
| 21 | + Migrations []string |
| 22 | +} |
| 23 | + |
| 24 | +type CreateDatabaseResponse struct { |
| 25 | + Uri string |
| 26 | +} |
| 27 | + |
| 28 | +type Client interface { |
| 29 | + CreateDatabase(context.Context, *CreateDatabaseRequest) (*CreateDatabaseResponse, error) |
| 30 | + Close(context.Context) |
| 31 | +} |
| 32 | + |
| 33 | +var flight singleflight.Group |
| 34 | + |
| 35 | +type ManagedClient struct { |
| 36 | + cache *poolcache.Cache |
| 37 | + replacer *shfmt.Replacer |
| 38 | + servers []config.Server |
| 39 | +} |
| 40 | + |
| 41 | +func dbid(migrations []string) string { |
| 42 | + h := fnv.New64() |
| 43 | + for _, query := range migrations { |
| 44 | + io.WriteString(h, query) |
| 45 | + } |
| 46 | + return fmt.Sprintf("%x", h.Sum(nil)) |
| 47 | +} |
| 48 | + |
| 49 | +func (m *ManagedClient) CreateDatabase(ctx context.Context, req *CreateDatabaseRequest) (*CreateDatabaseResponse, error) { |
| 50 | + hash := dbid(req.Migrations) |
| 51 | + name := fmt.Sprintf("sqlc_managed_%s", hash) |
| 52 | + |
| 53 | + var base string |
| 54 | + for _, server := range m.servers { |
| 55 | + if server.Engine == config.EnginePostgreSQL { |
| 56 | + base = server.URI |
| 57 | + break |
| 58 | + } |
| 59 | + } |
| 60 | + |
| 61 | + if strings.TrimSpace(base) == "" { |
| 62 | + return nil, fmt.Errorf("no PostgreSQL database server found") |
| 63 | + } |
| 64 | + |
| 65 | + serverUri := m.replacer.Replace(base) |
| 66 | + pool, err := m.cache.Open(ctx, serverUri) |
| 67 | + if err != nil { |
| 68 | + return nil, err |
| 69 | + } |
| 70 | + |
| 71 | + uri, err := url.Parse(serverUri) |
| 72 | + if err != nil { |
| 73 | + return nil, err |
| 74 | + } |
| 75 | + uri.Path = name |
| 76 | + |
| 77 | + key := uri.String() |
| 78 | + _, err, _ = flight.Do(key, func() (interface{}, error) { |
| 79 | + // TODO: Use a parameterized query |
| 80 | + row := pool.QueryRow(ctx, |
| 81 | + fmt.Sprintf(`SELECT datname FROM pg_database WHERE datname = '%s'`, name)) |
| 82 | + |
| 83 | + var datname string |
| 84 | + if err := row.Scan(&datname); err == nil { |
| 85 | + return nil, nil |
| 86 | + } |
| 87 | + |
| 88 | + if _, err := pool.Exec(ctx, fmt.Sprintf(`CREATE DATABASE "%s"`, name)); err != nil { |
| 89 | + return nil, err |
| 90 | + } |
| 91 | + |
| 92 | + conn, err := pgx.Connect(ctx, uri.String()) |
| 93 | + if err != nil { |
| 94 | + return nil, fmt.Errorf("connect %s: %s", name, err) |
| 95 | + } |
| 96 | + defer conn.Close(ctx) |
| 97 | + |
| 98 | + var migrationErr error |
| 99 | + for _, q := range req.Migrations { |
| 100 | + if len(strings.TrimSpace(q)) == 0 { |
| 101 | + continue |
| 102 | + } |
| 103 | + if _, err := conn.Exec(ctx, q); err != nil { |
| 104 | + migrationErr = fmt.Errorf("%s: %s", q, err) |
| 105 | + break |
| 106 | + } |
| 107 | + } |
| 108 | + |
| 109 | + if migrationErr != nil { |
| 110 | + pool.Exec(ctx, fmt.Sprintf(`DROP DATABASE "%s" IF EXISTS WITH (FORCE)`, name)) |
| 111 | + return nil, migrationErr |
| 112 | + } |
| 113 | + |
| 114 | + return nil, nil |
| 115 | + }) |
| 116 | + |
| 117 | + if err != nil { |
| 118 | + return nil, err |
| 119 | + } |
| 120 | + |
| 121 | + return &CreateDatabaseResponse{Uri: key}, err |
| 122 | +} |
| 123 | + |
| 124 | +func (m *ManagedClient) Close(ctx context.Context) { |
| 125 | + m.cache.Close() |
| 126 | +} |
| 127 | + |
| 128 | +func NewClient(servers []config.Server) *ManagedClient { |
| 129 | + return &ManagedClient{ |
| 130 | + cache: poolcache.New(), |
| 131 | + servers: servers, |
| 132 | + replacer: shfmt.NewReplacer(nil), |
| 133 | + } |
| 134 | +} |
0 commit comments