From fbf2ea18f5c5de8cedbd3af64140f5bd27901dc6 Mon Sep 17 00:00:00 2001 From: Zach Musgrave Date: Mon, 27 Sep 2021 17:05:14 -0700 Subject: [PATCH 1/3] Refactored table export --- go/cmd/dolt/commands/tblcmds/export.go | 49 ++++++++++++++------------ 1 file changed, 26 insertions(+), 23 deletions(-) diff --git a/go/cmd/dolt/commands/tblcmds/export.go b/go/cmd/dolt/commands/tblcmds/export.go index 329437c5ac..d4b320c60b 100644 --- a/go/cmd/dolt/commands/tblcmds/export.go +++ b/go/cmd/dolt/commands/tblcmds/export.go @@ -89,22 +89,8 @@ func (m exportOptions) DestName() string { return m.dest.String() } -// validateExportArgs validates the input from the arg parser, and returns the tuple: -// (table name to export, data location of table to export, data location to export to) -func validateExportArgs(apr *argparser.ArgParseResults, usage cli.UsagePrinter) (string, mvdata.TableDataLocation, mvdata.DataLocation) { - if apr.NArg() == 0 || apr.NArg() > 2 { - usage() - return "", mvdata.TableDataLocation{}, nil - } - - tableName := apr.Arg(0) - if !doltdb.IsValidTableName(tableName) { - cli.PrintErrln( - color.RedString("'%s' is not a valid table name\n", tableName), - "table names must match the regular expression:", doltdb.TableNameRegexStr) - return "", mvdata.TableDataLocation{}, nil - } - +// getExportDestination returns an export destination corresponding to the input parameters +func getExportDestination(apr *argparser.ArgParseResults) mvdata.DataLocation { path := "" if apr.NArg() > 1 { path = apr.Arg(1) @@ -119,7 +105,7 @@ func validateExportArgs(apr *argparser.ArgParseResults, usage cli.UsagePrinter) cli.PrintErrln( color.RedString("Could not infer type file '%s'\n", path), "File extensions should match supported file types, or should be explicitly defined via the file-type parameter") - return "", mvdata.TableDataLocation{}, nil + return nil } case mvdata.StreamDataLocation: @@ -128,21 +114,38 @@ func validateExportArgs(apr *argparser.ArgParseResults, usage cli.UsagePrinter) destLoc = val } else if val.Format != mvdata.CsvFile && val.Format != mvdata.PsvFile { cli.PrintErrln(color.RedString("Cannot export this format to stdout")) - return "", mvdata.TableDataLocation{}, nil + return nil } } - tableLoc := mvdata.TableDataLocation{Name: tableName} - - return tableName, tableLoc, destLoc + return destLoc } func parseExportArgs(ap *argparser.ArgParser, commandStr string, args []string) (*exportOptions, errhand.VerboseError) { help, usage := cli.HelpAndUsagePrinters(cli.GetCommandDocumentation(commandStr, exportDocs, ap)) apr := cli.ParseArgsOrDie(ap, args, help) - tableName, tableLoc, fileLoc := validateExportArgs(apr, usage) - if fileLoc == nil || len(tableLoc.Name) == 0 { + if apr.NArg() == 0 { + usage() + return nil, errhand.BuildDError("missing required argument").Build() + } else if apr.NArg() > 2 { + usage() + return nil, errhand.BuildDError("too many arguments").Build() + } + + tableName := apr.Arg(0) + if !doltdb.IsValidTableName(tableName) { + usage() + cli.PrintErrln( + color.RedString("'%s' is not a valid table name\n", tableName), + "table names must match the regular expression:", doltdb.TableNameRegexStr) + return nil, errhand.BuildDError("invalid table name").Build() + } + + tableLoc := mvdata.TableDataLocation{Name: tableName} + fileLoc := getExportDestination(apr) + + if fileLoc == nil { return nil, errhand.BuildDError("could not validate table export args").Build() } From 15d0777ba9b09eb903f8731ca287148ea56277a7 Mon Sep 17 00:00:00 2001 From: Zach Musgrave Date: Tue, 28 Sep 2021 13:06:27 -0700 Subject: [PATCH 2/3] Test for exporting sql string columns --- go/libraries/doltcore/sqle/sqlfmt/row_fmt.go | 2 +- integration-tests/bats/export-tables.bats | 11 +++++++++++ 2 files changed, 12 insertions(+), 1 deletion(-) diff --git a/go/libraries/doltcore/sqle/sqlfmt/row_fmt.go b/go/libraries/doltcore/sqle/sqlfmt/row_fmt.go index 2b20e4c80e..2afecdc60e 100644 --- a/go/libraries/doltcore/sqle/sqlfmt/row_fmt.go +++ b/go/libraries/doltcore/sqle/sqlfmt/row_fmt.go @@ -213,7 +213,7 @@ func valueAsSqlString(ti typeinfo.TypeInfo, value types.Value) (string, error) { } return quoteAndEscapeString(string(s)), nil default: - return *str, nil + return quoteAndEscapeString(*str), nil } } diff --git a/integration-tests/bats/export-tables.bats b/integration-tests/bats/export-tables.bats index d5729131ae..bafb9c5cef 100644 --- a/integration-tests/bats/export-tables.bats +++ b/integration-tests/bats/export-tables.bats @@ -106,6 +106,17 @@ if rows[2] != "9,8,7,6,5,4".split(","): [[ "$output" =~ "Successfully exported data." ]] || false [ -f export.sql ] diff --strip-trailing-cr $BATS_TEST_DIRNAME/helper/1pk5col-ints.sql export.sql + + dolt sql -q "create table strings (a varchar(10) primary key, b char(10))" + dolt sql -q "insert into strings values ('abc', '123'), ('def', '456')" + dolt commit -am "Checkpoint" + + dolt table export strings -f export.sql + dolt sql -q "drop table strings" + dolt sql < export.sql + + run dolt status + [[ "$output" =~ "working tree clean" ]] || false } @test "export-tables: export a table with a string with commas to csv" { From 3e29a269ac782c59e6994d596c0b815a717ac25a Mon Sep 17 00:00:00 2001 From: Zach Musgrave Date: Tue, 28 Sep 2021 13:13:55 -0700 Subject: [PATCH 3/3] Fixed bug for enum columns --- go/libraries/doltcore/sqle/sqlfmt/row_fmt.go | 10 ++-------- integration-tests/bats/export-tables.bats | 13 ++++++++++++- 2 files changed, 14 insertions(+), 9 deletions(-) diff --git a/go/libraries/doltcore/sqle/sqlfmt/row_fmt.go b/go/libraries/doltcore/sqle/sqlfmt/row_fmt.go index 2afecdc60e..7039170a4e 100644 --- a/go/libraries/doltcore/sqle/sqlfmt/row_fmt.go +++ b/go/libraries/doltcore/sqle/sqlfmt/row_fmt.go @@ -196,13 +196,7 @@ func valueAsSqlString(ti typeinfo.TypeInfo, value types.Value) (string, error) { return "TRUE", nil } return "FALSE", nil - case typeinfo.UuidTypeIdentifier: - return singleQuote + *str + singleQuote, nil - case typeinfo.TimeTypeIdentifier: - return singleQuote + *str + singleQuote, nil - case typeinfo.YearTypeIdentifier: - return singleQuote + *str + singleQuote, nil - case typeinfo.DatetimeTypeIdentifier: + case typeinfo.UuidTypeIdentifier, typeinfo.TimeTypeIdentifier, typeinfo.YearTypeIdentifier, typeinfo.DatetimeTypeIdentifier, typeinfo.EnumTypeIdentifier: return singleQuote + *str + singleQuote, nil case typeinfo.BlobStringTypeIdentifier, typeinfo.VarBinaryTypeIdentifier, typeinfo.InlineBlobTypeIdentifier: return quoteAndEscapeString(*str), nil @@ -213,7 +207,7 @@ func valueAsSqlString(ti typeinfo.TypeInfo, value types.Value) (string, error) { } return quoteAndEscapeString(string(s)), nil default: - return quoteAndEscapeString(*str), nil + return *str, nil } } diff --git a/integration-tests/bats/export-tables.bats b/integration-tests/bats/export-tables.bats index bafb9c5cef..3350341525 100644 --- a/integration-tests/bats/export-tables.bats +++ b/integration-tests/bats/export-tables.bats @@ -107,16 +107,27 @@ if rows[2] != "9,8,7,6,5,4".split(","): [ -f export.sql ] diff --strip-trailing-cr $BATS_TEST_DIRNAME/helper/1pk5col-ints.sql export.sql + # string columns dolt sql -q "create table strings (a varchar(10) primary key, b char(10))" dolt sql -q "insert into strings values ('abc', '123'), ('def', '456')" dolt commit -am "Checkpoint" dolt table export strings -f export.sql - dolt sql -q "drop table strings" dolt sql < export.sql run dolt status [[ "$output" =~ "working tree clean" ]] || false + + # enum columns + dolt sql -q "create table enums (a varchar(10) primary key, b enum('one','two','three'))" + dolt sql -q "insert into enums values ('abc', 'one'), ('def', 'two')" + dolt commit -am "Checkpoint" + + dolt table export enums -f export.sql + dolt sql < export.sql + + run dolt status + [[ "$output" =~ "working tree clean" ]] || false } @test "export-tables: export a table with a string with commas to csv" {