mirror of
https://github.com/dolthub/dolt.git
synced 2026-01-06 00:39:40 -06:00
fixed index/fk diff, added tests
This commit is contained in:
@@ -131,6 +131,40 @@ teardown() {
|
||||
[[ ! "$output" =~ "+ | 0" ]] || false
|
||||
}
|
||||
|
||||
@test "diff with index and foreign key changes" {
|
||||
dolt sql <<SQL
|
||||
CREATE TABLE parent (
|
||||
pk bigint PRIMARY KEY,
|
||||
c1 bigint,
|
||||
c2 bigint,
|
||||
INDEX c1 (c1)
|
||||
);
|
||||
ALTER TABLE test ADD CONSTRAINT fk1 FOREIGN KEY (c1) REFERENCES parent(c1);
|
||||
SQL
|
||||
dolt add -A
|
||||
dolt commit -m "added parent table, foreign key"
|
||||
dolt sql <<SQL
|
||||
ALTER TABLE parent ADD INDEX c2 (c2);
|
||||
ALTER TABLE parent DROP INDEX c1;
|
||||
ALTER TABLE test DROP FOREIGN KEY fk1;
|
||||
ALTER TABLE test ADD CONSTRAINT fk2 FOREIGN KEY (c2) REFERENCES parent(c2);
|
||||
SQL
|
||||
dolt diff test
|
||||
run dolt diff test
|
||||
[ "$status" -eq 0 ]
|
||||
[[ "$output" =~ "+ INDEX \`c2\` (\`c2\`)" ]] || false
|
||||
[[ "$output" =~ "- CONSTRAINT \`fk1\` FOREIGN KEY (\`c1\`)" ]] || false
|
||||
[[ "$output" =~ " REFERENCES \`parent\` (\`c1\`)" ]] || false
|
||||
[[ "$output" =~ "+ CONSTRAINT \`fk2\` FOREIGN KEY (\`c2\`)" ]] || false
|
||||
[[ "$output" =~ " REFERENCES \`parent\` (\`c2\`)" ]] || false
|
||||
|
||||
dolt diff parent
|
||||
run dolt diff parent
|
||||
[ "$status" -eq 0 ]
|
||||
[[ "$output" =~ "- INDEX \`c1\` (\`c1\`)" ]] || false
|
||||
[[ "$output" =~ "+ INDEX \`c2\` (\`c2\`)" ]] || false
|
||||
}
|
||||
|
||||
@test "diff summary comparing working table to last commit" {
|
||||
dolt sql -q "insert into test values (0, 0, 0, 0, 0, 0)"
|
||||
dolt sql -q "insert into test values (1, 1, 1, 1, 1, 1)"
|
||||
|
||||
@@ -442,7 +442,9 @@ func tabularSchemaDiff(ctx context.Context, td diff.TableDelta, fromSchemas, toS
|
||||
if err != nil {
|
||||
return errhand.BuildDError("cannot retrieve schema for table %s", td.ToName).AddCause(err).Build()
|
||||
}
|
||||
if eq, _ := schema.SchemasAreEqual(fromSch, toSch); eq {
|
||||
|
||||
eq, _ := schema.SchemasAreEqual(fromSch, toSch)
|
||||
if eq && !td.HasFKChanges() {
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -493,14 +495,14 @@ func tabularSchemaDiff(ctx context.Context, td diff.TableDelta, fromSchemas, toS
|
||||
for _, idxDiff := range diff.DiffSchIndexes(fromSch, toSch) {
|
||||
switch idxDiff.DiffType {
|
||||
case diff.SchDiffNone:
|
||||
cli.Println(" " + sqlfmt.FmtIndex(idxDiff.To))
|
||||
cli.Println(" " + sqlfmt.FmtIndex(idxDiff.To))
|
||||
case diff.SchDiffAdded:
|
||||
cli.Println(color.GreenString("+ " + sqlfmt.FmtIndex(idxDiff.To)))
|
||||
cli.Println(color.GreenString("+ " + sqlfmt.FmtIndex(idxDiff.To)))
|
||||
case diff.SchDiffRemoved:
|
||||
cli.Println(color.RedString("- " + sqlfmt.FmtIndex(idxDiff.From)))
|
||||
cli.Println(color.RedString("- " + sqlfmt.FmtIndex(idxDiff.From)))
|
||||
case diff.SchDiffModified:
|
||||
cli.Println("< " + sqlfmt.FmtIndex(idxDiff.From))
|
||||
cli.Println("> " + sqlfmt.FmtIndex(idxDiff.To))
|
||||
cli.Println("< " + sqlfmt.FmtIndex(idxDiff.From))
|
||||
cli.Println("> " + sqlfmt.FmtIndex(idxDiff.To))
|
||||
}
|
||||
}
|
||||
|
||||
@@ -508,17 +510,17 @@ func tabularSchemaDiff(ctx context.Context, td diff.TableDelta, fromSchemas, toS
|
||||
switch fkDiff.DiffType {
|
||||
case diff.SchDiffNone:
|
||||
parentSch := toSchemas[fkDiff.To.ReferencedTableName]
|
||||
cli.Println(" " + sqlfmt.FmtForeignKey(fkDiff.To, toSch, parentSch))
|
||||
cli.Println(" " + sqlfmt.FmtForeignKey(fkDiff.To, toSch, parentSch))
|
||||
case diff.SchDiffAdded:
|
||||
parentSch := toSchemas[fkDiff.To.ReferencedTableName]
|
||||
cli.Println(color.GreenString("+ " + sqlfmt.FmtForeignKey(fkDiff.To, toSch, parentSch)))
|
||||
cli.Println(color.GreenString("+ " + sqlfmt.FmtForeignKey(fkDiff.To, toSch, parentSch)))
|
||||
case diff.SchDiffRemoved:
|
||||
parentSch := toSchemas[fkDiff.From.ReferencedTableName]
|
||||
cli.Println(color.RedString("- " + sqlfmt.FmtForeignKey(fkDiff.From, fromSch, parentSch)))
|
||||
cli.Println(color.RedString("- " + sqlfmt.FmtForeignKey(fkDiff.From, fromSch, parentSch)))
|
||||
case diff.SchDiffModified:
|
||||
fromParent, toParent := fromSchemas[fkDiff.From.ReferencedTableName], toSchemas[fkDiff.To.ReferencedTableName]
|
||||
cli.Println("< " + sqlfmt.FmtForeignKey(fkDiff.From, fromSch, fromParent))
|
||||
cli.Println("> " + sqlfmt.FmtForeignKey(fkDiff.To, toSch, toParent))
|
||||
cli.Println("< " + sqlfmt.FmtForeignKey(fkDiff.From, fromSch, fromParent))
|
||||
cli.Println("> " + sqlfmt.FmtForeignKey(fkDiff.To, toSch, toParent))
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -16,7 +16,6 @@ package diff
|
||||
|
||||
import (
|
||||
"context"
|
||||
"reflect"
|
||||
"sort"
|
||||
|
||||
"github.com/liquidata-inc/dolt/go/libraries/doltcore/doltdb"
|
||||
@@ -410,7 +409,7 @@ func (td TableDelta) IsRename() bool {
|
||||
}
|
||||
|
||||
func (td TableDelta) HasFKChanges() bool {
|
||||
return fkSlicesAreEqual(td.FromFks, td.ToFks)
|
||||
return !fkSlicesAreEqual(td.FromFks, td.ToFks)
|
||||
}
|
||||
|
||||
// GetSchemas returns the table's schema at the fromRoot and toRoot, or schema.Empty if the table did not exist.
|
||||
@@ -474,7 +473,7 @@ func fkSlicesAreEqual(from, to []doltdb.ForeignKey) bool {
|
||||
})
|
||||
|
||||
for i := range from {
|
||||
if !reflect.DeepEqual(from[i], to[i]) {
|
||||
if !from[i].DeepEquals(to[i]) {
|
||||
return false
|
||||
}
|
||||
}
|
||||
|
||||
@@ -110,6 +110,7 @@ func DiffSchIndexes(fromSch, toSch schema.Schema) (diffs []IndexDifference) {
|
||||
DiffType: SchDiffRemoved,
|
||||
From: fromIdx,
|
||||
})
|
||||
return false, nil
|
||||
}
|
||||
|
||||
d := IndexDifference{
|
||||
@@ -129,7 +130,7 @@ func DiffSchIndexes(fromSch, toSch schema.Schema) (diffs []IndexDifference) {
|
||||
_ = toSch.Indexes().Iter(func(toIdx schema.Index) (stop bool, err error) {
|
||||
// if we've seen this index, skip
|
||||
for _, d := range diffs {
|
||||
if d.To.Equals(toIdx) {
|
||||
if d.To != nil && d.To.Equals(toIdx) {
|
||||
return false, nil
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user