Skip to content

Commit 134372c

Browse files
authored
Merge pull request #8526 from dolthub/nicktobey/unionshcema
Avoid comparing `sql.Types` in `dolt diff`
2 parents 1ecf383 + 43196c0 commit 134372c

File tree

2 files changed

+132
-5
lines changed

2 files changed

+132
-5
lines changed

go/cmd/dolt/commands/diff.go

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1401,7 +1401,12 @@ func diffRows(
14011401
toSch = pkSch.Schema
14021402
}
14031403

1404-
unionSch := unionSchemas(fromSch, toSch)
1404+
var unionSch sql.Schema
1405+
if fromSch.Equals(toSch) {
1406+
unionSch = fromSch
1407+
} else {
1408+
unionSch = unionSchemas(fromSch, toSch)
1409+
}
14051410

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

1572+
if at == bt {
1573+
return origA
1574+
}
1575+
15711576
// If both are numbers, we'll take the float.
15721577
if sqltypes.IsIntegral(at) && sqltypes.IsFloat(bt) {
15731578
return origB

integration-tests/bats/diff.bats

Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1966,3 +1966,125 @@ SQL
19661966
[[ "$output" =~ "t1" ]] || false
19671967
[[ "$output" =~ "t2" ]] || false
19681968
}
1969+
1970+
1971+
@test "diff: enum data change" {
1972+
dolt sql <<SQL
1973+
drop table test;
1974+
create table test (pk int primary key, size ENUM('x-small', 'small', 'medium', 'large', 'x-large'));
1975+
insert into test values (1,'x-small');
1976+
insert into test values (2,'small');
1977+
insert into test values (3,'medium');
1978+
SQL
1979+
dolt add .
1980+
dolt commit -am "First commit"
1981+
1982+
dolt sql <<SQL
1983+
insert into test values (4,'large');
1984+
delete from test where pk = 1;
1985+
update test set size = 'x-large' where pk = 2;
1986+
SQL
1987+
1988+
run dolt diff
1989+
1990+
EXPECTED=$(cat <<'EOF'
1991+
+---+----+---------+
1992+
| | pk | size |
1993+
+---+----+---------+
1994+
| - | 1 | x-small |
1995+
| < | 2 | small |
1996+
| > | 2 | x-large |
1997+
| + | 4 | large |
1998+
+---+----+---------+
1999+
EOF
2000+
)
2001+
2002+
[ "$status" -eq 0 ]
2003+
[[ "$output" =~ "$EXPECTED" ]] || false
2004+
2005+
run dolt diff --data --schema
2006+
[ "$status" -eq 0 ]
2007+
[[ "$output" =~ "$EXPECTED" ]] || false
2008+
2009+
run dolt diff --data
2010+
[[ "$output" =~ "$EXPECTED" ]] || false
2011+
}
2012+
2013+
@test "diff: enum and schema changes" {
2014+
dolt sql <<SQL
2015+
drop table test;
2016+
create table test (pk int primary key, size ENUM('x-small', 'small', 'medium', 'large', 'x-large'));
2017+
insert into test values (1,'x-small');
2018+
insert into test values (2,'small');
2019+
insert into test values (3,'medium');
2020+
SQL
2021+
dolt add .
2022+
dolt commit -am "First commit"
2023+
2024+
dolt sql <<SQL
2025+
alter table test add column c1 int;
2026+
insert into test values (4,'large',1);
2027+
delete from test where pk = 1;
2028+
update test set size = 'x-large' where pk = 2;
2029+
SQL
2030+
2031+
run dolt diff
2032+
2033+
EXPECTED=$(cat <<'EOF'
2034+
CREATE TABLE `test` (
2035+
`pk` int NOT NULL,
2036+
`size` enum('x-small','small','medium','large','x-large'),
2037+
+ `c1` int,
2038+
PRIMARY KEY (`pk`)
2039+
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_bin;
2040+
+---+----+---------+------+
2041+
| | pk | size | c1 |
2042+
+---+----+---------+------+
2043+
| - | 1 | x-small | NULL |
2044+
| < | 2 | small | NULL |
2045+
| > | 2 | x-large | NULL |
2046+
| + | 4 | large | 1 |
2047+
+---+----+---------+------+
2048+
EOF
2049+
)
2050+
2051+
[ "$status" -eq 0 ]
2052+
[[ "$output" =~ "$EXPECTED" ]] || false
2053+
2054+
run dolt diff --data --schema
2055+
[ "$status" -eq 0 ]
2056+
[[ "$output" =~ "$EXPECTED" ]] || false
2057+
2058+
run dolt diff --schema
2059+
2060+
EXPECTED=$(cat <<'EOF'
2061+
CREATE TABLE `test` (
2062+
`pk` int NOT NULL,
2063+
`size` enum('x-small','small','medium','large','x-large'),
2064+
+ `c1` int,
2065+
PRIMARY KEY (`pk`)
2066+
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_bin;
2067+
EOF
2068+
)
2069+
2070+
[[ "$output" =~ "$EXPECTED" ]] || false
2071+
# Count the line numbers to make sure there are no data changes output
2072+
[ "${#lines[@]}" -eq 9 ]
2073+
2074+
run dolt diff --data
2075+
EXPECTED=$(cat <<'EOF'
2076+
+---+----+---------+------+
2077+
| | pk | size | c1 |
2078+
+---+----+---------+------+
2079+
| - | 1 | x-small | NULL |
2080+
| < | 2 | small | NULL |
2081+
| > | 2 | x-large | NULL |
2082+
| + | 4 | large | 1 |
2083+
+---+----+---------+------+
2084+
EOF
2085+
)
2086+
2087+
[[ "$output" =~ "$EXPECTED" ]] || false
2088+
# Count the line numbers to make sure there are no schema changes output
2089+
[ "${#lines[@]}" -eq 11 ]
2090+
}

0 commit comments

Comments
 (0)