Skip to content

Commit 54798b5

Browse files
committed
accept user provided db url
1 parent ee7eb93 commit 54798b5

File tree

2 files changed

+21
-12
lines changed

2 files changed

+21
-12
lines changed

dbos/dbos.go

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"encoding/hex"
77
"fmt"
88
"log/slog"
9+
"net/url"
910
"os"
1011
"reflect"
1112
"runtime"
@@ -92,6 +93,7 @@ func getLogger() *slog.Logger {
9293
type config struct {
9394
logger *slog.Logger
9495
adminServer bool
96+
databaseURL string
9597
}
9698

9799
type LaunchOption func(*config)
@@ -108,6 +110,12 @@ func WithAdminServer() LaunchOption {
108110
}
109111
}
110112

113+
func WithDatabaseURL(url string) LaunchOption {
114+
return func(config *config) {
115+
config.databaseURL = url
116+
}
117+
}
118+
111119
func Launch(options ...LaunchOption) error {
112120
if dbos != nil {
113121
fmt.Println("warning: DBOS instance already initialized, skipping re-initialization")
@@ -139,7 +147,18 @@ func Launch(options ...LaunchOption) error {
139147
APP_ID = os.Getenv("DBOS__APPID")
140148

141149
// Create the system database
142-
systemDB, err := NewSystemDatabase()
150+
var databaseURL string
151+
if config.databaseURL != "" {
152+
databaseURL = config.databaseURL
153+
} else {
154+
databaseURL = os.Getenv("DBOS_DATABASE_URL")
155+
if databaseURL == "" {
156+
fmt.Println("DBOS_DATABASE_URL not set, using default: postgres://postgres:${PGPASSWORD}@localhost:5432/dbos?sslmode=disable")
157+
password := url.QueryEscape(os.Getenv("PGPASSWORD"))
158+
databaseURL = fmt.Sprintf("postgres://postgres:%s@localhost:5432/dbos?sslmode=disable", password)
159+
}
160+
}
161+
systemDB, err := NewSystemDatabase(databaseURL)
143162
if err != nil {
144163
return NewInitializationError(fmt.Sprintf("failed to create system database: %v", err))
145164
}

dbos/system_database.go

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,6 @@ import (
55
"embed"
66
"errors"
77
"fmt"
8-
"net/url"
9-
"os"
108
"strings"
119
"sync"
1210
"time"
@@ -125,15 +123,7 @@ func runMigrations(databaseURL string) error {
125123
}
126124

127125
// New creates a new SystemDatabase instance and runs migrations
128-
func NewSystemDatabase() (SystemDatabase, error) {
129-
// TODO: pass proper config
130-
databaseURL := os.Getenv("DBOS_DATABASE_URL")
131-
if databaseURL == "" {
132-
fmt.Println("DBOS_DATABASE_URL not set, using default: postgres://postgres:${PGPASSWORD}@localhost:5432/dbos?sslmode=disable")
133-
password := url.QueryEscape(os.Getenv("PGPASSWORD"))
134-
databaseURL = fmt.Sprintf("postgres://postgres:%s@localhost:5432/dbos?sslmode=disable", password)
135-
}
136-
126+
func NewSystemDatabase(databaseURL string) (SystemDatabase, error) {
137127
// Create the database if it doesn't exist
138128
if err := createDatabaseIfNotExists(databaseURL); err != nil {
139129
return nil, NewInitializationError(fmt.Sprintf("failed to create database: %v", err))

0 commit comments

Comments
 (0)