Skip to content

Commit

Permalink
starting doltgres server creates db in defined empty dir
Browse files Browse the repository at this point in the history
  • Loading branch information
jennifersp committed Dec 27, 2023
1 parent d38f316 commit 561f19e
Show file tree
Hide file tree
Showing 5 changed files with 75 additions and 11 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 for creating a database.
name := dEnv.Config.GetStringOrDefault(config.UserNameKey, DefUserName)
email := dEnv.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
58 changes: 58 additions & 0 deletions testing/bats/doltgres.bats
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
#!/usr/bin/env bats
load $BATS_TEST_DIRNAME/setup/common.bash

setup() {
setup_common
}

teardown() {
teardown_common
}

@test 'doltgres: no args' {
start_sql_server_with_args "--host 0.0.0.0"
run query_server -c "\l"
[ "$status" -eq 0 ]
[[ "$output" =~ "information_schema" ]] || false
[[ "$output" =~ "doltgres" ]] || false
[[ "$output" =~ "postgres" ]] || false

[ ! -d "doltgres" ]
}

@test 'doltgres: with --data-dir' {
start_sql_server_with_args "--host 0.0.0.0 --data-dir=."
run query_server -c "\l"
[ "$status" -eq 0 ]
[[ "$output" =~ "information_schema" ]] || false
[[ "$output" =~ "doltgres" ]] || false
[[ "$output" =~ "postgres" ]] || false

[ -d "doltgres" ]
}

@test 'doltgres: with DOLTGRES_DATA_DIR' {
DOLTGRES_DATA_DIR="$BATS_TEST_DIRNAME/test"
start_sql_server_with_args "--host 0.0.0.0"
run query_server -c "\l"
[ "$status" -eq 0 ]
[[ "$output" =~ "information_schema" ]] || false
[[ "$output" =~ "doltgres" ]] || false
[[ "$output" =~ "postgres" ]] || false

[ -d "test/doltgres" ]
[ ! -d "doltgres" ]
}

@test 'doltgres: with both --data-dir and DOLTGRES_DATA_DIR' {
DOLTGRES_DATA_DIR="$BATS_TEST_DIRNAME/test1"
start_sql_server_with_args "--host 0.0.0.0 --data-dir=./test2"
run query_server -c "\l"
[ "$status" -eq 0 ]
[[ "$output" =~ "information_schema" ]] || false
[[ "$output" =~ "doltgres" ]] || false
[[ "$output" =~ "postgres" ]] || false

[ -d "test2/doltgres" ]
[ ! -d "test1/doltgres" ]
}
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
2 changes: 0 additions & 2 deletions testing/bats/setup/common.bash
Original file line number Diff line number Diff line change
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
2 changes: 1 addition & 1 deletion testing/bats/setup/query-server-common.bash
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ 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=""
Expand Down

0 comments on commit 561f19e

Please sign in to comment.