Skip to content

Commit

Permalink
Merge pull request #6346 from dolthub/macneale4/empty-db-panic
Browse files Browse the repository at this point in the history
Allow empty databases in the shell prompt
  • Loading branch information
macneale4 authored Jul 19, 2023
2 parents 07a49cc + e94d0b2 commit f553866
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 6 deletions.
9 changes: 4 additions & 5 deletions go/cmd/dolt/commands/sql.go
Original file line number Diff line number Diff line change
Expand Up @@ -811,7 +811,6 @@ func execShell(sqlCtx *sql.Context, qryist cli.Queryist, format engine.PrintResu
// getDBFromSession returns the current database name for the session, handling all the errors along the way by printing
// red error messages to the CLI. If there was an issue getting the db name, the second return value is false.
func getDBFromSession(sqlCtx *sql.Context, qryist cli.Queryist) (db string, ok bool) {
db = "unknown"
_, resp, err := qryist.Query(sqlCtx, "select database()")
if err != nil {
cli.Println(color.RedString("Failure to get DB Name for session" + err.Error()))
Expand All @@ -827,11 +826,11 @@ func getDBFromSession(sqlCtx *sql.Context, qryist cli.Queryist) (db string, ok b
cli.Println(color.RedString("Failure to get DB Name for session" + err.Error()))
return db, false
}
if row[0] == nil || row[0].(string) == "" {
cli.Println(color.RedString("Empty Database name obtained" + err.Error()))
return db, false
if row[0] == nil {
db = ""
} else {
db = row[0].(string)
}
db = row[0].(string)
return db, true
}

Expand Down
32 changes: 32 additions & 0 deletions integration-tests/bats/sql-shell-empty-prompt.expect
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
#!/usr/bin/expect -f

set timeout 5
spawn dolt sql

expect {
"> " { send "select 23;\r"; }
timeout { exit 1; }
}

expect {
"| 23 |" { }
timeout { exit 1; }
}
expect {
"| 23 |" { }
timeout { exit 1; }
}

expect {
"> " { send "exit;\r"; }
timeout { exit 1; }
}

expect {
eof { }
timeout { exit 1; }
}

set waitval [wait -i $spawn_id]
set exval [lindex $waitval 3]
exit $exval
12 changes: 12 additions & 0 deletions integration-tests/bats/sql-shell.bats
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,18 @@ teardown() {
$BATS_TEST_DIRNAME/sql-works-after-failing-query.expect
}

@test "sql-shell: empty DB in prompt is OK" {
skiponwindows "Need to install expect and make this script work on windows."
if [ "$SQL_ENGINE" = "remote-engine" ]; then
skip "Presently sql command will not connect to remote server due to lack of lock file where there are not DBs."
fi
# ignore common setup. Use an empty db with no server.
rm -rf .dolt
mkdir emptyDb
cd emptyDb
$BATS_TEST_DIRNAME/sql-shell-empty-prompt.expect
}

@test "sql-shell: delimiter" {
skiponwindows "Need to install expect and make this script work on windows."
mkdir doltsql
Expand Down
3 changes: 2 additions & 1 deletion integration-tests/bats/sql-works-after-failing-query.expect
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#!/usr/bin/expect
#!/usr/bin/expect -f

set timeout 1
spawn dolt sql
Expand All @@ -15,3 +15,4 @@ expect {
"pid 0 is already in use" { exit 1 }
" 1 " { exit 0 }
}

0 comments on commit f553866

Please sign in to comment.