@@ -2,16 +2,13 @@ package cmd
22
33import (
44 "context"
5- "errors"
65 "fmt"
76 "os"
8- "os/exec"
97 "runtime/trace"
10- "strings"
118
129 "github.com/spf13/cobra"
1310 "github.com/sqlc-dev/sqlc/internal/config"
14- "github.com/sqlc-dev/sqlc/internal/opts "
11+ "github.com/sqlc-dev/sqlc/internal/migrations "
1512 "github.com/sqlc-dev/sqlc/internal/quickdb"
1613 pb "github.com/sqlc-dev/sqlc/internal/quickdb/v1"
1714 "github.com/sqlc-dev/sqlc/internal/sql/sqlpath"
@@ -20,103 +17,74 @@ import (
2017var createDBCmd = & cobra.Command {
2118 Use : "createdb" ,
2219 Short : "Create an ephemeral database" ,
23- Args : cobra .MinimumNArgs ( 1 ) ,
20+ Args : cobra .NoArgs ,
2421 RunE : func (cmd * cobra.Command , args []string ) error {
2522 defer trace .StartRegion (cmd .Context (), "createdb" ).End ()
2623 stderr := cmd .ErrOrStderr ()
2724 dir , name := getConfigPath (stderr , cmd .Flag ("file" ))
28- env , err := cmd .Flags ().GetString ("env" )
29- if err != nil {
30- return err
31- }
32- code , err := CreateDB (cmd .Context (), dir , name , args , env , & Options {
25+ err := CreateDB (cmd .Context (), dir , name , & Options {
3326 Env : ParseEnv (cmd ),
3427 Stderr : stderr ,
3528 })
3629 if err != nil {
3730 fmt .Fprintln (stderr , err .Error ())
38- os .Exit (code )
31+ os .Exit (1 )
3932 }
4033 return nil
4134 },
4235}
4336
44- func CreateDB (ctx context.Context , dir , filename string , args []string , env string , o * Options ) (int , error ) {
45- dbg := opts .DebugFromEnv ()
46- if ! dbg .ProcessPlugins {
47- return 1 , fmt .Errorf ("process-plugins disabled" )
48- }
37+ func CreateDB (ctx context.Context , dir , filename string , o * Options ) error {
4938 _ , conf , err := o .ReadConfig (dir , filename )
5039 if err != nil {
51- return 1 , err
40+ return err
5241 }
53- // Find the first SQL with a managed database
54- var pkg * config.SQL
42+ // Find the first queryset with a managed database
43+ var queryset * config.SQL
44+ var count int
5545 for _ , sql := range conf .SQL {
5646 sql := sql
5747 if sql .Database != nil && sql .Database .Managed {
58- pkg = & sql
59- break
48+ queryset = & sql
49+ count += 1
6050 }
6151 }
62- if pkg == nil {
63- return 1 , fmt .Errorf ("no managed database found" )
52+ if queryset == nil {
53+ return fmt .Errorf ("no querysets configured to use a managed database" )
54+ }
55+ if count > 1 {
56+ return fmt .Errorf ("multiple querysets configured to use managed databases" )
6457 }
65- if pkg .Engine != config .EnginePostgreSQL {
66- return 1 , fmt .Errorf ("managed: only PostgreSQL currently " )
58+ if queryset .Engine != config .EnginePostgreSQL {
59+ return fmt .Errorf ("managed databases currently only support PostgreSQL " )
6760 }
6861
69- var migrations []string
70- files , err := sqlpath .Glob (pkg .Schema )
62+ var ddl []string
63+ files , err := sqlpath .Glob (queryset .Schema )
7164 if err != nil {
72- return 1 , err
65+ return err
7366 }
7467 for _ , schema := range files {
7568 contents , err := os .ReadFile (schema )
7669 if err != nil {
77- return 1 , fmt .Errorf ("read file: %w" , err )
70+ return fmt .Errorf ("read file: %w" , err )
7871 }
79- migrations = append (migrations , string (contents ))
72+ ddl = append (ddl , migrations . RemoveRollbackStatements ( string (contents ) ))
8073 }
74+
8175 client , err := quickdb .NewClientFromConfig (conf .Cloud )
8276 if err != nil {
83- return 1 , fmt .Errorf ("client error: %w" , err )
77+ return fmt .Errorf ("client error: %w" , err )
8478 }
8579
8680 resp , err := client .CreateEphemeralDatabase (ctx , & pb.CreateEphemeralDatabaseRequest {
8781 Engine : "postgresql" ,
8882 Region : quickdb .GetClosestRegion (),
89- Migrations : migrations ,
83+ Migrations : ddl ,
9084 })
9185 if err != nil {
92- return 1 , fmt .Errorf ("managed: create database: %w" , err )
93- }
94-
95- defer func () {
96- client .DropEphemeralDatabase (ctx , & pb.DropEphemeralDatabaseRequest {
97- DatabaseId : resp .DatabaseId ,
98- })
99- }()
100-
101- cmd := exec .Command (args [0 ], args [1 :]... )
102- cmd .Env = append (os .Environ (), fmt .Sprintf ("%s=%s" , env , resp .Uri ))
103- cmd .Stdout = os .Stdout
104- cmd .Stderr = os .Stderr
105- cmd .Env = []string {fmt .Sprintf ("%s=%s" , env , resp .Uri )}
106- for _ , val := range os .Environ () {
107- if strings .HasPrefix (val , "SQLC_AUTH_TOKEN" ) {
108- continue
109- }
110- cmd .Env = append (cmd .Env , val )
86+ return fmt .Errorf ("managed: create database: %w" , err )
11187 }
112-
113- if err := cmd .Run (); err != nil {
114- var exitErr * exec.ExitError
115- if errors .As (err , & exitErr ) {
116- return exitErr .ExitCode (), err
117- }
118- return 1 , err
119- }
120-
121- return 0 , nil
88+ fmt .Println (resp .Uri )
89+ return nil
12290}
0 commit comments