From eaa4cddb2beaa2e2a62cafbf234b8b6233d89f21 Mon Sep 17 00:00:00 2001 From: Neil Macneale IV Date: Tue, 16 May 2023 14:15:30 -0700 Subject: [PATCH] Allow for unresolved targetEnvironment. Not all commands, or even all SQL statements need databases. --- go/cmd/dolt/dolt.go | 37 +++++++++---------- go/libraries/doltcore/env/multi_repo_env.go | 20 ++++++---- .../bats/sql-create-database.bats | 1 + integration-tests/bats/sql.bats | 2 +- 4 files changed, 31 insertions(+), 29 deletions(-) diff --git a/go/cmd/dolt/dolt.go b/go/cmd/dolt/dolt.go index aab3290041..9383900a2c 100644 --- a/go/cmd/dolt/dolt.go +++ b/go/cmd/dolt/dolt.go @@ -518,31 +518,28 @@ func buildLateBinder(ctx context.Context, dEnv *env.DoltEnv, mrEnv *env.MultiRep useDb = mrEnv.GetFirstDatabase() } - if targetEnv == nil { + if targetEnv == nil && useDb != "" { targetEnv = mrEnv.GetEnv(useDb) - if targetEnv == nil { - return nil, fmt.Errorf("database %s doesn't exist.", useDb) + } + + // nil targetEnv will happen if the user ran a command in an empty directory - which we support in some cases. + if targetEnv != nil { + isLocked, lock, err := targetEnv.GetLock() + if err != nil { + return nil, err + } + if isLocked { + if verbose { + cli.Println("verbose: starting remote mode") + } + return sqlserver.BuildConnectionStringQueryist(ctx, dEnv, apr, lock.Port, useDb) } } - isLocked, lock, err := targetEnv.GetLock() - if err != nil { - return nil, err - } - if isLocked { - if verbose { - cli.Println("verbose: starting remote mode") - } - - mrEnv.GetFirstDatabase() - - return sqlserver.BuildConnectionStringQueryist(ctx, dEnv, apr, lock.Port, useDb) - } else { - if verbose { - cli.Println("verbose: starting local mode") - } - return commands.BuildSqlEngineQueryist(ctx, dEnv, mrEnv, apr) + if verbose { + cli.Println("verbose: starting local mode") } + return commands.BuildSqlEngineQueryist(ctx, dEnv, mrEnv, apr) } // splitArgsOnSubCommand splits the args into two slices, the first containing all args before the first subcommand, diff --git a/go/libraries/doltcore/env/multi_repo_env.go b/go/libraries/doltcore/env/multi_repo_env.go index 556cbc6bdc..0a9c41221f 100644 --- a/go/libraries/doltcore/env/multi_repo_env.go +++ b/go/libraries/doltcore/env/multi_repo_env.go @@ -69,35 +69,39 @@ func MultiEnvForSingleEnv(ctx context.Context, env *DoltEnv) (*MultiRepoEnv, err func MultiEnvForDirectory( ctx context.Context, config config.ReadWriteConfig, - fs filesys.Filesys, + fs filesys.Filesys, // fs which is the root of the multi-env. This is essentially the --data-dir flag or cwd. version string, ignoreLockFile bool, dEnv *DoltEnv, ) (*MultiRepoEnv, error) { // Load current fs and put into mr env - var dbName string - if _, ok := fs.(*filesys.InMemFS); ok { - dbName = "dolt" - } else { + var dbName string = "dolt" + var newDEnv *DoltEnv = dEnv + + // InMemFS is used only for testing. + // All other FS Types should get a newly created Envronment which will serve as the primary env in the MultiRepoEnv + if _, ok := fs.(*filesys.InMemFS); !ok { path, err := fs.Abs("") if err != nil { return nil, err } envName := getRepoRootDir(path, string(os.PathSeparator)) dbName = dirToDBName(envName) + + newDEnv = Load(ctx, GetCurrentUserHomeDir, fs, doltdb.LocalDirDoltDB, version) } mrEnv := &MultiRepoEnv{ envs: make([]NamedEnv, 0), fs: fs, cfg: config, - dialProvider: NewGRPCDialProviderFromDoltEnv(dEnv), + dialProvider: NewGRPCDialProviderFromDoltEnv(newDEnv), ignoreLockFile: ignoreLockFile, } envSet := map[string]*DoltEnv{} - if dEnv.Valid() { - envSet[dbName] = dEnv + if newDEnv.Valid() { + envSet[dbName] = newDEnv } // If there are other directories in the directory, try to load them as additional databases diff --git a/integration-tests/bats/sql-create-database.bats b/integration-tests/bats/sql-create-database.bats index 88434bd5e1..444e971d23 100644 --- a/integration-tests/bats/sql-create-database.bats +++ b/integration-tests/bats/sql-create-database.bats @@ -1,3 +1,4 @@ +#!/usr/bin/env bats load $BATS_TEST_DIRNAME/helper/common.bash setup() { diff --git a/integration-tests/bats/sql.bats b/integration-tests/bats/sql.bats index 598965a2b9..6f5c2763fb 100755 --- a/integration-tests/bats/sql.bats +++ b/integration-tests/bats/sql.bats @@ -2828,5 +2828,5 @@ SQL # Use of the use-db flag when we have a different DB specified by data-dir should error. run dolt --data-dir="$ROOT_DIR/dbb" --use-db=dba sql -q "show tables" [ "$status" -eq 1 ] - [[ "$output" =~ "database not found" ]] || false + [[ "$output" =~ "provided --use-db dba does not exist or is not a directory" ]] || false }