From 561f19e3041a4635df08b49bfb583a3dab34be1e Mon Sep 17 00:00:00 2001 From: jennifersp Date: Wed, 27 Dec 2023 15:18:30 -0800 Subject: [PATCH] starting doltgres server creates db in defined empty dir --- server/server.go | 23 +++++--- testing/bats/doltgres.bats | 58 +++++++++++++++++++++ testing/bats/psql-commands.bats | 1 + testing/bats/setup/common.bash | 2 - testing/bats/setup/query-server-common.bash | 2 +- 5 files changed, 75 insertions(+), 11 deletions(-) create mode 100644 testing/bats/doltgres.bats diff --git a/server/server.go b/server/server.go index d2fa42966a..115e88192f 100644 --- a/server/server.go +++ b/server/server.go @@ -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 = "postgres@somewhere.com" + DoltgresDir = "doltgres" ) var sqlServerDocs = cli.CommandDocumentationContent{ @@ -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: "postgres@somewhere.com", + config.UserNameKey: DefUserName, + config.UserEmailKey: DefUserEmail, }) } @@ -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: "postgres@somewhere.com", + 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") } diff --git a/testing/bats/doltgres.bats b/testing/bats/doltgres.bats new file mode 100644 index 0000000000..0dfa9352bd --- /dev/null +++ b/testing/bats/doltgres.bats @@ -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" ] +} diff --git a/testing/bats/psql-commands.bats b/testing/bats/psql-commands.bats index 08574adf1d..516036c9cf 100644 --- a/testing/bats/psql-commands.bats +++ b/testing/bats/psql-commands.bats @@ -3,6 +3,7 @@ load $BATS_TEST_DIRNAME/setup/common.bash setup() { setup_common + start_sql_server query_server <