-
Notifications
You must be signed in to change notification settings - Fork 884
feat(createdb): Create ephemeral databases #2894
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
3fde947
feat(createdb): Create ephemeral databases
kyleconroy 2c11202
Just print to stdout
kyleconroy 162756b
Add names to querysets
kyleconroy aa6c366
Add a warning about the databse going away
kyleconroy ab4db66
Last changes
kyleconroy File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
package cmd | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"os" | ||
"runtime/trace" | ||
|
||
"github.com/spf13/cobra" | ||
"github.com/sqlc-dev/sqlc/internal/config" | ||
"github.com/sqlc-dev/sqlc/internal/migrations" | ||
"github.com/sqlc-dev/sqlc/internal/quickdb" | ||
pb "github.com/sqlc-dev/sqlc/internal/quickdb/v1" | ||
"github.com/sqlc-dev/sqlc/internal/sql/sqlpath" | ||
) | ||
|
||
var createDBCmd = &cobra.Command{ | ||
Use: "createdb", | ||
Short: "Create an ephemeral database", | ||
Args: cobra.NoArgs, | ||
RunE: func(cmd *cobra.Command, args []string) error { | ||
defer trace.StartRegion(cmd.Context(), "createdb").End() | ||
stderr := cmd.ErrOrStderr() | ||
dir, filename := getConfigPath(stderr, cmd.Flag("file")) | ||
querySetName, err := cmd.Flags().GetString("queryset") | ||
if err != nil { | ||
return err | ||
} | ||
err = CreateDB(cmd.Context(), dir, filename, querySetName, &Options{ | ||
Env: ParseEnv(cmd), | ||
Stderr: stderr, | ||
}) | ||
if err != nil { | ||
fmt.Fprintln(stderr, err.Error()) | ||
os.Exit(1) | ||
} | ||
return nil | ||
}, | ||
} | ||
|
||
func CreateDB(ctx context.Context, dir, filename, querySetName string, o *Options) error { | ||
_, conf, err := o.ReadConfig(dir, filename) | ||
if err != nil { | ||
return err | ||
} | ||
// Find the first queryset with a managed database | ||
var queryset *config.SQL | ||
var count int | ||
for _, sql := range conf.SQL { | ||
sql := sql | ||
if querySetName != "" && sql.Name != querySetName { | ||
continue | ||
} | ||
if sql.Database != nil && sql.Database.Managed { | ||
queryset = &sql | ||
count += 1 | ||
} | ||
} | ||
if queryset == nil && querySetName != "" { | ||
return fmt.Errorf("no queryset found with name %q", querySetName) | ||
} | ||
if queryset == nil { | ||
return fmt.Errorf("no querysets configured to use a managed database") | ||
} | ||
if count > 1 { | ||
return fmt.Errorf("multiple querysets configured to use managed databases") | ||
} | ||
if queryset.Engine != config.EnginePostgreSQL { | ||
return fmt.Errorf("managed databases currently only support PostgreSQL") | ||
} | ||
|
||
var ddl []string | ||
files, err := sqlpath.Glob(queryset.Schema) | ||
if err != nil { | ||
return err | ||
} | ||
for _, schema := range files { | ||
contents, err := os.ReadFile(schema) | ||
if err != nil { | ||
return fmt.Errorf("read file: %w", err) | ||
} | ||
ddl = append(ddl, migrations.RemoveRollbackStatements(string(contents))) | ||
} | ||
|
||
client, err := quickdb.NewClientFromConfig(conf.Cloud) | ||
if err != nil { | ||
return fmt.Errorf("client error: %w", err) | ||
} | ||
|
||
resp, err := client.CreateEphemeralDatabase(ctx, &pb.CreateEphemeralDatabaseRequest{ | ||
Engine: "postgresql", | ||
Region: quickdb.GetClosestRegion(), | ||
Migrations: ddl, | ||
}) | ||
if err != nil { | ||
return fmt.Errorf("managed: create database: %w", err) | ||
} | ||
fmt.Fprintln(os.Stderr, "WARNING: This database will be removed in two minutes") | ||
fmt.Println(resp.Uri) | ||
return nil | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.