Merge branch 'main' of github.com:dolthub/dolt into nicktobey/addcommand

This commit is contained in:
Nick Tobey
2023-06-05 11:14:01 -07:00
5 changed files with 108 additions and 36 deletions

View File

@@ -111,7 +111,7 @@ jobs:
run: expect -v
- name: Test all Unix
env:
SQL_ENGINE: ${{ matrix.sql-engine }}
SQL_ENGINE: ${{ matrix.sql_engine }}
PARQUET_RUNTIME_JAR: ${{ steps.parquet_cli.outputs.runtime_jar }}
BATS_TEST_RETRIES: "3"
run: |

View File

@@ -17,15 +17,22 @@ package commands
import (
"context"
"fmt"
"regexp"
"github.com/dolthub/go-mysql-server/sql"
"github.com/dolthub/dolt/go/cmd/dolt/cli"
"github.com/dolthub/dolt/go/cmd/dolt/commands/engine"
eventsapi "github.com/dolthub/dolt/go/gen/proto/dolt/services/eventsapi/v1alpha1"
"github.com/dolthub/dolt/go/libraries/doltcore/doltdb"
"github.com/dolthub/dolt/go/libraries/doltcore/env"
ref2 "github.com/dolthub/dolt/go/libraries/doltcore/ref"
"github.com/dolthub/dolt/go/libraries/utils/argparser"
"github.com/dolthub/dolt/go/libraries/utils/iohelp"
)
const (
blameQueryTemplate = "SELECT * FROM dolt_blame_%s"
blameQueryTemplate = "SELECT * FROM dolt_blame_%s AS OF '%s'"
)
var blameDocs = cli.CommandDocumentationContent{
@@ -54,10 +61,16 @@ func (cmd BlameCmd) Docs() *cli.CommandDocumentation {
}
func (cmd BlameCmd) ArgParser() *argparser.ArgParser {
ap := argparser.NewArgParserWithMaxArgs(cmd.Name(), 1)
ap := argparser.NewArgParserWithMaxArgs(cmd.Name(), 2)
return ap
}
func (cmd BlameCmd) RequiresRepo() bool {
return false
}
var _ cli.RepoNotRequiredCommand = BlameCmd{}
// EventType returns the type of the event to log
func (cmd BlameCmd) EventType() eventsapi.ClientEventType {
return eventsapi.ClientEventType_BLAME
@@ -84,11 +97,49 @@ func (cmd BlameCmd) Exec(ctx context.Context, commandStr string, args []string,
help, usage := cli.HelpAndUsagePrinters(cli.CommandDocsForCommandString(commandStr, blameDocs, ap))
apr := cli.ParseArgsOrDie(ap, args, help)
if apr.NArg() != 1 {
if apr.NArg() > 2 || apr.NArg() == 0 {
usage()
return 1
}
args = []string{"--" + QueryFlag, fmt.Sprintf(blameQueryTemplate, apr.Arg(0))}
return SqlCmd{}.Exec(ctx, "sql", args, dEnv, cliCtx)
queryist, sqlCtx, closeFunc, err := cliCtx.QueryEngine(ctx)
if err != nil {
iohelp.WriteLine(cli.CliOut, err.Error())
return 1
}
if closeFunc != nil {
defer closeFunc()
}
var schema sql.Schema
var ri sql.RowIter
if apr.NArg() == 1 {
schema, ri, err = queryist.Query(sqlCtx, fmt.Sprintf(blameQueryTemplate, apr.Arg(0), "HEAD"))
} else {
// validate input
ref := apr.Arg(0)
if !ref2.IsValidTagName(ref) && !doltdb.IsValidCommitHash(ref) && !isValidHeadRef(ref) {
iohelp.WriteLine(cli.CliOut, "Invalid reference provided")
return 1
}
schema, ri, err = queryist.Query(sqlCtx, fmt.Sprintf(blameQueryTemplate, apr.Arg(1), apr.Arg(0)))
}
if err != nil {
iohelp.WriteLine(cli.CliOut, err.Error())
return 1
}
err = engine.PrettyPrintResults(sqlCtx, engine.FormatTabular, schema, ri)
if err != nil {
iohelp.WriteLine(cli.CliOut, err.Error())
return 1
}
return 0
}
func isValidHeadRef(s string) bool {
var refRegex = regexp.MustCompile(`(?i)^head[\~\^0-9]*$`)
return refRegex.MatchString(s)
}

View File

@@ -50,55 +50,54 @@ SQL
[[ "$output" =~ "usage" ]] || false
}
@test "blame: too many arguments shows usage" {
run dolt blame HEAD blame_test foo
[ "$status" -eq 1 ]
[[ "$output" =~ "blame has too many positional arguments" ]] || false
}
@test "blame: annotates a small table with simple history" {
# should be the same as dolt blame HEAD blame_test
run dolt blame blame_test
[ "$status" -eq 0 ]
# TODO: Make these assertions better
[[ "$output" =~ "Thomas Foolery" ]] || false
[[ "$output" =~ "create blame_test table" ]] || false
[[ "${lines[1]}" =~ "pk".*"commit".*"commit_date".*"committer".*"email".*"message" ]] || false
[[ "$output" =~ "1 |".+"|".+"| Thomas Foolery, | bats-1@email.fake | create blame_test table |" ]] || false
[[ ! "$output" =~ "Richard Tracy" ]] || false
[[ ! "$output" =~ "add richard to blame_test" ]] || false
[[ "$output" =~ "Harry Wombat" ]] || false
[[ "$output" =~ "replace richard" ]] || false
[[ "$output" =~ "Johnny Moolah" ]] || false
[[ "$output" =~ "add more people" ]] || false
[[ "$output" =~ "2 |".+"|".+"| Harry Wombat, | bats-3@email.fake | replace richard with harry |" ]] || false
[[ "$output" =~ "3 |".+"|".+"| Johnny Moolah, | bats-4@email.fake | add more people to blame_test |" ]] || false
[[ "$output" =~ "4 |".+"|".+"| Johnny Moolah, | bats-4@email.fake | add more people to blame_test |" ]] || false
}
@test "blame: blames HEAD when commit ref omitted" {
run dolt blame blame_test
[ "$status" -eq 0 ]
[[ "$output" =~ "Thomas Foolery" ]] || false
[[ "$output" =~ "create blame_test table" ]] || false
[[ "$output" =~ "1".*"Thomas Foolery".*"create blame_test table" ]] || false
[[ ! "$output" =~ "Richard Tracy" ]] || false
[[ ! "$output" =~ "add richard to blame_test" ]] || false
[[ "$output" =~ "Harry Wombat" ]] || false
[[ "$output" =~ "replace richard" ]] || false
[[ "$output" =~ "Johnny Moolah" ]] || false
[[ "$output" =~ "add more people" ]] || false
[[ "$output" =~ "2".*"Harry Wombat".*"replace richard" ]] || false
[[ "$output" =~ "3".*"Johnny Moolah".*"add more people" ]] || false
[[ "$output" =~ "4".*"Johnny Moolah".*"add more people" ]] || false
}
@test "blame: works with HEAD as the commit ref" {
skip "SQL views do no support AS OF queries"
run dolt blame HEAD blame_test
[ "$status" -eq 0 ]
[[ "$output" =~ "Thomas Foolery" ]] || false
[[ "$output" =~ "create blame_test table" ]] || false
[[ "$output" =~ "1".*"Thomas Foolery".*"create blame_test table" ]] || false
[[ ! "$output" =~ "Richard Tracy" ]] || false
[[ ! "$output" =~ "add richard to blame_test" ]] || false
[[ "$output" =~ "Harry Wombat" ]] || false
[[ "$output" =~ "replace richard" ]] || false
[[ "$output" =~ "Johnny Moolah" ]] || false
[[ "$output" =~ "add more people" ]] || false
[[ "$output" =~ "2".*"Harry Wombat".*"replace richard" ]] || false
[[ "$output" =~ "3".*"Johnny Moolah".*"add more people" ]] || false
[[ "$output" =~ "4".*"Johnny Moolah".*"add more people" ]] || false
}
@test "blame: works with HEAD~1 as the commit ref" {
skip "SQL views do no support AS OF queries"
run dolt blame HEAD~1 blame_test
[ "$status" -eq 0 ]
[[ "$output" =~ "Thomas Foolery" ]] || false
[[ "$output" =~ "create blame_test table" ]] || false
[[ ! "$output" =~ "Richard Tracy" ]] || false
@@ -110,7 +109,7 @@ SQL
}
@test "blame: works with HEAD~2 as the commit ref" {
skip "SQL views do no support AS OF queries"
skip "SQL views return incorrect data when using AS OF with commits that modify existing data"
run dolt blame HEAD~2 blame_test
[ "$status" -eq 0 ]
@@ -125,10 +124,9 @@ SQL
}
@test "blame: works with HEAD~3 as the commit ref" {
skip "SQL views do no support AS OF queries"
run dolt blame HEAD~3 blame_test
[ "$status" -eq 0 ]
[[ "$output" =~ "Thomas Foolery" ]] || false
[[ "$output" =~ "create blame_test table" ]] || false
[[ ! "$output" =~ "Richard Tracy" ]] || false
@@ -140,10 +138,9 @@ SQL
}
@test "blame: returns an error when the table is not found in the given revision" {
skip "SQL views do no support AS OF queries"
run dolt blame HEAD~4 blame_test
[ "$status" -eq 1 ]
[[ "$output" =~ "no table named blame_test found" ]] || false
[ "$status" -eq 0 ]
[[ "$output" = "" ]] || false
}
@test "blame: pk ordered output" {

View File

@@ -52,7 +52,7 @@ teardown() {
cd $BATS_TMPDIR
if ! [ "$DOLT_DEFAULT_BIN_FORMAT" = "__DOLT__" ]; then
dolt config --list | awk '{ print $1 }' | grep sqlserver.global | xargs dolt config --global --unset
dolt config --list | awk '{ print $1 }' | grep sqlserver.global | xargs -r dolt config --global --unset
fi
}

View File

@@ -11,6 +11,9 @@ make_repo() {
}
setup() {
if [ "$SQL_ENGINE" = "remote-engine" ]; then
skip "This test tests remote connections directly, SQL_ENGINE is not needed."
fi
setup_no_dolt_init
make_repo defaultDB
make_repo altDB
@@ -116,6 +119,27 @@ get_staged_tables() {
[[ "$output" =~ "defaultDB does not exist" ]] || false
}
@test "sql-local-remote: verify dolt blame behavior is identical in switch between server/no server" {
cd altDB
dolt sql -q "create table test (pk int primary key)"
dolt sql -q "insert into test values (1)"
dolt add test
dolt commit -m "insert initial value into test"
dolt sql -q "insert into test values (2), (3)"
dolt add test
dolt commit -m "insert more values into test"
cd ..
start_sql_server altDB
run dolt --user dolt blame test
[ "$status" -eq 0 ]
export out="$output"
stop_sql_server 1
run dolt blame test
[ "$status" -eq 0 ]
[[ "$output" = $out ]] || false
}
@test "sql-local-remote: verify simple dolt add behavior." {
start_sql_server altDb
cd altDb