@@ -2,16 +2,13 @@ package cmd
2
2
3
3
import (
4
4
"context"
5
- "errors"
6
5
"fmt"
7
6
"os"
8
- "os/exec"
9
7
"runtime/trace"
10
- "strings"
11
8
12
9
"github.com/spf13/cobra"
13
10
"github.com/sqlc-dev/sqlc/internal/config"
14
- "github.com/sqlc-dev/sqlc/internal/opts "
11
+ "github.com/sqlc-dev/sqlc/internal/migrations "
15
12
"github.com/sqlc-dev/sqlc/internal/quickdb"
16
13
pb "github.com/sqlc-dev/sqlc/internal/quickdb/v1"
17
14
"github.com/sqlc-dev/sqlc/internal/sql/sqlpath"
@@ -20,103 +17,74 @@ import (
20
17
var createDBCmd = & cobra.Command {
21
18
Use : "createdb" ,
22
19
Short : "Create an ephemeral database" ,
23
- Args : cobra .MinimumNArgs ( 1 ) ,
20
+ Args : cobra .NoArgs ,
24
21
RunE : func (cmd * cobra.Command , args []string ) error {
25
22
defer trace .StartRegion (cmd .Context (), "createdb" ).End ()
26
23
stderr := cmd .ErrOrStderr ()
27
24
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 {
33
26
Env : ParseEnv (cmd ),
34
27
Stderr : stderr ,
35
28
})
36
29
if err != nil {
37
30
fmt .Fprintln (stderr , err .Error ())
38
- os .Exit (code )
31
+ os .Exit (1 )
39
32
}
40
33
return nil
41
34
},
42
35
}
43
36
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 {
49
38
_ , conf , err := o .ReadConfig (dir , filename )
50
39
if err != nil {
51
- return 1 , err
40
+ return err
52
41
}
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
55
45
for _ , sql := range conf .SQL {
56
46
sql := sql
57
47
if sql .Database != nil && sql .Database .Managed {
58
- pkg = & sql
59
- break
48
+ queryset = & sql
49
+ count += 1
60
50
}
61
51
}
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" )
64
57
}
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 " )
67
60
}
68
61
69
- var migrations []string
70
- files , err := sqlpath .Glob (pkg .Schema )
62
+ var ddl []string
63
+ files , err := sqlpath .Glob (queryset .Schema )
71
64
if err != nil {
72
- return 1 , err
65
+ return err
73
66
}
74
67
for _ , schema := range files {
75
68
contents , err := os .ReadFile (schema )
76
69
if err != nil {
77
- return 1 , fmt .Errorf ("read file: %w" , err )
70
+ return fmt .Errorf ("read file: %w" , err )
78
71
}
79
- migrations = append (migrations , string (contents ))
72
+ ddl = append (ddl , migrations . RemoveRollbackStatements ( string (contents ) ))
80
73
}
74
+
81
75
client , err := quickdb .NewClientFromConfig (conf .Cloud )
82
76
if err != nil {
83
- return 1 , fmt .Errorf ("client error: %w" , err )
77
+ return fmt .Errorf ("client error: %w" , err )
84
78
}
85
79
86
80
resp , err := client .CreateEphemeralDatabase (ctx , & pb.CreateEphemeralDatabaseRequest {
87
81
Engine : "postgresql" ,
88
82
Region : quickdb .GetClosestRegion (),
89
- Migrations : migrations ,
83
+ Migrations : ddl ,
90
84
})
91
85
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 )
111
87
}
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
122
90
}
0 commit comments