Skip to content

Commit 0d8d73d

Browse files
committed
feat(vet): Support managed databases
1 parent 1aa479f commit 0d8d73d

File tree

3 files changed

+69
-4
lines changed

3 files changed

+69
-4
lines changed

internal/cmd/vet.go

Lines changed: 65 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@ import (
2626
"github.com/sqlc-dev/sqlc/internal/debug"
2727
"github.com/sqlc-dev/sqlc/internal/opts"
2828
"github.com/sqlc-dev/sqlc/internal/plugin"
29+
"github.com/sqlc-dev/sqlc/internal/quickdb"
30+
pb "github.com/sqlc-dev/sqlc/internal/quickdb/v1"
2931
"github.com/sqlc-dev/sqlc/internal/shfmt"
3032
"github.com/sqlc-dev/sqlc/internal/vet"
3133
)
@@ -376,6 +378,62 @@ type checker struct {
376378
Envmap map[string]string
377379
Stderr io.Writer
378380
NoDatabase bool
381+
Client pb.QuickClient
382+
}
383+
384+
func (c *checker) fetchDatabaseUri(ctx context.Context, s config.SQL) (string, func() error, error) {
385+
cleanup := func() error {
386+
return nil
387+
}
388+
389+
if s.Database == nil {
390+
panic("fetch database URI called with nil database")
391+
}
392+
if !s.Database.Managed {
393+
uri, err := c.DSN(s.Database.URI)
394+
return uri, cleanup, err
395+
}
396+
if s.Engine != config.EnginePostgreSQL {
397+
return "", cleanup, fmt.Errorf("managed: only PostgreSQL currently")
398+
}
399+
400+
if c.Client == nil {
401+
// FIXME: Eventual race condition
402+
client, err := quickdb.NewClientFromConfig(c.Conf.Cloud)
403+
if err != nil {
404+
return "", cleanup, fmt.Errorf("managed: client: %w", err)
405+
}
406+
c.Client = client
407+
}
408+
409+
var migrations []string
410+
for _, query := range s.Schema {
411+
contents, err := os.ReadFile(query)
412+
if err != nil {
413+
return "", cleanup, fmt.Errorf("read file: %w", err)
414+
}
415+
migrations = append(migrations, string(contents))
416+
}
417+
418+
start := time.Now()
419+
resp, err := c.Client.CreateEphemeralDatabase(ctx, &pb.CreateEphemeralDatabaseRequest{
420+
Engine: "postgresql",
421+
Region: "sjc",
422+
Migrations: migrations,
423+
})
424+
if err != nil {
425+
return "", cleanup, fmt.Errorf("managed: create database: %w", err)
426+
}
427+
428+
cleanup = func() error {
429+
_, err := c.Client.DropEphemeralDatabase(ctx, &pb.DropEphemeralDatabaseRequest{
430+
DatabaseId: resp.DatabaseId,
431+
})
432+
return err
433+
}
434+
435+
fmt.Println("createdb+template", time.Since(start))
436+
return resp.Uri, cleanup, nil
379437
}
380438

381439
func (c *checker) DSN(dsn string) (string, error) {
@@ -422,10 +480,16 @@ func (c *checker) checkSQL(ctx context.Context, s config.SQL) error {
422480
if c.NoDatabase {
423481
return fmt.Errorf("database: connections disabled via command line flag")
424482
}
425-
dburl, err := c.DSN(s.Database.URI)
483+
dburl, cleanup, err := c.fetchDatabaseUri(ctx, s)
426484
if err != nil {
427485
return err
428486
}
487+
defer func() {
488+
if err := cleanup(); err != nil {
489+
fmt.Fprintf(c.Stderr, "error cleaning up: %s\n", err)
490+
}
491+
}()
492+
429493
switch s.Engine {
430494
case config.EnginePostgreSQL:
431495
conn, err := pgx.Connect(ctx, dburl)

internal/config/config.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,8 @@ type Project struct {
6969
}
7070

7171
type Database struct {
72-
URI string `json:"uri" yaml:"uri"`
72+
URI string `json:"uri" yaml:"uri"`
73+
Managed bool `json:"managed" yaml:"managed"`
7374
}
7475

7576
type Cloud struct {

internal/config/validate.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@ func Validate(c *Config) error {
1212
return fmt.Errorf("invalid config: emit_methods_with_db_argument and emit_prepared_queries settings are mutually exclusive")
1313
}
1414
if sql.Database != nil {
15-
if sql.Database.URI == "" {
16-
return fmt.Errorf("invalid config: database must have a non-empty URI")
15+
if sql.Database.URI == "" && !sql.Database.Managed {
16+
return fmt.Errorf("invalid config: database must be managed or have a non-empty URI")
1717
}
1818
}
1919
}

0 commit comments

Comments
 (0)