Skip to content

Commit 751613c

Browse files
committed
respect DOLTGRES_DB env var
1 parent d1dacfe commit 751613c

2 files changed

Lines changed: 39 additions & 27 deletions

File tree

server/auth/init.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -37,15 +37,15 @@ func Init(dEnv *env.DoltEnv) {
3737

3838
// GetSuperUserAndPassword returns the superuser and password for the server to use, as defined in the environment
3939
func GetSuperUserAndPassword() (string, string) {
40-
password := "password"
41-
if envPassword := os.Getenv(doltgresPasswordEnvVar); envPassword != "" {
42-
password = envPassword
43-
}
44-
4540
user := "postgres"
4641
if envUser := os.Getenv(doltgresUserEnvVar); envUser != "" {
4742
user = envUser
4843
}
4944

45+
password := "password"
46+
if envPassword := os.Getenv(doltgresPasswordEnvVar); envPassword != "" {
47+
password = envPassword
48+
}
49+
5050
return user, password
5151
}

server/server.go

Lines changed: 34 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import (
1818
"context"
1919
"fmt"
2020
_ "net/http/pprof"
21+
"os"
2122
"path/filepath"
2223

2324
"github.com/cockroachdb/errors"
@@ -49,9 +50,10 @@ import (
4950
const (
5051
Version = "0.54.4"
5152

52-
DefUserName = "postres"
53-
DefUserEmail = "postgres@somewhere.com"
54-
DoltgresDir = "postgres"
53+
DefUserName = "postres"
54+
DefUserEmail = "postgres@somewhere.com"
55+
DefUserEmailFmt = "%s@somewhere.com"
56+
DefaultDbNameEnvVar = "DOLTGRES_DB"
5557
)
5658

5759
func init() {
@@ -91,7 +93,7 @@ func runServer(ctx context.Context, cfg *servercfg.DoltgresConfig, dEnv *env.Dol
9193
if dEnv.HasDoltDataDir() {
9294
cwd, _ := dEnv.FS.Abs(".")
9395
return nil, errors.Errorf("Cannot start a server within a directory containing a Dolt or Doltgres database. "+
94-
"To use the current directory (%s) as a database, start the server from the parent directory.", cwd)
96+
"To use the current directory (%s) as a database, start the server from the parent directory.", cwd)
9597
}
9698

9799
defer tempfiles.MovableTempFileProvider.Clean()
@@ -121,9 +123,10 @@ func runServer(ctx context.Context, cfg *servercfg.DoltgresConfig, dEnv *env.Dol
121123
}
122124

123125
// We need a username and password for many SQL commands, so set defaults if they don't exist
126+
user, _ := auth.GetSuperUserAndPassword()
124127
dEnv.Config.SetFailsafes(map[string]string{
125-
config.UserNameKey: DefUserName,
126-
config.UserEmailKey: DefUserEmail,
128+
config.UserNameKey: user,
129+
config.UserEmailKey: fmt.Sprintf(DefUserEmailFmt, user),
127130
})
128131

129132
// Reload the dolt environment with the correct data dir that was specified in the configuration.
@@ -134,19 +137,16 @@ func runServer(ctx context.Context, cfg *servercfg.DoltgresConfig, dEnv *env.Dol
134137
}
135138
dEnv = env.Load(ctx, dEnv.GetUserHomeDir, dataDirFs, doltdb.LocalDirDoltDB, dEnv.Version)
136139

137-
// Automatically initialize a doltgres database if necessary
138-
// TODO: probably should only do this if there are no databases in the data dir already
139-
createDoltgresDatabase := false
140-
if exists, isDirectory := dataDirFs.Exists(DoltgresDir); !exists {
141-
createDoltgresDatabase = true
142-
} else if !isDirectory {
143-
workingDir, _ := dataDirFs.Abs(".")
144-
// The else branch means that there's a Doltgres item, so we need to error if it's a file since we
145-
// enforce the creation of a Doltgres database/directory, which would create a name conflict with the file
146-
return nil, errors.Errorf("Attempted to create the default `postgres` database at `%s`, but a file with "+
147-
"the same name was found. Either remove the file, change the directory using the `--data-dir` argument, "+
148-
"or change the environment variable `%s` so that it points to a different directory.", workingDir, servercfg.DOLTGRES_DATA_DIR)
140+
// Determine whether we need to initialize the default database
141+
initializeDefaultDatabase := true
142+
mrEnv, err := env.MultiEnvForDirectory(ctx, dEnv.Config.WriteableConfig(), dataDirFs, Version, dEnv)
143+
if err != nil {
144+
return nil, err
149145
}
146+
mrEnv.Iter(func(_ string, _ *env.DoltEnv) (stop bool, err error) {
147+
initializeDefaultDatabase = false
148+
return true, nil
149+
})
150150

151151
controller := svcs.NewController()
152152
newCtx, cancelF := context.WithCancel(ctx)
@@ -178,8 +178,8 @@ func runServer(ctx context.Context, cfg *servercfg.DoltgresConfig, dEnv *env.Dol
178178
return nil, err
179179
}
180180

181-
if createDoltgresDatabase {
182-
err = createDatabase(ssCfg, "postgres")
181+
if initializeDefaultDatabase {
182+
err = createDefaultDatabase(ssCfg)
183183
if err != nil {
184184
return nil, err
185185
}
@@ -194,10 +194,11 @@ func runServer(ctx context.Context, cfg *servercfg.DoltgresConfig, dEnv *env.Dol
194194
return controller, nil
195195
}
196196

197-
// createDatabase creates the database named on the local server using the configuration values to connect, returning
197+
// createDefaultDatabase creates the database named on the local server using the configuration values to connect, returning
198198
// any error
199-
func createDatabase(cfg doltservercfg.ServerConfig, dbName string) error {
199+
func createDefaultDatabase(cfg doltservercfg.ServerConfig) error {
200200
user, password := auth.GetSuperUserAndPassword()
201+
dbName := getDefaultDatabaseName(user)
201202

202203
dsn := fmt.Sprintf("postgres://%s:%s@localhost:%d", user, password, cfg.Port())
203204

@@ -213,6 +214,17 @@ func createDatabase(cfg doltservercfg.ServerConfig, dbName string) error {
213214
return err
214215
}
215216

217+
// getDefaultDatabaseName returns the name of the default database to create on first server start.
218+
// If the environment variable DOLTGRES_DB is set, that value is used. Otherwise, the username is used.
219+
// The username is in turn configured with the environment variable DOLTGRES_USER, defaulting to "postgres".
220+
func getDefaultDatabaseName(userName string) string {
221+
defaultDbName, defaultDbNameSet := os.LookupEnv(DefaultDbNameEnvVar)
222+
if defaultDbNameSet {
223+
return defaultDbName
224+
}
225+
return userName
226+
}
227+
216228
// startReplication begins the background thread that replicates from Postgres, if one is configured.
217229
func startReplication(cfg *servercfg.DoltgresConfig, ssCfg doltservercfg.ServerConfig) (*logrepl.LogicalReplicator, error) {
218230
if cfg.PostgresReplicationConfig == nil {

0 commit comments

Comments
 (0)