Skip to content

Commit

Permalink
Merge pull request #8526 from dolthub/nicktobey/unionshcema
Browse files Browse the repository at this point in the history
Avoid comparing `sql.Types` in `dolt diff`
  • Loading branch information
nicktobey authored Nov 4, 2024
2 parents 1ecf383 + 43196c0 commit 134372c
Show file tree
Hide file tree
Showing 2 changed files with 132 additions and 5 deletions.
15 changes: 10 additions & 5 deletions go/cmd/dolt/commands/diff.go
Original file line number Diff line number Diff line change
Expand Up @@ -1401,7 +1401,12 @@ func diffRows(
toSch = pkSch.Schema
}

unionSch := unionSchemas(fromSch, toSch)
var unionSch sql.Schema
if fromSch.Equals(toSch) {
unionSch = fromSch
} else {
unionSch = unionSchemas(fromSch, toSch)
}

// We always instantiate a RowWriter in case the diffWriter needs it to close off any work from schema output
rowWriter, err := dw.RowWriter(fromTableInfo, toTableInfo, tableSummary, unionSch)
Expand Down Expand Up @@ -1561,13 +1566,13 @@ func unionSchemas(s1 sql.Schema, s2 sql.Schema) sql.Schema {
//
// Note this is only for printing the diff. This is not robust for other purposes.
func chooseMostFlexibleType(origA, origB sql.Type) sql.Type {
if origA == origB {
return origA
}

at := origA.Type()
bt := origB.Type()

if at == bt {
return origA
}

// If both are numbers, we'll take the float.
if sqltypes.IsIntegral(at) && sqltypes.IsFloat(bt) {
return origB
Expand Down
122 changes: 122 additions & 0 deletions integration-tests/bats/diff.bats
Original file line number Diff line number Diff line change
Expand Up @@ -1966,3 +1966,125 @@ SQL
[[ "$output" =~ "t1" ]] || false
[[ "$output" =~ "t2" ]] || false
}


@test "diff: enum data change" {
dolt sql <<SQL
drop table test;
create table test (pk int primary key, size ENUM('x-small', 'small', 'medium', 'large', 'x-large'));
insert into test values (1,'x-small');
insert into test values (2,'small');
insert into test values (3,'medium');
SQL
dolt add .
dolt commit -am "First commit"

dolt sql <<SQL
insert into test values (4,'large');
delete from test where pk = 1;
update test set size = 'x-large' where pk = 2;
SQL

run dolt diff

EXPECTED=$(cat <<'EOF'
+---+----+---------+
| | pk | size |
+---+----+---------+
| - | 1 | x-small |
| < | 2 | small |
| > | 2 | x-large |
| + | 4 | large |
+---+----+---------+
EOF
)

[ "$status" -eq 0 ]
[[ "$output" =~ "$EXPECTED" ]] || false

run dolt diff --data --schema
[ "$status" -eq 0 ]
[[ "$output" =~ "$EXPECTED" ]] || false

run dolt diff --data
[[ "$output" =~ "$EXPECTED" ]] || false
}

@test "diff: enum and schema changes" {
dolt sql <<SQL
drop table test;
create table test (pk int primary key, size ENUM('x-small', 'small', 'medium', 'large', 'x-large'));
insert into test values (1,'x-small');
insert into test values (2,'small');
insert into test values (3,'medium');
SQL
dolt add .
dolt commit -am "First commit"

dolt sql <<SQL
alter table test add column c1 int;
insert into test values (4,'large',1);
delete from test where pk = 1;
update test set size = 'x-large' where pk = 2;
SQL

run dolt diff

EXPECTED=$(cat <<'EOF'
CREATE TABLE `test` (
`pk` int NOT NULL,
`size` enum('x-small','small','medium','large','x-large'),
+ `c1` int,
PRIMARY KEY (`pk`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_bin;
+---+----+---------+------+
| | pk | size | c1 |
+---+----+---------+------+
| - | 1 | x-small | NULL |
| < | 2 | small | NULL |
| > | 2 | x-large | NULL |
| + | 4 | large | 1 |
+---+----+---------+------+
EOF
)

[ "$status" -eq 0 ]
[[ "$output" =~ "$EXPECTED" ]] || false

run dolt diff --data --schema
[ "$status" -eq 0 ]
[[ "$output" =~ "$EXPECTED" ]] || false

run dolt diff --schema

EXPECTED=$(cat <<'EOF'
CREATE TABLE `test` (
`pk` int NOT NULL,
`size` enum('x-small','small','medium','large','x-large'),
+ `c1` int,
PRIMARY KEY (`pk`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_bin;
EOF
)

[[ "$output" =~ "$EXPECTED" ]] || false
# Count the line numbers to make sure there are no data changes output
[ "${#lines[@]}" -eq 9 ]

run dolt diff --data
EXPECTED=$(cat <<'EOF'
+---+----+---------+------+
| | pk | size | c1 |
+---+----+---------+------+
| - | 1 | x-small | NULL |
| < | 2 | small | NULL |
| > | 2 | x-large | NULL |
| + | 4 | large | 1 |
+---+----+---------+------+
EOF
)

[[ "$output" =~ "$EXPECTED" ]] || false
# Count the line numbers to make sure there are no schema changes output
[ "${#lines[@]}" -eq 11 ]
}

0 comments on commit 134372c

Please sign in to comment.