Skip to content

feat(createdb): Use locally running server for createdb command #3518

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 1 commit into from
Aug 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading