Skip to content

Commit 17597b1

Browse files
committed
handle k/v urls format in the CLI
1 parent 383211b commit 17597b1

File tree

1 file changed

+24
-23
lines changed

1 file changed

+24
-23
lines changed

cmd/dbos/reset.go

Lines changed: 24 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
package main
22

33
import (
4-
"database/sql"
4+
"context"
55
"fmt"
6-
"net/url"
76

8-
_ "github.com/jackc/pgx/v5/stdlib"
7+
"github.com/jackc/pgx/v5"
8+
"github.com/jackc/pgx/v5/pgxpool"
99
"github.com/spf13/cobra"
1010
)
1111

@@ -39,42 +39,43 @@ func runReset(cmd *cobra.Command, args []string) error {
3939
return err
4040
}
4141

42-
// Parse the URL to get database name
43-
parsedURL, err := url.Parse(dbURL)
44-
if err != nil {
45-
return fmt.Errorf("invalid database URL: %w", err)
46-
}
42+
ctx := context.Background()
4743

48-
// Extract database name from path
49-
dbName := parsedURL.Path
50-
if len(dbName) > 0 && dbName[0] == '/' {
51-
dbName = dbName[1:] // Remove leading slash
44+
// Parse the connection string using pgxpool.ParseConfig which handles both URL and key-value formats
45+
config, err := pgxpool.ParseConfig(dbURL)
46+
if err != nil {
47+
return fmt.Errorf("failed to parse database URL: %w", err)
5248
}
5349

50+
// Get the database name from the config
51+
dbName := config.ConnConfig.Database
5452
if dbName == "" {
55-
return fmt.Errorf("database name is required in URL")
53+
return fmt.Errorf("database name not found in connection string")
5654
}
5755

58-
// Connect to postgres database to drop and recreate the system database
59-
parsedURL.Path = "/postgres"
60-
postgresURL := parsedURL.String()
56+
// Create a connection configuration pointing to the postgres database
57+
postgresConfig := config.ConnConfig.Copy()
58+
postgresConfig.Database = "postgres"
6159

62-
db, err := sql.Open("pgx", postgresURL)
60+
// Connect to the postgres database
61+
conn, err := pgx.ConnectConfig(ctx, postgresConfig)
6362
if err != nil {
64-
return fmt.Errorf("failed to connect to postgres database: %w", err)
63+
return fmt.Errorf("failed to connect to PostgreSQL server: %w", err)
6564
}
66-
defer db.Close()
65+
defer conn.Close(ctx)
6766

6867
// Drop the system database if it exists
6968
logger.Info("Resetting system database", "database", dbName)
70-
dropQuery := fmt.Sprintf("DROP DATABASE IF EXISTS %s WITH (FORCE)", dbName)
71-
if _, err := db.Exec(dropQuery); err != nil {
69+
dropSQL := fmt.Sprintf("DROP DATABASE IF EXISTS %s WITH (FORCE)", pgx.Identifier{dbName}.Sanitize())
70+
_, err = conn.Exec(ctx, dropSQL)
71+
if err != nil {
7272
return fmt.Errorf("failed to drop system database: %w", err)
7373
}
7474

7575
// Create the database
76-
createQuery := fmt.Sprintf("CREATE DATABASE %s", dbName)
77-
if _, err := db.Exec(createQuery); err != nil {
76+
createSQL := fmt.Sprintf("CREATE DATABASE %s", pgx.Identifier{dbName}.Sanitize())
77+
_, err = conn.Exec(ctx, createSQL)
78+
if err != nil {
7879
return fmt.Errorf("failed to create system database: %w", err)
7980
}
8081

0 commit comments

Comments
 (0)