Skip to content

Commit fa2b963

Browse files
authored
WIP (#3518)
1 parent b78e322 commit fa2b963

File tree

2 files changed

+23
-12
lines changed

2 files changed

+23
-12
lines changed

internal/cmd/createdb.go

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,12 @@ import (
55
"fmt"
66
"os"
77
"runtime/trace"
8+
"time"
89

910
"github.com/spf13/cobra"
1011
"github.com/sqlc-dev/sqlc/internal/config"
12+
"github.com/sqlc-dev/sqlc/internal/dbmanager"
1113
"github.com/sqlc-dev/sqlc/internal/migrations"
12-
"github.com/sqlc-dev/sqlc/internal/quickdb"
13-
pb "github.com/sqlc-dev/sqlc/internal/quickdb/v1"
1414
"github.com/sqlc-dev/sqlc/internal/sql/sqlpath"
1515
)
1616

@@ -88,20 +88,16 @@ func CreateDB(ctx context.Context, dir, filename, querySetName string, o *Option
8888
ddl = append(ddl, migrations.RemoveRollbackStatements(string(contents)))
8989
}
9090

91-
client, err := quickdb.NewClientFromConfig(conf.Cloud)
92-
if err != nil {
93-
return fmt.Errorf("client error: %w", err)
94-
}
95-
96-
resp, err := client.CreateEphemeralDatabase(ctx, &pb.CreateEphemeralDatabaseRequest{
91+
now := time.Now().UTC().UnixNano()
92+
client := dbmanager.NewClient(conf.Servers)
93+
resp, err := client.CreateDatabase(ctx, &dbmanager.CreateDatabaseRequest{
9794
Engine: string(queryset.Engine),
98-
Region: quickdb.GetClosestRegion(),
9995
Migrations: ddl,
96+
Prefix: fmt.Sprintf("sqlc_createdb_%d", now),
10097
})
10198
if err != nil {
10299
return fmt.Errorf("managed: create database: %w", err)
103100
}
104-
fmt.Fprintln(os.Stderr, "WARNING: This database will be removed in two minutes")
105101
fmt.Println(resp.Uri)
106102
return nil
107103
}

internal/dbmanager/client.go

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import (
1919
type CreateDatabaseRequest struct {
2020
Engine string
2121
Migrations []string
22+
Prefix string
2223
}
2324

2425
type CreateDatabaseResponse struct {
@@ -48,11 +49,25 @@ func dbid(migrations []string) string {
4849

4950
func (m *ManagedClient) CreateDatabase(ctx context.Context, req *CreateDatabaseRequest) (*CreateDatabaseResponse, error) {
5051
hash := dbid(req.Migrations)
51-
name := fmt.Sprintf("sqlc_managed_%s", hash)
52+
prefix := req.Prefix
53+
if prefix == "" {
54+
prefix = "sqlc_managed"
55+
}
56+
name := fmt.Sprintf("%s_%s", prefix, hash)
57+
58+
engine := config.Engine(req.Engine)
59+
switch engine {
60+
case config.EngineMySQL:
61+
// pass
62+
case config.EnginePostgreSQL:
63+
// pass
64+
default:
65+
return nil, fmt.Errorf("unsupported the %s engine", engine)
66+
}
5267

5368
var base string
5469
for _, server := range m.servers {
55-
if server.Engine == config.EnginePostgreSQL {
70+
if server.Engine == engine {
5671
base = server.URI
5772
break
5873
}

0 commit comments

Comments
 (0)