Allow for unresolved targetEnvironment.

Not all commands, or even all SQL statements need databases.
This commit is contained in:
Neil Macneale IV
2023-05-16 14:15:30 -07:00
parent 2a7099144b
commit eaa4cddb2b
4 changed files with 31 additions and 29 deletions

View File

@@ -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,

View File

@@ -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

View File

@@ -1,3 +1,4 @@
#!/usr/bin/env bats
load $BATS_TEST_DIRNAME/helper/common.bash
setup() {

View File

@@ -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
}