@@ -26,6 +26,8 @@ import (
26
26
"github.com/sqlc-dev/sqlc/internal/debug"
27
27
"github.com/sqlc-dev/sqlc/internal/opts"
28
28
"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"
29
31
"github.com/sqlc-dev/sqlc/internal/shfmt"
30
32
"github.com/sqlc-dev/sqlc/internal/vet"
31
33
)
@@ -376,6 +378,62 @@ type checker struct {
376
378
Envmap map [string ]string
377
379
Stderr io.Writer
378
380
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
379
437
}
380
438
381
439
func (c * checker ) DSN (dsn string ) (string , error ) {
@@ -422,10 +480,16 @@ func (c *checker) checkSQL(ctx context.Context, s config.SQL) error {
422
480
if c .NoDatabase {
423
481
return fmt .Errorf ("database: connections disabled via command line flag" )
424
482
}
425
- dburl , err := c .DSN ( s . Database . URI )
483
+ dburl , cleanup , err := c .fetchDatabaseUri ( ctx , s )
426
484
if err != nil {
427
485
return err
428
486
}
487
+ defer func () {
488
+ if err := cleanup (); err != nil {
489
+ fmt .Fprintf (c .Stderr , "error cleaning up: %s\n " , err )
490
+ }
491
+ }()
492
+
429
493
switch s .Engine {
430
494
case config .EnginePostgreSQL :
431
495
conn , err := pgx .Connect (ctx , dburl )
0 commit comments