Skip to content

Commit

Permalink
Vinai/more dbeaver stuff (dolthub#1455)
Browse files Browse the repository at this point in the history
This pr fixes a bug where cross db queries were not committing/allowed when the current database is not set. This allows for basic compatibility with dbeaver.
Vinai Rachakonda authored Mar 19, 2021
1 parent 265275a commit de8f4d9
Showing 5 changed files with 88 additions and 12 deletions.
4 changes: 2 additions & 2 deletions go/go.mod
Original file line number Diff line number Diff line change
@@ -18,11 +18,11 @@ require (
github.com/denisbrodbeck/machineid v1.0.1
github.com/dolthub/dolt/go/gen/proto/dolt/services/eventsapi v0.0.0-20201005193433-3ee972b1d078
github.com/dolthub/fslock v0.0.2
github.com/dolthub/go-mysql-server v0.8.1-0.20210318235349-0703523a75c1
github.com/dolthub/go-mysql-server v0.8.1-0.20210319025306-7fcc1fc72450
github.com/dolthub/ishell v0.0.0-20210205014355-16a4ce758446
github.com/dolthub/mmap-go v1.0.4-0.20201107010347-f9f2a9588a66
github.com/dolthub/sqllogictest/go v0.0.0-20201105013724-5123fc66e12c
github.com/dolthub/vitess v0.0.0-20210317175908-b2663b2c4d9c
github.com/dolthub/vitess v0.0.0-20210317181852-72b3882fdff9
github.com/dustin/go-humanize v1.0.0
github.com/fatih/color v1.9.0
github.com/flynn-archive/go-shlex v0.0.0-20150515145356-3f9db97f8568
7 changes: 4 additions & 3 deletions go/go.sum
Original file line number Diff line number Diff line change
@@ -148,16 +148,17 @@ github.com/dgrijalva/jwt-go v3.2.0+incompatible/go.mod h1:E3ru+11k8xSBh+hMPgOLZm
github.com/dgryski/go-sip13 v0.0.0-20181026042036-e10d5fee7954/go.mod h1:vAd38F8PWV+bWy6jNmig1y/TA+kYO4g3RSRF0IAv0no=
github.com/dolthub/fslock v0.0.2 h1:8vUh47iKovgrtXNrXVIzsIoWLlspoXg+3nslhUzgKSw=
github.com/dolthub/fslock v0.0.2/go.mod h1:0i7bsNkK+XHwFL3dIsSWeXSV7sykVzzVr6+jq8oeEo0=
github.com/dolthub/go-mysql-server v0.8.1-0.20210318235349-0703523a75c1 h1:AxhoATT5C8w9MHJX4nG1GtyyjAGblx8Xvpp7r7GESxw=
github.com/dolthub/go-mysql-server v0.8.1-0.20210318235349-0703523a75c1/go.mod h1:hOD66EJ7OUqJHEkbiAtv5UNQIicu6h6T6n9jMcEmTyI=
github.com/dolthub/go-mysql-server v0.8.1-0.20210319025306-7fcc1fc72450 h1:N32fDj4eLZ+KKEIF2NtWd/t/qt+YWg8Wkk0XXezOqdo=
github.com/dolthub/go-mysql-server v0.8.1-0.20210319025306-7fcc1fc72450/go.mod h1:hOD66EJ7OUqJHEkbiAtv5UNQIicu6h6T6n9jMcEmTyI=
github.com/dolthub/ishell v0.0.0-20210205014355-16a4ce758446 h1:0ol5pj+QlKUKAtqs1LiPM3ZJKs+rHPgLSsMXmhTrCAM=
github.com/dolthub/ishell v0.0.0-20210205014355-16a4ce758446/go.mod h1:dhGBqcCEfK5kuFmeO5+WOx3hqc1k3M29c1oS/R7N4ms=
github.com/dolthub/mmap-go v1.0.4-0.20201107010347-f9f2a9588a66 h1:WRPDbpJWEnPxPmiuOTndT+lUWUeGjx6eoNOK9O4tQQQ=
github.com/dolthub/mmap-go v1.0.4-0.20201107010347-f9f2a9588a66/go.mod h1:N5ZIbMGuDUpTpOFQ7HcsN6WSIpTGQjHP+Mz27AfmAgk=
github.com/dolthub/sqllogictest/go v0.0.0-20201105013724-5123fc66e12c h1:ZIo6IOXU3/rJK4lp83QRq1zGhQrjQQtlmE2b7H1Vv/k=
github.com/dolthub/sqllogictest/go v0.0.0-20201105013724-5123fc66e12c/go.mod h1:siLfyv2c92W1eN/R4QqG/+RjjX5W2+gCTRjZxBjI3TY=
github.com/dolthub/vitess v0.0.0-20210317175908-b2663b2c4d9c h1:u/hKK5EBh+FJlZlcMjSYLB9qXTG+pnyaa6Vww8sSHvY=
github.com/dolthub/vitess v0.0.0-20210317175908-b2663b2c4d9c/go.mod h1:hUE8oSk2H5JZnvtlLBhJPYC8WZCA5AoSntdLTcBvdBM=
github.com/dolthub/vitess v0.0.0-20210317181852-72b3882fdff9 h1:4TNap9ZZ4UOd6F46tstE4jOxDBifYNWuTLiWqIEr/eI=
github.com/dolthub/vitess v0.0.0-20210317181852-72b3882fdff9/go.mod h1:hUE8oSk2H5JZnvtlLBhJPYC8WZCA5AoSntdLTcBvdBM=
github.com/dustin/go-humanize v0.0.0-20171111073723-bb3d318650d4/go.mod h1:HtrtbFcZ19U5GC7JDqmcUSB87Iq5E25KnS6fMYU6eOk=
github.com/dustin/go-humanize v1.0.0 h1:VSnTsYCnlFHaM2/igO1h6X3HA71jcobQuxemgkq4zYo=
github.com/dustin/go-humanize v1.0.0/go.mod h1:HtrtbFcZ19U5GC7JDqmcUSB87Iq5E25KnS6fMYU6eOk=
13 changes: 7 additions & 6 deletions go/libraries/doltcore/sqle/dolt_session.go
Original file line number Diff line number Diff line change
@@ -115,20 +115,21 @@ func TableCacheFromSess(sess sql.Session, dbName string) TableCache {
return sess.(*DoltSession).caches[dbName]
}

func (sess *DoltSession) CommitTransaction(ctx *sql.Context) error {
currentDb := sess.GetCurrentDatabase()
if currentDb == "" {
return sql.ErrNoDatabaseSelected.New()
func (sess *DoltSession) CommitTransaction(ctx *sql.Context, dbName string) error {
// This is triggered when certain commands are sent to the server (ex. commit) when a database is not selected.
// These commands should not error.
if dbName == "" {
return nil
}

dbRoot, ok := sess.dbRoots[currentDb]
dbRoot, ok := sess.dbRoots[dbName]
// It's possible that this returns false if the user has created an in-Memory database. Moreover,
// the analyzer will check for us whether a db exists or not.
if !ok {
return nil
}

dbData := sess.dbDatas[currentDb]
dbData := sess.dbDatas[dbName]

root := dbRoot.root
h, err := dbData.Ddb.WriteRootValue(ctx, root)
32 changes: 31 additions & 1 deletion integration-tests/bats/helper/query-server-common.bash
Original file line number Diff line number Diff line change
@@ -29,7 +29,11 @@ else:
from pytest import DoltConnection, csv_to_row_maps
dc = DoltConnection(port=int(port_str), database=database, user='dolt', auto_commit=auto_commit)
if not database:
dc = DoltConnection(port=int(port_str), database=None, user='dolt', auto_commit=auto_commit)
else:
dc = DoltConnection(port=int(port_str), database=database, user='dolt', auto_commit=auto_commit)
dc.connect()
queries = query_strs.split(';')
@@ -161,3 +165,29 @@ update_query() {
insert_query() {
server_query $1 "$2" ""
}

# unselected_server_query connects to a running mysql server, but not to a particular database, executes a query and
# compares the results against what is expected.
# In the event that the results do not match expectations, the python process will exit with an exit code of 1
# * param1 is 1 for autocommit = true, 0 for autocommit = false
# * param2 is the query_str
unselected_server_query() {
let PORT="$$ % (65536-1024) + 1024"
PYTEST_DIR="$BATS_TEST_DIRNAME/helper"
echo Executing server_query
python3 -c "$PYTHON_QUERY_SCRIPT" -- "$PYTEST_DIR" "" "$PORT" "$1" "$2" "$3"
}

# unselected_update_query runs an update query and should be called with 2 parameters
# * param1 is 1 for autocommit = true, 0 for autocommit = false
# * param2 is the query string
unselected_update_query() {
unselected_server_query $1 "$2" ""
}

# unselected_insert_query runs an insert query and should be called with 2 parameters
# * param1 is 1 for autocommit = true, 0 for autocommit = false
# * param2 is the query string
unselected_insert_query() {
unselected_server_query $1 "$2" ""
}
44 changes: 44 additions & 0 deletions integration-tests/bats/sql-server.bats
Original file line number Diff line number Diff line change
@@ -553,3 +553,47 @@ SQL

server_query 1 "SELECT * FROM test" "pk,c1,c2,c3,c4,c5\n0,1,2,3,4,5\n1,1,2,3,4,5"
}

@test "sql-server: Run queries on database without ever selecting it" {
skiponwindows "Has dependencies that are missing on the Jenkins Windows installation."

start_multi_db_server repo1

# create table with autocommit on and verify table creation
unselected_server_query 1 "CREATE TABLE repo2.one_pk (
pk int,
PRIMARY KEY (pk)
)" ""

insert_query 1 "INSERT INTO repo2.one_pk VALUES (0), (1), (2)"
unselected_server_query 1 "SELECT * FROM repo2.one_pk" "pk\n0\n1\n2"

unselected_update_query 1 "UPDATE repo2.one_pk SET pk=3 WHERE pk=2"
unselected_server_query 1 "SELECT * FROM repo2.one_pk" "pk\n0\n1\n3"

unselected_update_query 1 "DELETE FROM repo2.one_pk WHERE pk=3"
unselected_server_query 1 "SELECT * FROM repo2.one_pk" "pk\n0\n1"

# Empty commit statements should not error
unselected_server_query 1 "commit"

# create a new database and table and rerun
unselected_server_query 1 "CREATE DATABASE testdb" ""
unselected_server_query 1 "CREATE TABLE testdb.one_pk (
pk int,
PRIMARY KEY (pk)
)" ""

insert_query 1 "INSERT INTO testdb.one_pk VALUES (0), (1), (2)"
unselected_server_query 1 "SELECT * FROM testdb.one_pk" "pk\n0\n1\n2"

unselected_update_query 1 "UPDATE testdb.one_pk SET pk=3 WHERE pk=2"
unselected_server_query 1 "SELECT * FROM testdb.one_pk" "pk\n0\n1\n3"

unselected_update_query 1 "DELETE FROM testdb.one_pk WHERE pk=3"
unselected_server_query 1 "SELECT * FROM testdb.one_pk" "pk\n0\n1"

# one last query on insert db.
insert_query 1 "INSERT INTO repo2.one_pk VALUES (4)"
unselected_server_query 1 "SELECT * FROM repo2.one_pk" "pk\n0\n1\n4"
}

0 comments on commit de8f4d9

Please sign in to comment.