Skip to content

Commit 0bf8120

Browse files
authored
Polish (dbos-inc#137)
- Remove obsolete config property - Allow either DatabaseURL or SystemDBPool to be configured (tests in `dbos_test.go` already cover this) - add `--schema `to the CLI readme
1 parent 38a171b commit 0bf8120

File tree

5 files changed

+37
-24
lines changed

5 files changed

+37
-24
lines changed

cmd/dbos/README.md

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,19 @@ export DBOS_SYSTEM_DATABASE_URL=postgres://user:pass@localhost/mydb
3737
dbos migrate
3838
```
3939

40+
### Database Schema Configuration
41+
42+
By default, DBOS creates its system tables in the `dbos` schema. You can specify a custom schema name using the `--schema` global flag.
43+
44+
Example:
45+
```bash
46+
# Use a custom schema for all DBOS system tables
47+
dbos migrate --schema myapp_schema
48+
49+
# All workflow commands will use the specified schema
50+
dbos workflow list --schema myapp_schema
51+
```
52+
4053
### Configuration File
4154

4255
The CLI uses a `dbos-config.yaml` file for configuration. By default, it looks for this file in the current directory. You can specify a different config file using the `--config` flag.
@@ -60,6 +73,7 @@ These options are available for all commands:
6073
6174
- `-D, --db-url <url>` - Your DBOS system database URL
6275
- `--config <file>` - Config file (default: dbos-config.yaml)
76+
- `--schema <name>` - Database schema name (default: dbos)
6377
- `--verbose` - Enable verbose mode (DEBUG level logging)
6478

6579
## Commands
@@ -87,6 +101,8 @@ Create DBOS system tables in your database. This command runs the migration comm
87101
```bash
88102
dbos migrate
89103
dbos migrate --app-role myapp_role
104+
dbos migrate --schema custom_schema
105+
dbos migrate --schema custom_schema --app-role myapp_role
90106
```
91107

92108
The migrate commands are defined in `dbos-config.yaml`:

cmd/dbos/utils.go

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -71,14 +71,13 @@ func getDBURL(_ *cobra.Command) (string, error) {
7171
}
7272

7373
// createDBOSContext creates a new DBOS context with the provided database URL
74-
func createDBOSContext(userContext context.Context, dbURL string) (dbos.DBOSContext, error) {
74+
func createDBOSContext(ctx context.Context, dbURL string) (dbos.DBOSContext, error) {
7575
appName := "dbos-cli"
7676

7777
config := dbos.Config{
7878
DatabaseURL: dbURL,
7979
AppName: appName,
8080
Logger: initLogger(slog.LevelError),
81-
Context: userContext,
8281
}
8382

8483
// Use the global schema flag if it's set
@@ -87,11 +86,11 @@ func createDBOSContext(userContext context.Context, dbURL string) (dbos.DBOSCont
8786
logger.Debug("Using database schema", "schema", schema)
8887
}
8988

90-
ctx, err := dbos.NewDBOSContext(context.Background(), config)
89+
dbosCtx, err := dbos.NewDBOSContext(ctx, config)
9190
if err != nil {
9291
return nil, fmt.Errorf("failed to create DBOS context: %w", err)
9392
}
94-
return ctx, nil
93+
return dbosCtx, nil
9594
}
9695

9796
// outputJSON outputs data as JSON

dbos/client.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,10 @@ import (
1414
)
1515

1616
type ClientConfig struct {
17-
DatabaseURL string // Connection URL for the PostgreSQL database
17+
DatabaseURL string // DatabaseURL is a PostgreSQL connection string. Either this or SystemDBPool is required.
18+
SystemDBPool *pgxpool.Pool // SystemDBPool is a custom System Database Pool. It's optional and takes precedence over DatabaseURL if both are provided.
1819
DatabaseSchema string // Database schema name (defaults to "dbos")
1920
Logger *slog.Logger // Optional custom logger
20-
SystemDBPool *pgxpool.Pool // Optional existing connection pool for the system database
2121
}
2222

2323
// Client provides a programmatic way to interact with your DBOS application from external code.

dbos/dbos.go

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -30,24 +30,23 @@ const (
3030
// Config holds configuration parameters for initializing a DBOS context.
3131
// DatabaseURL and AppName are required.
3232
type Config struct {
33-
DatabaseURL string // PostgreSQL connection string (required)
34-
AppName string // Application name for identification (required)
35-
DatabaseSchema string // Database schema name (defaults to "dbos")
36-
Logger *slog.Logger // Custom logger instance (defaults to a new slog logger)
37-
AdminServer bool // Enable Transact admin HTTP server (disabled by default)
38-
AdminServerPort int // Port for the admin HTTP server (default: 3001)
39-
ConductorURL string // DBOS conductor service URL (optional)
40-
ConductorAPIKey string // DBOS conductor API key (optional)
41-
ApplicationVersion string // Application version (optional, overridden by DBOS__APPVERSION env var)
42-
ExecutorID string // Executor ID (optional, overridden by DBOS__VMID env var)
43-
Context context.Context // User Context
44-
SystemDBPool *pgxpool.Pool // Custom System Database Pool
33+
AppName string // Application name for identification (required)
34+
DatabaseURL string // DatabaseURL is a PostgreSQL connection string. Either this or SystemDBPool is required.
35+
SystemDBPool *pgxpool.Pool // SystemDBPool is a custom System Database Pool. It's optional and takes precedence over DatabaseURL if both are provided.
36+
DatabaseSchema string // Database schema name (defaults to "dbos")
37+
Logger *slog.Logger // Custom logger instance (defaults to a new slog logger)
38+
AdminServer bool // Enable Transact admin HTTP server (disabled by default)
39+
AdminServerPort int // Port for the admin HTTP server (default: 3001)
40+
ConductorURL string // DBOS conductor service URL (optional)
41+
ConductorAPIKey string // DBOS conductor API key (optional)
42+
ApplicationVersion string // Application version (optional, overridden by DBOS__APPVERSION env var)
43+
ExecutorID string // Executor ID (optional, overridden by DBOS__VMID env var)
4544
}
4645

4746
func processConfig(inputConfig *Config) (*Config, error) {
4847
// First check required fields
49-
if len(inputConfig.DatabaseURL) == 0 {
50-
return nil, fmt.Errorf("missing required config field: databaseURL")
48+
if len(inputConfig.DatabaseURL) == 0 && inputConfig.SystemDBPool == nil {
49+
return nil, fmt.Errorf("either databaseURL or systemDBPool must be provided")
5150
}
5251
if len(inputConfig.AppName) == 0 {
5352
return nil, fmt.Errorf("missing required config field: appName")

dbos/dbos_test.go

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ func TestConfig(t *testing.T) {
6262
assert.Equal(t, expectedMsg, dbosErr.Message)
6363
})
6464

65-
t.Run("FailsWithoutDatabaseURL", func(t *testing.T) {
65+
t.Run("FailsWithoutDatabaseURLOrSystemDBPool", func(t *testing.T) {
6666
config := Config{
6767
AppName: "test-app",
6868
}
@@ -75,7 +75,7 @@ func TestConfig(t *testing.T) {
7575

7676
assert.Equal(t, InitializationError, dbosErr.Code)
7777

78-
expectedMsg := "Error initializing DBOS Transact: missing required config field: databaseURL"
78+
expectedMsg := "Error initializing DBOS Transact: either databaseURL or systemDBPool must be provided"
7979
assert.Equal(t, expectedMsg, dbosErr.Message)
8080
})
8181

@@ -523,7 +523,6 @@ func TestCustomPool(t *testing.T) {
523523
require.NoError(t, err)
524524

525525
config := Config{
526-
DatabaseURL: databaseURL,
527526
AppName: "test-custom-pool",
528527
SystemDBPool: pool,
529528
}
@@ -609,7 +608,7 @@ func TestCustomPool(t *testing.T) {
609608
return input, nil
610609
}
611610

612-
t.Run("InvalidDatabaseUrl", func(t *testing.T) {
611+
t.Run("CustomPoolTakesPrecedence", func(t *testing.T) {
613612
invalidDatabaseURL := "postgres://invalid:invalid@localhost:5432/invaliddb"
614613
databaseURL := getDatabaseURL()
615614
poolConfig, err := pgxpool.ParseConfig(databaseURL)

0 commit comments

Comments
 (0)