From fa2b9637f96a4cd3ccd26fdea6c41d3ca777c5b1 Mon Sep 17 00:00:00 2001 From: Kyle Gray Date: Sun, 4 Aug 2024 20:22:49 -0700 Subject: [PATCH] WIP (#3518) --- internal/cmd/createdb.go | 16 ++++++---------- internal/dbmanager/client.go | 19 +++++++++++++++++-- 2 files changed, 23 insertions(+), 12 deletions(-) diff --git a/internal/cmd/createdb.go b/internal/cmd/createdb.go index 9298dfb088..02b3031352 100644 --- a/internal/cmd/createdb.go +++ b/internal/cmd/createdb.go @@ -5,12 +5,12 @@ import ( "fmt" "os" "runtime/trace" + "time" "github.com/spf13/cobra" "github.com/sqlc-dev/sqlc/internal/config" + "github.com/sqlc-dev/sqlc/internal/dbmanager" "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" ) @@ -88,20 +88,16 @@ func CreateDB(ctx context.Context, dir, filename, querySetName string, o *Option 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{ + now := time.Now().UTC().UnixNano() + client := dbmanager.NewClient(conf.Servers) + resp, err := client.CreateDatabase(ctx, &dbmanager.CreateDatabaseRequest{ Engine: string(queryset.Engine), - Region: quickdb.GetClosestRegion(), Migrations: ddl, + Prefix: fmt.Sprintf("sqlc_createdb_%d", now), }) 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 } diff --git a/internal/dbmanager/client.go b/internal/dbmanager/client.go index 2c7d781081..37bb051c8d 100644 --- a/internal/dbmanager/client.go +++ b/internal/dbmanager/client.go @@ -19,6 +19,7 @@ import ( type CreateDatabaseRequest struct { Engine string Migrations []string + Prefix string } type CreateDatabaseResponse struct { @@ -48,11 +49,25 @@ func dbid(migrations []string) string { func (m *ManagedClient) CreateDatabase(ctx context.Context, req *CreateDatabaseRequest) (*CreateDatabaseResponse, error) { hash := dbid(req.Migrations) - name := fmt.Sprintf("sqlc_managed_%s", hash) + prefix := req.Prefix + if prefix == "" { + prefix = "sqlc_managed" + } + name := fmt.Sprintf("%s_%s", prefix, hash) + + engine := config.Engine(req.Engine) + switch engine { + case config.EngineMySQL: + // pass + case config.EnginePostgreSQL: + // pass + default: + return nil, fmt.Errorf("unsupported the %s engine", engine) + } var base string for _, server := range m.servers { - if server.Engine == config.EnginePostgreSQL { + if server.Engine == engine { base = server.URI break }