Skip to content

Commit

Permalink
keep sql.Schema for conflicts table schema (#7877)
Browse files Browse the repository at this point in the history
  • Loading branch information
jycor authored May 17, 2024
1 parent c4695ce commit b1dfd75
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 54 deletions.
72 changes: 18 additions & 54 deletions go/cmd/dolt/commands/cnfcmds/cat.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,9 @@ import (
"github.com/dolthub/dolt/go/cmd/dolt/errhand"
eventsapi "github.com/dolthub/dolt/go/gen/proto/dolt/services/eventsapi/v1alpha1"
"github.com/dolthub/dolt/go/libraries/doltcore/env"
"github.com/dolthub/dolt/go/libraries/doltcore/schema"
"github.com/dolthub/dolt/go/libraries/doltcore/sqle/sqlutil"
"github.com/dolthub/dolt/go/libraries/doltcore/table/untyped/tabular"
"github.com/dolthub/dolt/go/libraries/utils/argparser"
"github.com/dolthub/dolt/go/libraries/utils/iohelp"
"github.com/dolthub/dolt/go/store/types"
)

type mergeStatus struct {
Expand Down Expand Up @@ -176,19 +173,14 @@ func printConflicts(queryist cli.Queryist, sqlCtx *sql.Context, tblNames []strin
return fmt.Errorf("error: failed to get conflict rows for table '%s': %w", tblName, err)
}

targetSch, err := getUnionSchemaFromConflictsSchema(confSqlSch)
sqlTargetSch, err := getUnionSchemaFromConflictsSchema(confSqlSch)
if err != nil {
return fmt.Errorf("error: failed to get union schema for table '%s': %w", tblName, err)
}

sqlTargetSch, err := sqlutil.FromDoltSchema(sqlCtx.GetCurrentDatabase(), tblName, targetSch)
if err != nil {
return fmt.Errorf("error: failed to convert dolt schema to sql schema for table '%s': %w", tblName, err)
}
tw := tabular.NewFixedWidthConflictTableWriter(sqlTargetSch, stdOut, 100)

tw := tabular.NewFixedWidthConflictTableWriter(sqlTargetSch.Schema, stdOut, 100)

err = writeConflictResults(sqlCtx, confSqlSch, sqlTargetSch.Schema, rowItr, tw)
err = writeConflictResults(sqlCtx, confSqlSch, sqlTargetSch, rowItr, tw)
if err != nil {
return fmt.Errorf("error: failed to write conflict results for table '%s': %w", tblName, err)
}
Expand All @@ -197,57 +189,29 @@ func printConflicts(queryist cli.Queryist, sqlCtx *sql.Context, tblNames []strin
return nil
}

func getUnionSchemaFromConflictsSchema(conflictsSch sql.Schema) (schema.Schema, error) {
func getUnionSchemaFromConflictsSchema(conflictsSch sql.Schema) (sql.Schema, error) {
// using array to preserve column order
baseColNames, theirColNames, ourColNames := []string{}, []string{}, []string{}

for _, col := range conflictsSch {
conflictCpy := conflictsSch.Copy()
var baseCols, theirCols, ourCols sql.Schema
for _, col := range conflictCpy {
conflictColName := col.Name
_, shouldIgnore := conflictColsToIgnore[conflictColName]
if shouldIgnore {
continue
}

if strings.HasPrefix(conflictColName, basePrefix) {
colName := conflictColName[len(basePrefix):]
baseColNames = append(baseColNames, colName)
} else if strings.HasPrefix(conflictColName, theirPrefix) {
colName := conflictColName[len(theirPrefix):]
theirColNames = append(theirColNames, colName)
} else if strings.HasPrefix(conflictColName, ourPrefix) {
colName := conflictColName[len(ourPrefix):]
ourColNames = append(ourColNames, colName)
}
}

unionColNames := []string{}
for _, colName := range baseColNames {
if !isStringInArray(colName, unionColNames) {
unionColNames = append(unionColNames, colName)
}
}
for _, colName := range theirColNames {
if !isStringInArray(colName, unionColNames) {
unionColNames = append(unionColNames, colName)
}
}
for _, colName := range ourColNames {
if !isStringInArray(colName, unionColNames) {
unionColNames = append(unionColNames, colName)
switch {
case strings.HasPrefix(conflictColName, basePrefix):
col.Name = conflictColName[len(basePrefix):]
baseCols = append(baseCols, col)
case strings.HasPrefix(conflictColName, theirPrefix):
col.Name = conflictColName[len(theirPrefix):]
theirCols = append(theirCols, col)
case strings.HasPrefix(conflictColName, ourPrefix):
col.Name = conflictColName[len(ourPrefix):]
ourCols = append(ourCols, col)
}
}

unionCols := []schema.Column{}
for _, colName := range unionColNames {
col := schema.NewColumn(colName, 0, types.StringKind, false)
unionCols = append(unionCols, col)
}
unionColl := schema.NewColCollection(unionCols...)
unionSchema, err := schema.SchemaFromCols(unionColl)
if err != nil {
return nil, fmt.Errorf("error: failed to create union schema: %w", err)
}
return unionSchema, nil
return append(append(baseCols, theirCols...), ourCols...), nil
}

func printSchemaConflicts(queryist cli.Queryist, sqlCtx *sql.Context, wrCloser io.WriteCloser, table string) error {
Expand Down
30 changes: 30 additions & 0 deletions integration-tests/bats/conflict-cat.bats
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,36 @@ teardown() {
teardown_common
}

@test "conflict-cat: large diff" {
dolt sql -q "create table t (i int primary key, t text);"
dolt commit -Am "create table t"

dolt branch other
dolt sql -q "insert into t values (1, space(10000));"
dolt commit -Am "main 10000 spaces"

dolt checkout other
dolt sql -q "insert into t values (1, space(10001));"
dolt commit -Am "other 10001 spaces"

dolt checkout main
run dolt merge other
[ "$status" -eq 1 ]
[[ "$output" =~ "CONFLICT (content): Merge conflict in t" ]] || false

run dolt conflicts cat t
[ "$status" -eq 0 ]

dolt sql -q "select length(base_t), length(our_t), length(their_t) from dolt_conflicts_t";
run dolt sql -q "select length(base_t), length(our_t), length(their_t) from dolt_conflicts_t";
[ "$status" -eq 0 ]
[[ "$output" =~ "+----------------+---------------+-----------------+" ]] || false
[[ "$output" =~ "| length(base_t) | length(our_t) | length(their_t) |" ]] || false
[[ "$output" =~ "+----------------+---------------+-----------------+" ]] || false
[[ "$output" =~ "| NULL | 10000 | 10001 |" ]] || false
[[ "$output" =~ "+----------------+---------------+-----------------+" ]] || false
}

@test "conflict-cat: smoke test print schema output" {
dolt sql << SQL
CREATE TABLE people (
Expand Down
Binary file modified integration-tests/bats/performance-repo/.dolt/noms/journal.idx
Binary file not shown.

0 comments on commit b1dfd75

Please sign in to comment.