diff --git a/bats/helper/employees-tbl-schema-unordered.csv b/bats/helper/employees-tbl-schema-unordered.csv new file mode 100644 index 0000000000..b3f86f2eca --- /dev/null +++ b/bats/helper/employees-tbl-schema-unordered.csv @@ -0,0 +1,4 @@ +id, title, start date, end date, first name, last name +0, "ceo", "", "", "tim", "sehn" +1, "founder", "", "", "aaron", "son" +2, "founder", "", "", "brian", "hendriks" diff --git a/bats/helper/employees-tbl-schema-unordered.json b/bats/helper/employees-tbl-schema-unordered.json new file mode 100644 index 0000000000..5db39e50a5 --- /dev/null +++ b/bats/helper/employees-tbl-schema-unordered.json @@ -0,0 +1,28 @@ +{ + "rows": [ + { + "id": 0, + "title": "ceo", + "start date": "", + "end date": "", + "first name": "tim", + "last name": "sehn" + }, + { + "id": 1, + "title": "founder", + "start date": "", + "end date": "", + "first name": "aaron", + "last name": "son" + }, + { + "id": 2, + "title": "founder", + "start date": "", + "end date": "", + "first name": "brian", + "last name": "hendricks" + } + ] +} diff --git a/bats/replace-tables.bats b/bats/replace-tables.bats index 4ecc7b0248..91860b9037 100644 --- a/bats/replace-tables.bats +++ b/bats/replace-tables.bats @@ -155,4 +155,23 @@ teardown() { [ "$status" -eq 0 ] [[ "$output" =~ "Rows Processed: 4, Additions: 4, Modifications: 0, Had No Effect: 0" ]] || false [[ "$output" =~ "Import completed successfully." ]] || false -} \ No newline at end of file +} + +@test "replace table with a json with columns in different order" { + run dolt table create -s `batshelper employees-sch.json` employees + [ "$status" -eq 0 ] + run dolt table import -r employees `batshelper employees-tbl-schema-unordered.json` + [ "$status" -eq 0 ] + [[ "$output" =~ "Rows Processed: 3, Additions: 3, Modifications: 0, Had No Effect: 0" ]] || false + [[ "$output" =~ "Import completed successfully." ]] || false +} + +@test "replace table with a csv with columns in different order" { + run dolt table create -s `batshelper employees-sch.json` employees + [ "$status" -eq 0 ] + run dolt table import -r employees `batshelper employees-tbl-schema-unordered.csv` + echo "$output" + [ "$status" -eq 0 ] + [[ "$output" =~ "Rows Processed: 3, Additions: 3, Modifications: 0, Had No Effect: 0" ]] || false + [[ "$output" =~ "Import completed successfully." ]] || false +} diff --git a/bats/update-tables.bats b/bats/update-tables.bats index b4cdf7ed3d..de840f3580 100644 --- a/bats/update-tables.bats +++ b/bats/update-tables.bats @@ -77,4 +77,24 @@ teardown() { run dolt table import -u employees `batshelper employees-tbl.json` [ "$status" -eq 1 ] [[ "$output" =~ "The following table could not be found:" ]] || false -} \ No newline at end of file +} + +@test "replace table with a json with columns in different order" { + run dolt table create -s `batshelper employees-sch.json` employees + [ "$status" -eq 0 ] + run dolt table import -u employees `batshelper employees-tbl-schema-unordered.json` + echo "$output" + [ "$status" -eq 0 ] + [[ "$output" =~ "Rows Processed: 3, Additions: 3, Modifications: 0, Had No Effect: 0" ]] || false + [[ "$output" =~ "Import completed successfully." ]] || false +} + +@test "replace table with a csv with columns in different order" { + run dolt table create -s `batshelper employees-sch.json` employees + [ "$status" -eq 0 ] + run dolt table import -u employees `batshelper employees-tbl-schema-unordered.csv` + echo "$output" + [ "$status" -eq 0 ] + [[ "$output" =~ "Rows Processed: 3, Additions: 3, Modifications: 0, Had No Effect: 0" ]] || false + [[ "$output" =~ "Import completed successfully." ]] || false +} diff --git a/go/libraries/doltcore/schema/schema.go b/go/libraries/doltcore/schema/schema.go index 5889cf6219..edf6250416 100644 --- a/go/libraries/doltcore/schema/schema.go +++ b/go/libraries/doltcore/schema/schema.go @@ -84,8 +84,8 @@ func SchemasAreEqual(sch1, sch2 Schema) (bool, error) { return areEqual, nil } -// VerifyInSchema tests that the incoming schema matches the schema from the original table. -// The test for column equality is more flexible than SchemasAreEqual. +// VerifyInSchema tests that the incoming schema matches the schema from the original table +// based on the presence of the column name in the original schema. func VerifyInSchema(inSch, outSch Schema) (bool, error) { inSchCols := inSch.GetAllCols() outSchCols := outSch.GetAllCols() @@ -95,10 +95,10 @@ func VerifyInSchema(inSch, outSch Schema) (bool, error) { } match := true - err := outSchCols.Iter(func(tag uint64, outCol Column) (stop bool, err error) { - inCol, ok := inSchCols.GetByTag(tag) + err := inSchCols.Iter(func(tag uint64, inCol Column) (stop bool, err error) { + _, isValid := outSchCols.GetByNameCaseInsensitive(inCol.Name) - if !ok || inCol.Name != outCol.Name || inCol.Tag != outCol.Tag { + if !isValid { match = false return true, nil }