Skip to content

Commit

Permalink
WIP (#3518)
Browse files Browse the repository at this point in the history
  • Loading branch information
kyleconroy authored Aug 5, 2024
1 parent b78e322 commit fa2b963
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 12 deletions.
16 changes: 6 additions & 10 deletions internal/cmd/createdb.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand Down Expand Up @@ -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
}
19 changes: 17 additions & 2 deletions internal/dbmanager/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import (
type CreateDatabaseRequest struct {
Engine string
Migrations []string
Prefix string
}

type CreateDatabaseResponse struct {
Expand Down Expand Up @@ -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
}
Expand Down

0 comments on commit fa2b963

Please sign in to comment.