Skip to content

Commit

Permalink
starting doltgres server creates db in defined data directory (#83)
Browse files Browse the repository at this point in the history
  • Loading branch information
jennifersp authored Dec 28, 2023
1 parent d38f316 commit a444d36
Show file tree
Hide file tree
Showing 5 changed files with 68 additions and 14 deletions.
23 changes: 15 additions & 8 deletions server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,10 @@ const (
DOLTGRES_DATA_DIR = "DOLTGRES_DATA_DIR"
// DOLTGRES_DATA_DIR_DEFAULT is the portion to append to the user's home directory if DOLTGRES_DATA_DIR has not been specified
DOLTGRES_DATA_DIR_DEFAULT = "doltgres/databases"

DefUserName = "postres"
DefUserEmail = "[email protected]"
DoltgresDir = "doltgres"
)

var sqlServerDocs = cli.CommandDocumentationContent{
Expand Down Expand Up @@ -134,8 +138,8 @@ func RunInMemory(args []string) (*svcs.Controller, error) {
globalConfig, _ := dEnv.Config.GetConfig(env.GlobalConfig)
if globalConfig.GetStringOrDefault(config.UserNameKey, "") == "" {
globalConfig.SetStrings(map[string]string{
config.UserNameKey: "postgres",
config.UserEmailKey: "[email protected]",
config.UserNameKey: DefUserName,
config.UserEmailKey: DefUserEmail,
})
}

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

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

// Automatically initialize a doltgres database if necessary
if !dEnv.HasDoltDir() {
// Need to make sure that there isn't a doltgres item in the path.
if exists, isDirectory := dEnv.FS.Exists("doltgres"); !exists {
err := dEnv.FS.MkDirs("doltgres")
if exists, isDirectory := dEnv.FS.Exists(DoltgresDir); !exists {
err := dEnv.FS.MkDirs(DoltgresDir)
if err != nil {
return nil, err
}
subdirectoryFS, err := dEnv.FS.WithWorkingDir("doltgres")
subdirectoryFS, err := dEnv.FS.WithWorkingDir(DoltgresDir)
if err != nil {
return nil, err
}

// We'll use a temporary environment to instantiate the subdirectory
tempDEnv := env.Load(ctx, env.GetCurrentUserHomeDir, subdirectoryFS, dEnv.UrlStr(), Version)
res := commands.InitCmd{}.Exec(ctx, "init", []string{}, tempDEnv, configCliContext{tempDEnv})
// username and user email is needed to create a new database.
name := tempDEnv.Config.GetStringOrDefault(config.UserNameKey, DefUserName)
email := tempDEnv.Config.GetStringOrDefault(config.UserEmailKey, DefUserEmail)
res := commands.InitCmd{}.Exec(ctx, "init", []string{"--name", name, "--email", email}, tempDEnv, configCliContext{tempDEnv})
if res != 0 {
return nil, fmt.Errorf("failed to initialize doltgres database")
}
Expand Down
49 changes: 49 additions & 0 deletions testing/bats/doltgres.bats
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
#!/usr/bin/env bats
load $BATS_TEST_DIRNAME/setup/common.bash

setup() {
setup_common
# tests are run without setting doltgres config user.name and user.email
stash_current_dolt_user
unset_dolt_user
}

teardown() {
restore_stashed_dolt_user
teardown_common
}

@test 'doltgres: DOLTGRES_DATA_DIR set to current dir' {
[ ! -d "doltgres" ]
export DOLTGRES_DATA_DIR="$(pwd)"
export SQL_USER="doltgres"
start_sql_server_with_args "--host 0.0.0.0" "--user doltgres" > log.txt 2>&1

run cat log.txt
[[ ! "$output" =~ "Author identity unknown" ]] || false
[ -d "doltgres" ]

run query_server -c "\l"
[ "$status" -eq 0 ]
[[ "$output" =~ "information_schema" ]] || false
[[ "$output" =~ "doltgres" ]] || false
[[ "$output" =~ "postgres" ]] || false
}

@test 'doltgres: setting both --data-dir and DOLTGRES_DATA_DIR should use --data-dir value' {
[ ! -d "doltgres" ]

export DOLTGRES_DATA_DIR="$(pwd)"
export SQL_USER="doltgres"
start_sql_server_with_args "--host 0.0.0.0" "--user doltgres" "--data-dir=./test" > log.txt 2>&1

run cat log.txt
[[ ! "$output" =~ "Author identity unknown" ]] || false
[ ! -d "doltgres" ]
[ -d "test/doltgres" ]

run query_server -c "\l"
[ "$status" -eq 0 ]
[[ "$output" =~ "information_schema" ]] || false
[[ "$output" =~ "doltgres" ]] || false
}
1 change: 1 addition & 0 deletions testing/bats/psql-commands.bats
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ load $BATS_TEST_DIRNAME/setup/common.bash

setup() {
setup_common
start_sql_server
query_server <<SQL
CREATE TABLE test1 (pk BIGINT PRIMARY KEY, v1 SMALLINT);
CREATE TABLE test2 (pk BIGINT PRIMARY KEY, v1 INTEGER, v2 SMALLINT);
Expand Down
6 changes: 2 additions & 4 deletions testing/bats/setup/common.bash
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@ set_dolt_user() {
}

unset_dolt_user() {
doltgresql config --global --unset user.name
doltgresql config --global --unset user.email
doltgresql config --global --unset user.name
doltgresql config --global --unset user.email
}

current_dolt_user_name() {
Expand Down Expand Up @@ -66,8 +66,6 @@ setup_common() {
if [ -z "$DOLT_TEST_RETRIES" ]; then
export BATS_TEST_RETRIES="$DOLT_TEST_RETRIES"
fi

start_sql_server
}

teardown_common() {
Expand Down
3 changes: 1 addition & 2 deletions testing/bats/setup/query-server-common.bash
Original file line number Diff line number Diff line change
Expand Up @@ -43,12 +43,11 @@ start_sql_server() {
}

# like start_sql_server, but the second argument is a string with all
# arguments to dolt-sql-server (excluding --port, which is defined in
# arguments to doltgres (excluding --port, which is defined in
# this func)
start_sql_server_with_args() {
DEFAULT_DB=""
nativevar DEFAULT_DB "$DEFAULT_DB" /w
nativevar DOLTGRES_DATA_DIR "$(pwd)" /p
PORT=$( definePORT )
doltgresql "$@" --port=$PORT &
SERVER_PID=$!
Expand Down

0 comments on commit a444d36

Please sign in to comment.