Merge pull request #2193 from dolthub/zachmu/sql-export

Bug fix for SQL export of enum columns
This commit is contained in:
Zach Musgrave
2021-09-28 13:57:02 -07:00
committed by GitHub
3 changed files with 49 additions and 30 deletions

View File

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

View File

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

View File

@@ -106,6 +106,28 @@ 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
# 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 < 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" {