Skip to content

Commit 405d976

Browse files
committed
CLI configurable DB schema
1 parent c0c31d8 commit 405d976

File tree

3 files changed

+31
-17
lines changed

3 files changed

+31
-17
lines changed

cmd/dbos/migrate.go

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -41,9 +41,15 @@ func runMigrate(cmd *cobra.Command, args []string) error {
4141
return fmt.Errorf("failed to create DBOS context: %w", err)
4242
}
4343

44+
// Determine the schema to use (from flag or default)
45+
dbSchema := "dbos"
46+
if schema != "" {
47+
dbSchema = schema
48+
}
49+
4450
// Grant permissions to application role if specified
4551
if applicationRole != "" {
46-
if err := grantDBOSSchemaPermissions(dbURL, applicationRole); err != nil {
52+
if err := grantDBOSSchemaPermissions(dbURL, applicationRole, dbSchema); err != nil {
4753
return err
4854
}
4955
}
@@ -74,8 +80,8 @@ func runMigrate(cmd *cobra.Command, args []string) error {
7480
return nil
7581
}
7682

77-
func grantDBOSSchemaPermissions(databaseURL, roleName string) error {
78-
logger.Info("Granting permissions for DBOS schema", "role", roleName)
83+
func grantDBOSSchemaPermissions(databaseURL, roleName, schemaName string) error {
84+
logger.Info("Granting permissions for schema", "role", roleName, "schema", schemaName)
7985

8086
db, err := sql.Open("pgx", databaseURL)
8187
if err != nil {
@@ -86,15 +92,15 @@ func grantDBOSSchemaPermissions(databaseURL, roleName string) error {
8692
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
8793
defer cancel()
8894

89-
// Grant usage on the dbos schema
95+
// Grant usage on the specified schema
9096
queries := []string{
91-
fmt.Sprintf(`GRANT USAGE ON SCHEMA dbos TO "%s"`, roleName),
92-
fmt.Sprintf(`GRANT ALL PRIVILEGES ON ALL TABLES IN SCHEMA dbos TO "%s"`, roleName),
93-
fmt.Sprintf(`GRANT ALL PRIVILEGES ON ALL SEQUENCES IN SCHEMA dbos TO "%s"`, roleName),
94-
fmt.Sprintf(`GRANT EXECUTE ON ALL FUNCTIONS IN SCHEMA dbos TO "%s"`, roleName),
95-
fmt.Sprintf(`ALTER DEFAULT PRIVILEGES IN SCHEMA dbos GRANT ALL ON TABLES TO "%s"`, roleName),
96-
fmt.Sprintf(`ALTER DEFAULT PRIVILEGES IN SCHEMA dbos GRANT ALL ON SEQUENCES TO "%s"`, roleName),
97-
fmt.Sprintf(`ALTER DEFAULT PRIVILEGES IN SCHEMA dbos GRANT EXECUTE ON FUNCTIONS TO "%s"`, roleName),
97+
fmt.Sprintf(`GRANT USAGE ON SCHEMA %s TO "%s"`, schemaName, roleName),
98+
fmt.Sprintf(`GRANT ALL PRIVILEGES ON ALL TABLES IN SCHEMA %s TO "%s"`, schemaName, roleName),
99+
fmt.Sprintf(`GRANT ALL PRIVILEGES ON ALL SEQUENCES IN SCHEMA %s TO "%s"`, schemaName, roleName),
100+
fmt.Sprintf(`GRANT EXECUTE ON ALL FUNCTIONS IN SCHEMA %s TO "%s"`, schemaName, roleName),
101+
fmt.Sprintf(`ALTER DEFAULT PRIVILEGES IN SCHEMA %s GRANT ALL ON TABLES TO "%s"`, schemaName, roleName),
102+
fmt.Sprintf(`ALTER DEFAULT PRIVILEGES IN SCHEMA %s GRANT ALL ON SEQUENCES TO "%s"`, schemaName, roleName),
103+
fmt.Sprintf(`ALTER DEFAULT PRIVILEGES IN SCHEMA %s GRANT EXECUTE ON FUNCTIONS TO "%s"`, schemaName, roleName),
98104
}
99105

100106
for _, query := range queries {

cmd/dbos/root.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ var (
2020
dbURL string
2121
configFile string
2222
verbose bool
23+
schema string
2324

2425
// Global config
2526
config *Config
@@ -33,6 +34,7 @@ func init() {
3334
rootCmd.PersistentFlags().StringVarP(&dbURL, "db-url", "D", "", "Your DBOS system database URL")
3435
rootCmd.PersistentFlags().StringVar(&configFile, "config", "", "Config file (default is dbos-config.yaml)")
3536
rootCmd.PersistentFlags().BoolVar(&verbose, "verbose", false, "Enable verbose mode (DEBUG level logging)")
37+
rootCmd.PersistentFlags().StringVar(&schema, "schema", "", "Database schema name (defaults to \"dbos\")")
3638

3739
// Add all subcommands
3840
rootCmd.AddCommand(versionCmd)

cmd/dbos/utils.go

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -64,10 +64,8 @@ func getDBURL(_ *cobra.Command) (string, error) {
6464
}
6565

6666
// Log the database URL in verbose mode with masked password
67-
if verbose {
68-
maskedURL := maskPassword(resolvedURL)
69-
logger.Debug("Using database URL", "source", source, "url", maskedURL)
70-
}
67+
maskedURL := maskPassword(resolvedURL)
68+
logger.Debug("Using database URL", "source", source, "url", maskedURL)
7169

7270
return resolvedURL, nil
7371
}
@@ -76,12 +74,20 @@ func getDBURL(_ *cobra.Command) (string, error) {
7674
func createDBOSContext(userContext context.Context, dbURL string) (dbos.DBOSContext, error) {
7775
appName := "dbos-cli"
7876

79-
ctx, err := dbos.NewDBOSContext(context.Background(), dbos.Config{
77+
config := dbos.Config{
8078
DatabaseURL: dbURL,
8179
AppName: appName,
8280
Logger: initLogger(slog.LevelError),
8381
Context: userContext,
84-
})
82+
}
83+
84+
// Use the global schema flag if it's set
85+
if schema != "" {
86+
config.DatabaseSchema = schema
87+
logger.Debug("Using database schema", "schema", schema)
88+
}
89+
90+
ctx, err := dbos.NewDBOSContext(context.Background(), config)
8591
if err != nil {
8692
return nil, fmt.Errorf("failed to create DBOS context: %w", err)
8793
}

0 commit comments

Comments
 (0)