Skip to content

Commit 561f19e

Browse files
committed
starting doltgres server creates db in defined empty dir
1 parent d38f316 commit 561f19e

File tree

5 files changed

+75
-11
lines changed

5 files changed

+75
-11
lines changed

server/server.go

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,10 @@ const (
4343
DOLTGRES_DATA_DIR = "DOLTGRES_DATA_DIR"
4444
// DOLTGRES_DATA_DIR_DEFAULT is the portion to append to the user's home directory if DOLTGRES_DATA_DIR has not been specified
4545
DOLTGRES_DATA_DIR_DEFAULT = "doltgres/databases"
46+
47+
DefUserName = "postres"
48+
DefUserEmail = "[email protected]"
49+
DoltgresDir = "doltgres"
4650
)
4751

4852
var sqlServerDocs = cli.CommandDocumentationContent{
@@ -134,8 +138,8 @@ func RunInMemory(args []string) (*svcs.Controller, error) {
134138
globalConfig, _ := dEnv.Config.GetConfig(env.GlobalConfig)
135139
if globalConfig.GetStringOrDefault(config.UserNameKey, "") == "" {
136140
globalConfig.SetStrings(map[string]string{
137-
config.UserNameKey: "postgres",
138-
config.UserEmailKey: "[email protected]",
141+
config.UserNameKey: DefUserName,
142+
config.UserEmailKey: DefUserEmail,
139143
})
140144
}
141145

@@ -188,26 +192,29 @@ func runServer(ctx context.Context, args []string, dEnv *env.DoltEnv) (*svcs.Con
188192

189193
// We need a username and password for many SQL commands, so set defaults if they don't exist
190194
dEnv.Config.SetFailsafes(map[string]string{
191-
config.UserNameKey: "postgres",
192-
config.UserEmailKey: "[email protected]",
195+
config.UserNameKey: DefUserName,
196+
config.UserEmailKey: DefUserEmail,
193197
})
194198

195199
// Automatically initialize a doltgres database if necessary
196200
if !dEnv.HasDoltDir() {
197201
// Need to make sure that there isn't a doltgres item in the path.
198-
if exists, isDirectory := dEnv.FS.Exists("doltgres"); !exists {
199-
err := dEnv.FS.MkDirs("doltgres")
202+
if exists, isDirectory := dEnv.FS.Exists(DoltgresDir); !exists {
203+
err := dEnv.FS.MkDirs(DoltgresDir)
200204
if err != nil {
201205
return nil, err
202206
}
203-
subdirectoryFS, err := dEnv.FS.WithWorkingDir("doltgres")
207+
subdirectoryFS, err := dEnv.FS.WithWorkingDir(DoltgresDir)
204208
if err != nil {
205209
return nil, err
206210
}
207211

208212
// We'll use a temporary environment to instantiate the subdirectory
209213
tempDEnv := env.Load(ctx, env.GetCurrentUserHomeDir, subdirectoryFS, dEnv.UrlStr(), Version)
210-
res := commands.InitCmd{}.Exec(ctx, "init", []string{}, tempDEnv, configCliContext{tempDEnv})
214+
// username and user email is needed for creating a database.
215+
name := dEnv.Config.GetStringOrDefault(config.UserNameKey, DefUserName)
216+
email := dEnv.Config.GetStringOrDefault(config.UserEmailKey, DefUserEmail)
217+
res := commands.InitCmd{}.Exec(ctx, "init", []string{"--name", name, "--email", email}, tempDEnv, configCliContext{tempDEnv})
211218
if res != 0 {
212219
return nil, fmt.Errorf("failed to initialize doltgres database")
213220
}

testing/bats/doltgres.bats

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
#!/usr/bin/env bats
2+
load $BATS_TEST_DIRNAME/setup/common.bash
3+
4+
setup() {
5+
setup_common
6+
}
7+
8+
teardown() {
9+
teardown_common
10+
}
11+
12+
@test 'doltgres: no args' {
13+
start_sql_server_with_args "--host 0.0.0.0"
14+
run query_server -c "\l"
15+
[ "$status" -eq 0 ]
16+
[[ "$output" =~ "information_schema" ]] || false
17+
[[ "$output" =~ "doltgres" ]] || false
18+
[[ "$output" =~ "postgres" ]] || false
19+
20+
[ ! -d "doltgres" ]
21+
}
22+
23+
@test 'doltgres: with --data-dir' {
24+
start_sql_server_with_args "--host 0.0.0.0 --data-dir=."
25+
run query_server -c "\l"
26+
[ "$status" -eq 0 ]
27+
[[ "$output" =~ "information_schema" ]] || false
28+
[[ "$output" =~ "doltgres" ]] || false
29+
[[ "$output" =~ "postgres" ]] || false
30+
31+
[ -d "doltgres" ]
32+
}
33+
34+
@test 'doltgres: with DOLTGRES_DATA_DIR' {
35+
DOLTGRES_DATA_DIR="$BATS_TEST_DIRNAME/test"
36+
start_sql_server_with_args "--host 0.0.0.0"
37+
run query_server -c "\l"
38+
[ "$status" -eq 0 ]
39+
[[ "$output" =~ "information_schema" ]] || false
40+
[[ "$output" =~ "doltgres" ]] || false
41+
[[ "$output" =~ "postgres" ]] || false
42+
43+
[ -d "test/doltgres" ]
44+
[ ! -d "doltgres" ]
45+
}
46+
47+
@test 'doltgres: with both --data-dir and DOLTGRES_DATA_DIR' {
48+
DOLTGRES_DATA_DIR="$BATS_TEST_DIRNAME/test1"
49+
start_sql_server_with_args "--host 0.0.0.0 --data-dir=./test2"
50+
run query_server -c "\l"
51+
[ "$status" -eq 0 ]
52+
[[ "$output" =~ "information_schema" ]] || false
53+
[[ "$output" =~ "doltgres" ]] || false
54+
[[ "$output" =~ "postgres" ]] || false
55+
56+
[ -d "test2/doltgres" ]
57+
[ ! -d "test1/doltgres" ]
58+
}

testing/bats/psql-commands.bats

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ load $BATS_TEST_DIRNAME/setup/common.bash
33

44
setup() {
55
setup_common
6+
start_sql_server
67
query_server <<SQL
78
CREATE TABLE test1 (pk BIGINT PRIMARY KEY, v1 SMALLINT);
89
CREATE TABLE test2 (pk BIGINT PRIMARY KEY, v1 INTEGER, v2 SMALLINT);

testing/bats/setup/common.bash

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,8 +66,6 @@ setup_common() {
6666
if [ -z "$DOLT_TEST_RETRIES" ]; then
6767
export BATS_TEST_RETRIES="$DOLT_TEST_RETRIES"
6868
fi
69-
70-
start_sql_server
7169
}
7270

7371
teardown_common() {

testing/bats/setup/query-server-common.bash

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ start_sql_server() {
4343
}
4444

4545
# like start_sql_server, but the second argument is a string with all
46-
# arguments to dolt-sql-server (excluding --port, which is defined in
46+
# arguments to doltgres (excluding --port, which is defined in
4747
# this func)
4848
start_sql_server_with_args() {
4949
DEFAULT_DB=""

0 commit comments

Comments
 (0)