Merge pull request #5420 from dolthub/taylor/diff-summ-cli

Add new version of `--summary` option to `dolt diff`
This commit is contained in:
Taylor Bantle
2023-02-24 15:35:27 -08:00
committed by GitHub
6 changed files with 497 additions and 313 deletions

View File

@@ -23,6 +23,7 @@ import (
"strings"
"github.com/dolthub/go-mysql-server/sql"
"github.com/dolthub/go-mysql-server/sql/types"
"github.com/dolthub/dolt/go/cmd/dolt/cli"
"github.com/dolthub/dolt/go/cmd/dolt/commands/engine"
@@ -35,7 +36,9 @@ import (
"github.com/dolthub/dolt/go/libraries/doltcore/sqle"
"github.com/dolthub/dolt/go/libraries/doltcore/sqle/sqlfmt"
"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/libraries/utils/set"
)
@@ -47,6 +50,7 @@ const (
SchemaOnlyDiff diffPart = 1 // 0b0001
DataOnlyDiff diffPart = 2 // 0b0010
Stat diffPart = 4 // 0b0100
Summary diffPart = 8 // 0b1000
SchemaAndDataDiff = SchemaOnlyDiff | DataOnlyDiff
@@ -54,16 +58,17 @@ const (
SQLDiffOutput diffOutput = 2
JsonDiffOutput diffOutput = 3
DataFlag = "data"
SchemaFlag = "schema"
StatFlag = "stat"
whereParam = "where"
limitParam = "limit"
SQLFlag = "sql"
CachedFlag = "cached"
SkinnyFlag = "skinny"
MergeBase = "merge-base"
DiffMode = "diff-mode"
DataFlag = "data"
SchemaFlag = "schema"
StatFlag = "stat"
SummaryFlag = "summary"
whereParam = "where"
limitParam = "limit"
SQLFlag = "sql"
CachedFlag = "cached"
SkinnyFlag = "skinny"
MergeBase = "merge-base"
DiffMode = "diff-mode"
)
var diffDocs = cli.CommandDocumentationContent{
@@ -139,6 +144,7 @@ func (cmd DiffCmd) ArgParser() *argparser.ArgParser {
ap.SupportsFlag(DataFlag, "d", "Show only the data changes, do not show the schema changes (Both shown by default).")
ap.SupportsFlag(SchemaFlag, "s", "Show only the schema changes, do not show the data changes (Both shown by default).")
ap.SupportsFlag(StatFlag, "", "Show stats of data changes")
ap.SupportsFlag(SummaryFlag, "", "Show summary of data and schema changes")
ap.SupportsString(FormatFlag, "r", "result output format", "How to format diff output. Valid values are tabular, sql, json. Defaults to tabular.")
ap.SupportsString(whereParam, "", "column", "filters columns based on values in the diff. See {{.EmphasisLeft}}dolt diff --help{{.EmphasisRight}} for details.")
ap.SupportsInt(limitParam, "", "record_count", "limits to the first N diffs.")
@@ -173,9 +179,9 @@ func (cmd DiffCmd) Exec(ctx context.Context, commandStr string, args []string, d
}
func (cmd DiffCmd) validateArgs(apr *argparser.ArgParseResults) errhand.VerboseError {
if apr.Contains(StatFlag) {
if apr.Contains(StatFlag) || apr.Contains(SummaryFlag) {
if apr.Contains(SchemaFlag) || apr.Contains(DataFlag) {
return errhand.BuildDError("invalid Arguments: --stat cannot be combined with --schema or --data").Build()
return errhand.BuildDError("invalid Arguments: --stat and --summary cannot be combined with --schema or --data").Build()
}
}
@@ -199,6 +205,8 @@ func parseDiffArgs(ctx context.Context, dEnv *env.DoltEnv, apr *argparser.ArgPar
dArgs.diffParts = SchemaOnlyDiff
} else if apr.Contains(StatFlag) {
dArgs.diffParts = Stat
} else if apr.Contains(SummaryFlag) {
dArgs.diffParts = Summary
}
dArgs.skinny = apr.Contains(SkinnyFlag)
@@ -248,6 +256,10 @@ func parseDiffArgs(ctx context.Context, dEnv *env.DoltEnv, apr *argparser.ArgPar
if err != nil {
return nil, err
}
if ok {
dArgs.tableSet.Add(tableName)
continue
}
if !ok {
return nil, fmt.Errorf("table %s does not exist in either revision", tableName)
}
@@ -467,6 +479,41 @@ func maybeResolve(ctx context.Context, dEnv *env.DoltEnv, spec string) (*doltdb.
return root, true
}
var diffSummarySchema = sql.Schema{
&sql.Column{Name: "Table name", Type: types.Text, Nullable: false},
&sql.Column{Name: "Diff type", Type: types.Text, Nullable: false},
&sql.Column{Name: "Data change", Type: types.Boolean, Nullable: false},
&sql.Column{Name: "Schema change", Type: types.Boolean, Nullable: false},
}
func printDiffSummary(ctx context.Context, tds []diff.TableDelta, dArgs *diffArgs) errhand.VerboseError {
cliWR := iohelp.NopWrCloser(cli.OutStream)
wr := tabular.NewFixedWidthTableWriter(diffSummarySchema, cliWR, 100)
defer wr.Close(ctx)
for _, td := range tds {
if !dArgs.tableSet.Contains(td.FromName) && !dArgs.tableSet.Contains(td.ToName) {
continue
}
if td.FromTable == nil && td.ToTable == nil {
return errhand.BuildDError("error: both tables in tableDelta are nil").Build()
}
summ, err := td.GetSummary(ctx)
if err != nil {
return errhand.BuildDError("could not get table delta summary").AddCause(err).Build()
}
err = wr.WriteSqlRow(ctx, sql.Row{td.CurName(), summ.DiffType, summ.DataChange, summ.SchemaChange})
if err != nil {
return errhand.BuildDError("could not write table delta summary").AddCause(err).Build()
}
}
return nil
}
func diffUserTables(ctx context.Context, dEnv *env.DoltEnv, dArgs *diffArgs) errhand.VerboseError {
var err error
@@ -484,6 +531,10 @@ func diffUserTables(ctx context.Context, dEnv *env.DoltEnv, dArgs *diffArgs) err
return strings.Compare(tableDeltas[i].ToName, tableDeltas[j].ToName) < 0
})
if dArgs.diffParts&Summary != 0 {
return printDiffSummary(ctx, tableDeltas, dArgs)
}
dw, err := newDiffWriter(dArgs.diffOutput)
if err != nil {
return errhand.VerboseErrorFromError(err)
@@ -681,7 +732,6 @@ func diffRows(
}
fromSch = pkSch.Schema
}
if td.ToSch != nil {
pkSch, err := sqlutil.FromDoltSchema(td.ToName, td.ToSch)
if err != nil {

View File

@@ -57,6 +57,13 @@ type TableDelta struct {
FromFksParentSch map[string]schema.Schema
}
type TableDeltaSummary struct {
DiffType string
DataChange bool
SchemaChange bool
TableName string
}
// GetStagedUnstagedTableDeltas represents staged and unstaged changes as TableDelta slices.
func GetStagedUnstagedTableDeltas(ctx context.Context, roots doltdb.Roots) (staged, unstaged []TableDelta, err error) {
staged, err = GetTableDeltas(ctx, roots.Head, roots.Staged)
@@ -387,6 +394,81 @@ func (td TableDelta) IsKeyless(ctx context.Context) (bool, error) {
}
}
// isTableDataEmpty return true if the table does not contain any data
func isTableDataEmpty(ctx context.Context, table *doltdb.Table) (bool, error) {
rowData, err := table.GetRowData(ctx)
if err != nil {
return false, err
}
return rowData.Empty()
}
// GetSummary returns a summary of the table delta.
func (td TableDelta) GetSummary(ctx context.Context) (*TableDeltaSummary, error) {
// Dropping a table is always a schema change, and also a data change if the table contained data
if td.IsDrop() {
isEmpty, err := isTableDataEmpty(ctx, td.FromTable)
if err != nil {
return nil, err
}
return &TableDeltaSummary{
TableName: td.FromName,
DataChange: !isEmpty,
SchemaChange: true,
DiffType: "dropped",
}, nil
}
// Renaming a table is always a schema change, and also a data change if the table data differs
if td.IsRename() {
dataChanged, err := td.HasHashChanged()
if err != nil {
return nil, err
}
return &TableDeltaSummary{
TableName: td.ToName,
DataChange: dataChanged,
SchemaChange: true,
DiffType: "renamed",
}, nil
}
// Creating a table is always a schema change, and also a data change if data was inserted
if td.IsAdd() {
isEmpty, err := isTableDataEmpty(ctx, td.ToTable)
if err != nil {
return nil, err
}
return &TableDeltaSummary{
TableName: td.ToName,
DataChange: !isEmpty,
SchemaChange: true,
DiffType: "added",
}, nil
}
dataChanged, err := td.HasHashChanged()
if err != nil {
return nil, err
}
schemaChanged, err := td.HasSchemaChanged(ctx)
if err != nil {
return nil, err
}
return &TableDeltaSummary{
TableName: td.ToName,
DataChange: dataChanged,
SchemaChange: schemaChanged,
DiffType: "modified",
}, nil
}
// GetRowData returns the table's row data at the fromRoot and toRoot, or an empty map if the table did not exist.
func (td TableDelta) GetRowData(ctx context.Context) (from, to durable.Index, err error) {
if td.FromTable == nil && td.ToTable == nil {

View File

@@ -52,14 +52,6 @@ type UnscopedDiffTable struct {
commitCheck doltdb.CommitFilter
}
// tableChange is an internal data structure used to hold the results of processing
// a diff.TableDelta structure into the output data for this system table.
type tableChange struct {
tableName string
dataChange bool
schemaChange bool
}
// NewUnscopedDiffTable creates an UnscopedDiffTable
func NewUnscopedDiffTable(_ *sql.Context, dbName string, ddb *doltdb.DoltDB, head *doltdb.Commit) sql.Table {
return &UnscopedDiffTable{dbName: dbName, ddb: ddb, head: head}
@@ -241,20 +233,20 @@ func (d *doltDiffWorkingSetRowItr) Next(ctx *sql.Context) (sql.Row, error) {
return nil, io.EOF
}
change, err := processTableDelta(ctx, tableDelta)
change, err := tableDelta.GetSummary(ctx)
if err != nil {
return nil, err
}
sqlRow := sql.NewRow(
changeSet,
change.tableName,
change.TableName,
nil, // committer
nil, // email
nil, // date
nil, // message
change.dataChange,
change.schemaChange,
change.DataChange,
change.SchemaChange,
)
return sqlRow, nil
@@ -288,7 +280,7 @@ type doltDiffCommitHistoryRowItr struct {
commits []*doltdb.Commit
meta *datas.CommitMeta
hash hash.Hash
tableChanges []tableChange
tableChanges []diff.TableDeltaSummary
tableChangesIdx int
}
@@ -358,13 +350,13 @@ func (itr *doltDiffCommitHistoryRowItr) Next(ctx *sql.Context) (sql.Row, error)
return sql.NewRow(
h.String(),
tableChange.tableName,
tableChange.TableName,
meta.Name,
meta.Email,
meta.Time(),
meta.Description,
tableChange.dataChange,
tableChange.schemaChange,
tableChange.DataChange,
tableChange.SchemaChange,
), nil
}
@@ -399,7 +391,7 @@ func (itr *doltDiffCommitHistoryRowItr) loadTableChanges(ctx context.Context, co
// calculateTableChanges calculates the tables that changed in the specified commit, by comparing that
// commit with its immediate ancestor commit.
func (itr *doltDiffCommitHistoryRowItr) calculateTableChanges(ctx context.Context, commit *doltdb.Commit) ([]tableChange, error) {
func (itr *doltDiffCommitHistoryRowItr) calculateTableChanges(ctx context.Context, commit *doltdb.Commit) ([]diff.TableDeltaSummary, error) {
if len(commit.DatasParents()) == 0 {
return nil, nil
}
@@ -424,9 +416,9 @@ func (itr *doltDiffCommitHistoryRowItr) calculateTableChanges(ctx context.Contex
return nil, err
}
tableChanges := make([]tableChange, len(deltas))
tableChanges := make([]diff.TableDeltaSummary, len(deltas))
for i := 0; i < len(deltas); i++ {
change, err := processTableDelta(itr.ctx, deltas[i])
change, err := deltas[i].GetSummary(itr.ctx)
if err != nil {
return nil, err
}
@@ -442,68 +434,6 @@ func (itr *doltDiffCommitHistoryRowItr) calculateTableChanges(ctx context.Contex
return tableChanges, nil
}
// processTableDelta processes the specified TableDelta to determine what kind of change it was (i.e. table drop,
// table rename, table create, or data update) and returns a tableChange struct representing the change.
func processTableDelta(ctx *sql.Context, delta diff.TableDelta) (*tableChange, error) {
// Dropping a table is always a schema change, and also a data change if the table contained data
if delta.IsDrop() {
isEmpty, err := isTableDataEmpty(ctx, delta.FromTable)
if err != nil {
return nil, err
}
return &tableChange{
tableName: delta.FromName,
dataChange: !isEmpty,
schemaChange: true,
}, nil
}
// Renaming a table is always a schema change, and also a data change if the table data differs
if delta.IsRename() {
dataChanged, err := delta.HasHashChanged()
if err != nil {
return nil, err
}
return &tableChange{
tableName: delta.ToName,
dataChange: dataChanged,
schemaChange: true,
}, nil
}
// Creating a table is always a schema change, and also a data change if data was inserted
if delta.IsAdd() {
isEmpty, err := isTableDataEmpty(ctx, delta.ToTable)
if err != nil {
return nil, err
}
return &tableChange{
tableName: delta.ToName,
dataChange: !isEmpty,
schemaChange: true,
}, nil
}
dataChanged, err := delta.HasHashChanged()
if err != nil {
return nil, err
}
schemaChanged, err := delta.HasSchemaChanged(ctx)
if err != nil {
return nil, err
}
return &tableChange{
tableName: delta.ToName,
dataChange: dataChanged,
schemaChange: schemaChanged,
}, nil
}
// Close closes the iterator.
func (itr *doltDiffCommitHistoryRowItr) Close(*sql.Context) error {
return nil

View File

@@ -0,0 +1,341 @@
#!/usr/bin/env bats
load $BATS_TEST_DIRNAME/helper/common.bash
setup() {
setup_common
dolt sql <<SQL
CREATE TABLE test (
pk BIGINT NOT NULL COMMENT 'tag:0',
c1 BIGINT COMMENT 'tag:1',
c2 BIGINT COMMENT 'tag:2',
c3 BIGINT COMMENT 'tag:3',
c4 BIGINT COMMENT 'tag:4',
c5 BIGINT COMMENT 'tag:5',
PRIMARY KEY (pk)
);
SQL
}
teardown() {
assert_feature_version
teardown_common
}
@test "diff-stat: stat/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)"
dolt add test
dolt commit -m "table created"
dolt sql -q "insert into test values (2, 11, 0, 0, 0, 0)"
dolt sql -q "insert into test values (3, 11, 0, 0, 0, 0)"
run dolt diff --stat
[ "$status" -eq 0 ]
[[ "$output" =~ "2 Rows Unmodified (100.00%)" ]] || false
[[ "$output" =~ "2 Rows Added (100.00%)" ]] || false
[[ "$output" =~ "0 Rows Deleted (0.00%)" ]] || false
[[ "$output" =~ "0 Rows Modified (0.00%)" ]] || false
[[ "$output" =~ "12 Cells Added (100.00%)" ]] || false
[[ "$output" =~ "0 Cells Deleted (0.00%)" ]] || false
[[ "$output" =~ "0 Cells Modified (0.00%)" ]] || false
[[ "$output" =~ "(2 Row Entries vs 4 Row Entries)" ]] || false
run dolt diff --summary
[ "$status" -eq 0 ]
[[ "$output" =~ "| Table name | Diff type | Data change | Schema change |" ]] || false
[[ "$output" =~ "| test | modified | true | false |" ]] || false
dolt add test
dolt commit -m "added two rows"
dolt sql -q "replace into test values (0, 11, 0, 0, 0, 6)"
run dolt diff --stat
[ "$status" -eq 0 ]
[[ "$output" =~ "3 Rows Unmodified (75.00%)" ]] || false
[[ "$output" =~ "0 Rows Added (0.00%)" ]] || false
[[ "$output" =~ "0 Rows Deleted (0.00%)" ]] || false
[[ "$output" =~ "1 Row Modified (25.00%)" ]] || false
[[ "$output" =~ "0 Cells Added (0.00%)" ]] || false
[[ "$output" =~ "0 Cells Deleted (0.00%)" ]] || false
[[ "$output" =~ "2 Cells Modified (8.33%)" ]] || false
[[ "$output" =~ "(4 Row Entries vs 4 Row Entries)" ]] || false
run dolt diff --summary
[ "$status" -eq 0 ]
echo "$output"
[[ "$output" =~ "| Table name | Diff type | Data change | Schema change |" ]] || false
[[ "$output" =~ "| test | modified | true | false |" ]] || false
dolt add test
dolt commit -m "modified first row"
dolt sql -q "delete from test where pk = 0"
run dolt diff --stat
[ "$status" -eq 0 ]
[[ "$output" =~ "3 Rows Unmodified (75.00%)" ]] || false
[[ "$output" =~ "0 Rows Added (0.00%)" ]] || false
[[ "$output" =~ "1 Row Deleted (25.00%)" ]] || false
[[ "$output" =~ "0 Rows Modified (0.00%)" ]] || false
[[ "$output" =~ "0 Cells Added (0.00%)" ]] || false
[[ "$output" =~ "6 Cells Deleted (25.00%)" ]] || false
[[ "$output" =~ "0 Cells Modified (0.00%)" ]] || false
[[ "$output" =~ "(4 Row Entries vs 3 Row Entries)" ]] || false
run dolt diff --summary
[ "$status" -eq 0 ]
echo "$output"
[[ "$output" =~ "| Table name | Diff type | Data change | Schema change |" ]] || false
[[ "$output" =~ "| test | modified | true | false |" ]] || false
}
@test "diff-stat: stat/summary comparing row with a deleted cell and an added cell" {
run dolt diff --summary
[ "$status" -eq 0 ]
[[ "$output" =~ "| Table name | Diff type | Data change | Schema change |" ]] || false
[[ "$output" =~ "| test | added | false | true |" ]] || false
dolt add test
dolt commit -m "create table"
dolt sql -q "insert into test values (0, 1, 2, 3, 4, 5)"
dolt add test
dolt commit -m "put row"
dolt sql -q "replace into test (pk, c1, c3, c4, c5) values (0, 1, 3, 4, 5)"
run dolt diff --stat
[ "$status" -eq 0 ]
[[ "$output" =~ "0 Rows Unmodified (0.00%)" ]] || false
[[ "$output" =~ "0 Rows Added (0.00%)" ]] || false
[[ "$output" =~ "0 Rows Deleted (0.00%)" ]] || false
[[ "$output" =~ "1 Row Modified (100.00%)" ]] || false
[[ "$output" =~ "0 Cells Added (0.00%)" ]] || false
[[ "$output" =~ "0 Cells Deleted (0.00%)" ]] || false
[[ "$output" =~ "1 Cell Modified (16.67%)" ]] || false
[[ "$output" =~ "(1 Row Entry vs 1 Row Entry)" ]] || false
run dolt diff --summary
[ "$status" -eq 0 ]
[[ "$output" =~ "| Table name | Diff type | Data change | Schema change |" ]] || false
[[ "$output" =~ "| test | modified | true | false |" ]] || false
dolt add test
dolt commit -m "row modified"
dolt sql -q "replace into test values (0, 1, 2, 3, 4, 5)"
run dolt diff --stat
[ "$status" -eq 0 ]
[[ "$output" =~ "0 Rows Unmodified (0.00%)" ]] || false
[[ "$output" =~ "0 Rows Added (0.00%)" ]] || false
[[ "$output" =~ "0 Rows Deleted (0.00%)" ]] || false
[[ "$output" =~ "1 Row Modified (100.00%)" ]] || false
[[ "$output" =~ "0 Cells Added (0.00%)" ]] || false
[[ "$output" =~ "0 Cells Deleted (0.00%)" ]] || false
[[ "$output" =~ "1 Cell Modified (16.67%)" ]] || false
[[ "$output" =~ "(1 Row Entry vs 1 Row Entry)" ]] || false
run dolt diff --summary
[ "$status" -eq 0 ]
[[ "$output" =~ "| Table name | Diff type | Data change | Schema change |" ]] || false
[[ "$output" =~ "| test | modified | true | false |" ]] || false
}
@test "diff-stat: stat/summary comparing two branches" {
dolt checkout -b firstbranch
dolt sql -q "insert into test values (0, 0, 0, 0, 0, 0)"
dolt add test
dolt commit -m "Added one row"
dolt checkout -b newbranch
dolt sql -q "insert into test values (1, 1, 1, 1, 1, 1)"
dolt add test
dolt commit -m "Added another row"
run dolt diff --stat firstbranch newbranch
[ "$status" -eq 0 ]
[[ "$output" =~ "1 Row Unmodified (100.00%)" ]] || false
[[ "$output" =~ "1 Row Added (100.00%)" ]] || false
[[ "$output" =~ "0 Rows Deleted (0.00%)" ]] || false
[[ "$output" =~ "0 Rows Modified (0.00%)" ]] || false
[[ "$output" =~ "6 Cells Added (100.00%)" ]] || false
[[ "$output" =~ "0 Cells Deleted (0.00%)" ]] || false
[[ "$output" =~ "0 Cells Modified (0.00%)" ]] || false
[[ "$output" =~ "(1 Row Entry vs 2 Row Entries)" ]] || false
run dolt diff --summary firstbranch newbranch
[ "$status" -eq 0 ]
[[ "$output" =~ "| Table name | Diff type | Data change | Schema change |" ]] || false
[[ "$output" =~ "| test | modified | true | false |" ]] || false
run dolt diff --stat firstbranch..newbranch
[ "$status" -eq 0 ]
[[ "$output" =~ "1 Row Unmodified (100.00%)" ]] || false
[[ "$output" =~ "1 Row Added (100.00%)" ]] || false
[[ "$output" =~ "0 Rows Deleted (0.00%)" ]] || false
[[ "$output" =~ "0 Rows Modified (0.00%)" ]] || false
[[ "$output" =~ "6 Cells Added (100.00%)" ]] || false
[[ "$output" =~ "0 Cells Deleted (0.00%)" ]] || false
[[ "$output" =~ "0 Cells Modified (0.00%)" ]] || false
[[ "$output" =~ "(1 Row Entry vs 2 Row Entries)" ]] || false
run dolt diff --summary firstbranch..newbranch
[ "$status" -eq 0 ]
[[ "$output" =~ "| Table name | Diff type | Data change | Schema change |" ]] || false
[[ "$output" =~ "| test | modified | true | false |" ]] || false
}
@test "diff-stat: stat/summary shows correct changes after schema change" {
cat <<DELIM > employees.csv
"id","first name","last name","title","start date","end date"
0,tim,sehn,ceo,"",""
1,aaron,son,founder,"",""
2,brian,hendricks,founder,"",""
DELIM
dolt table import -c -pk=id employees employees.csv
dolt add employees
dolt commit -m "Added employees table with data"
dolt sql -q "alter table employees add city longtext"
dolt sql -q "insert into employees values (3, 'taylor', 'bantle', 'software engineer', '', '', 'Santa Monica')"
run dolt diff --stat
[ "$status" -eq 0 ]
[[ "$output" =~ "3 Rows Unmodified (100.00%)" ]] || false
[[ "$output" =~ "1 Row Added (33.33%)" ]] || false
[[ "$output" =~ "0 Rows Deleted (0.00%)" ]] || false
[[ "$output" =~ "0 Rows Modified (0.00%)" ]] || false
[[ "$output" =~ "10 Cells Added (55.56%)" ]] || false
[[ "$output" =~ "0 Cells Deleted (0.00%)" ]] || false
[[ "$output" =~ "0 Cells Modified (0.00%)" ]] || false
[[ "$output" =~ "(3 Row Entries vs 4 Row Entries)" ]] || false
run dolt diff --summary
[ "$status" -eq 0 ]
[[ "$output" =~ "| Table name | Diff type | Data change | Schema change |" ]] || false
[[ "$output" =~ "| employees | modified | true | true |" ]] || false
dolt sql -q "replace into employees values (0, 'tim', 'sehn', 'ceo', '2 years ago', '', 'Santa Monica')"
dolt diff --stat
run dolt diff --stat
[ "$status" -eq 0 ]
[[ "$output" =~ "2 Rows Unmodified (66.67%)" ]] || false
[[ "$output" =~ "1 Row Added (33.33%)" ]] || false
[[ "$output" =~ "0 Rows Deleted (0.00%)" ]] || false
[[ "$output" =~ "1 Row Modified (33.33%)" ]] || false
[[ "$output" =~ "10 Cells Added (55.56%)" ]] || false
[[ "$output" =~ "0 Cells Deleted (0.00%)" ]] || false
[[ "$output" =~ "2 Cells Modified (11.11%)" ]] || false
[[ "$output" =~ "(3 Row Entries vs 4 Row Entries)" ]] || false
run dolt diff --summary
[ "$status" -eq 0 ]
[[ "$output" =~ "| Table name | Diff type | Data change | Schema change |" ]] || false
[[ "$output" =~ "| employees | modified | true | true |" ]] || false
}
@test "diff-stat: stat/summary gets summaries for all tables with changes" {
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)"
dolt sql <<SQL
CREATE TABLE employees (
\`id\` varchar(20) NOT NULL,
\`first name\` LONGTEXT,
\`last name\` LONGTEXT,
\`title\` LONGTEXT,
\`start date\` LONGTEXT,
\`end date\` LONGTEXT,
PRIMARY KEY (id)
);
SQL
dolt sql -q "insert into employees values (0, 'tim', 'sehn', 'ceo', '', '')"
dolt add test employees
dolt commit -m "test tables created"
dolt sql -q "insert into test values (2, 11, 0, 0, 0, 0)"
dolt sql -q "insert into employees values (1, 'brian', 'hendriks', 'founder', '', '')"
run dolt diff --stat
[ "$status" -eq 0 ]
[[ "$output" =~ "diff --dolt a/test b/test" ]] || false
[[ "$output" =~ "--- a/test @" ]] || false
[[ "$output" =~ "+++ b/test @" ]] || false
[[ "$output" =~ "diff --dolt a/employees b/employees" ]] || false
[[ "$output" =~ "--- a/employees @" ]] || false
[[ "$output" =~ "+++ b/employees @" ]] || false
run dolt diff --summary
[ "$status" -eq 0 ]
[[ "$output" =~ "| Table name | Diff type | Data change | Schema change |" ]] || false
[[ "$output" =~ "| test | modified | true | false |" ]] || false
[[ "$output" =~ "| employees | modified | true | false |" ]] || false
run dolt diff --stat employees
[ "$status" -eq 0 ]
[[ "$output" =~ "diff --dolt a/employees b/employees" ]] || false
[[ "$output" =~ "--- a/employees @" ]] || false
[[ "$output" =~ "+++ b/employees @" ]] || false
run dolt diff --summary employees
[ "$status" -eq 0 ]
[[ "$output" =~ "| Table name | Diff type | Data change | Schema change |" ]] || false
[[ "$output" =~ "| employees | modified | true | false |" ]] || false
}
@test "diff-stat: two and three dot diff stat/summary" {
dolt checkout main
dolt sql -q 'insert into test values (0,0,0,0,0,0)'
dolt add .
dolt commit -m table
dolt checkout -b branch1
dolt sql -q 'insert into test values (1,1,1,1,1,1)'
dolt add .
dolt commit -m row
dolt checkout main
dolt sql -q 'insert into test values (2,2,2,2,2,2)'
dolt add .
dolt commit -m newrow
run dolt diff main..branch1 --stat
[ "$status" -eq 0 ]
[[ "$output" =~ "1 Row Unmodified (50.00%)" ]] || false
[[ "$output" =~ "1 Row Added (50.00%)" ]] || false
[[ "$output" =~ "1 Row Deleted (50.00%)" ]] || false
[[ "$output" =~ "0 Rows Modified (0.00%)" ]] || false
[[ "$output" =~ "6 Cells Added (50.00%)" ]] || false
[[ "$output" =~ "6 Cells Deleted (50.00%)" ]] || false
[[ "$output" =~ "0 Cells Modified (0.00%)" ]] || false
[[ "$output" =~ "(2 Row Entries vs 2 Row Entries)" ]] || false
run dolt diff main..branch1 --summary
[ "$status" -eq 0 ]
[[ "$output" =~ "| Table name | Diff type | Data change | Schema change |" ]] || false
[[ "$output" =~ "| test | modified | true | false |" ]] || false
run dolt diff main...branch1 --stat
echo $output
[ "$status" -eq 0 ]
[[ "$output" =~ "1 Row Unmodified (100.00%)" ]] || false
[[ "$output" =~ "1 Row Added (100.00%)" ]] || false
[[ "$output" =~ "0 Rows Deleted (0.00%)" ]] || false
[[ "$output" =~ "0 Rows Modified (0.00%)" ]] || false
[[ "$output" =~ "6 Cells Added (100.00%)" ]] || false
[[ "$output" =~ "0 Cells Deleted (0.00%)" ]] || false
[[ "$output" =~ "0 Cells Modified (0.00%)" ]] || false
[[ "$output" =~ "(1 Row Entry vs 2 Row Entries)" ]] || false
run dolt diff main...branch1 --summary
[ "$status" -eq 0 ]
[[ "$output" =~ "| Table name | Diff type | Data change | Schema change |" ]] || false
[[ "$output" =~ "| test | modified | true | false |" ]] || false
}
@test "diff-stat: diff stat incorrect primary key set change regression test" {
dolt sql -q "create table testdrop (col1 varchar(20), id int primary key, col2 varchar(20))"
dolt add .
dolt sql -q "insert into testdrop values ('test1', 1, 'test2')"
dolt commit -am "Add testdrop table"
dolt sql -q "alter table testdrop drop column col1"
run dolt diff --stat
[ $status -eq 0 ]
[[ $output =~ "1 Row Modified (100.00%)" ]]
}

View File

@@ -361,30 +361,6 @@ SQL
[ "$status" -eq 0 ]
[[ "$output" =~ "+ | 1" ]] || false
[[ ! "$output" =~ "- | 2" ]] || false
# Dots work with --stat
run dolt diff main..branch1 --stat
[ "$status" -eq 0 ]
[[ "$output" =~ "1 Row Unmodified (50.00%)" ]] || false
[[ "$output" =~ "1 Row Added (50.00%)" ]] || false
[[ "$output" =~ "1 Row Deleted (50.00%)" ]] || false
[[ "$output" =~ "0 Rows Modified (0.00%)" ]] || false
[[ "$output" =~ "6 Cells Added (50.00%)" ]] || false
[[ "$output" =~ "6 Cells Deleted (50.00%)" ]] || false
[[ "$output" =~ "0 Cells Modified (0.00%)" ]] || false
[[ "$output" =~ "(2 Row Entries vs 2 Row Entries)" ]] || false
run dolt diff main...branch1 --stat
echo $output
[ "$status" -eq 0 ]
[[ "$output" =~ "1 Row Unmodified (100.00%)" ]] || false
[[ "$output" =~ "1 Row Added (100.00%)" ]] || false
[[ "$output" =~ "0 Rows Deleted (0.00%)" ]] || false
[[ "$output" =~ "0 Rows Modified (0.00%)" ]] || false
[[ "$output" =~ "6 Cells Added (100.00%)" ]] || false
[[ "$output" =~ "0 Cells Deleted (0.00%)" ]] || false
[[ "$output" =~ "0 Cells Modified (0.00%)" ]] || false
[[ "$output" =~ "(1 Row Entry vs 2 Row Entries)" ]] || false
}
@test "diff: data and schema changes" {
@@ -774,189 +750,6 @@ SQL
[[ "$output" =~ '+ KEY `c2` (`c2`)' ]] || false
}
@test "diff: stat 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)"
dolt add test
dolt commit -m "table created"
dolt sql -q "insert into test values (2, 11, 0, 0, 0, 0)"
dolt sql -q "insert into test values (3, 11, 0, 0, 0, 0)"
dolt diff --stat
run dolt diff --stat
[ "$status" -eq 0 ]
[[ "$output" =~ "2 Rows Unmodified (100.00%)" ]] || false
[[ "$output" =~ "2 Rows Added (100.00%)" ]] || false
[[ "$output" =~ "0 Rows Deleted (0.00%)" ]] || false
[[ "$output" =~ "0 Rows Modified (0.00%)" ]] || false
[[ "$output" =~ "12 Cells Added (100.00%)" ]] || false
[[ "$output" =~ "0 Cells Deleted (0.00%)" ]] || false
[[ "$output" =~ "0 Cells Modified (0.00%)" ]] || false
[[ "$output" =~ "(2 Row Entries vs 4 Row Entries)" ]] || false
dolt add test
dolt commit -m "added two rows"
dolt sql -q "replace into test values (0, 11, 0, 0, 0, 6)"
run dolt diff --stat
[ "$status" -eq 0 ]
[[ "$output" =~ "3 Rows Unmodified (75.00%)" ]] || false
[[ "$output" =~ "0 Rows Added (0.00%)" ]] || false
[[ "$output" =~ "0 Rows Deleted (0.00%)" ]] || false
[[ "$output" =~ "1 Row Modified (25.00%)" ]] || false
[[ "$output" =~ "0 Cells Added (0.00%)" ]] || false
[[ "$output" =~ "0 Cells Deleted (0.00%)" ]] || false
[[ "$output" =~ "2 Cells Modified (8.33%)" ]] || false
[[ "$output" =~ "(4 Row Entries vs 4 Row Entries)" ]] || false
dolt add test
dolt commit -m "modified first row"
dolt sql -q "delete from test where pk = 0"
run dolt diff --stat
[ "$status" -eq 0 ]
[[ "$output" =~ "3 Rows Unmodified (75.00%)" ]] || false
[[ "$output" =~ "0 Rows Added (0.00%)" ]] || false
[[ "$output" =~ "1 Row Deleted (25.00%)" ]] || false
[[ "$output" =~ "0 Rows Modified (0.00%)" ]] || false
[[ "$output" =~ "0 Cells Added (0.00%)" ]] || false
[[ "$output" =~ "6 Cells Deleted (25.00%)" ]] || false
[[ "$output" =~ "0 Cells Modified (0.00%)" ]] || false
[[ "$output" =~ "(4 Row Entries vs 3 Row Entries)" ]] || false
}
@test "diff: stat comparing row with a deleted cell and an added cell" {
dolt add test
dolt commit -m "create table"
dolt sql -q "insert into test values (0, 1, 2, 3, 4, 5)"
dolt add test
dolt commit -m "put row"
dolt sql -q "replace into test (pk, c1, c3, c4, c5) values (0, 1, 3, 4, 5)"
run dolt diff --stat
[ "$status" -eq 0 ]
[[ "$output" =~ "0 Rows Unmodified (0.00%)" ]] || false
[[ "$output" =~ "0 Rows Added (0.00%)" ]] || false
[[ "$output" =~ "0 Rows Deleted (0.00%)" ]] || false
[[ "$output" =~ "1 Row Modified (100.00%)" ]] || false
[[ "$output" =~ "0 Cells Added (0.00%)" ]] || false
[[ "$output" =~ "0 Cells Deleted (0.00%)" ]] || false
[[ "$output" =~ "1 Cell Modified (16.67%)" ]] || false
[[ "$output" =~ "(1 Row Entry vs 1 Row Entry)" ]] || false
dolt add test
dolt commit -m "row modified"
dolt sql -q "replace into test values (0, 1, 2, 3, 4, 5)"
run dolt diff --stat
[ "$status" -eq 0 ]
[[ "$output" =~ "0 Rows Unmodified (0.00%)" ]] || false
[[ "$output" =~ "0 Rows Added (0.00%)" ]] || false
[[ "$output" =~ "0 Rows Deleted (0.00%)" ]] || false
[[ "$output" =~ "1 Row Modified (100.00%)" ]] || false
[[ "$output" =~ "0 Cells Added (0.00%)" ]] || false
[[ "$output" =~ "0 Cells Deleted (0.00%)" ]] || false
[[ "$output" =~ "1 Cell Modified (16.67%)" ]] || false
[[ "$output" =~ "(1 Row Entry vs 1 Row Entry)" ]] || false
}
@test "diff: stat comparing two branches" {
dolt checkout -b firstbranch
dolt sql -q "insert into test values (0, 0, 0, 0, 0, 0)"
dolt add test
dolt commit -m "Added one row"
dolt checkout -b newbranch
dolt sql -q "insert into test values (1, 1, 1, 1, 1, 1)"
dolt add test
dolt commit -m "Added another row"
run dolt diff --stat firstbranch newbranch
[ "$status" -eq 0 ]
[[ "$output" =~ "1 Row Unmodified (100.00%)" ]] || false
[[ "$output" =~ "1 Row Added (100.00%)" ]] || false
[[ "$output" =~ "0 Rows Deleted (0.00%)" ]] || false
[[ "$output" =~ "0 Rows Modified (0.00%)" ]] || false
[[ "$output" =~ "6 Cells Added (100.00%)" ]] || false
[[ "$output" =~ "0 Cells Deleted (0.00%)" ]] || false
[[ "$output" =~ "0 Cells Modified (0.00%)" ]] || false
[[ "$output" =~ "(1 Row Entry vs 2 Row Entries)" ]] || false
run dolt diff --stat firstbranch..newbranch
[ "$status" -eq 0 ]
[[ "$output" =~ "1 Row Unmodified (100.00%)" ]] || false
[[ "$output" =~ "1 Row Added (100.00%)" ]] || false
[[ "$output" =~ "0 Rows Deleted (0.00%)" ]] || false
[[ "$output" =~ "0 Rows Modified (0.00%)" ]] || false
[[ "$output" =~ "6 Cells Added (100.00%)" ]] || false
[[ "$output" =~ "0 Cells Deleted (0.00%)" ]] || false
[[ "$output" =~ "0 Cells Modified (0.00%)" ]] || false
[[ "$output" =~ "(1 Row Entry vs 2 Row Entries)" ]] || false
}
@test "diff: stat shows correct changes after schema change" {
cat <<DELIM > employees.csv
"id","first name","last name","title","start date","end date"
0,tim,sehn,ceo,"",""
1,aaron,son,founder,"",""
2,brian,hendricks,founder,"",""
DELIM
dolt table import -c -pk=id employees employees.csv
dolt add employees
dolt commit -m "Added employees table with data"
dolt sql -q "alter table employees add city longtext"
dolt sql -q "insert into employees values (3, 'taylor', 'bantle', 'software engineer', '', '', 'Santa Monica')"
dolt diff --stat
run dolt diff --stat
[ "$status" -eq 0 ]
[[ "$output" =~ "3 Rows Unmodified (100.00%)" ]] || false
[[ "$output" =~ "1 Row Added (33.33%)" ]] || false
[[ "$output" =~ "0 Rows Deleted (0.00%)" ]] || false
[[ "$output" =~ "0 Rows Modified (0.00%)" ]] || false
[[ "$output" =~ "10 Cells Added (55.56%)" ]] || false
[[ "$output" =~ "0 Cells Deleted (0.00%)" ]] || false
[[ "$output" =~ "0 Cells Modified (0.00%)" ]] || false
[[ "$output" =~ "(3 Row Entries vs 4 Row Entries)" ]] || false
dolt sql -q "replace into employees values (0, 'tim', 'sehn', 'ceo', '2 years ago', '', 'Santa Monica')"
dolt diff --stat
run dolt diff --stat
[ "$status" -eq 0 ]
[[ "$output" =~ "2 Rows Unmodified (66.67%)" ]] || false
[[ "$output" =~ "1 Row Added (33.33%)" ]] || false
[[ "$output" =~ "0 Rows Deleted (0.00%)" ]] || false
[[ "$output" =~ "1 Row Modified (33.33%)" ]] || false
[[ "$output" =~ "10 Cells Added (55.56%)" ]] || false
[[ "$output" =~ "0 Cells Deleted (0.00%)" ]] || false
[[ "$output" =~ "2 Cells Modified (11.11%)" ]] || false
[[ "$output" =~ "(3 Row Entries vs 4 Row Entries)" ]] || false
}
@test "diff: stat gets summaries for all tables with changes" {
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)"
dolt sql <<SQL
CREATE TABLE employees (
\`id\` varchar(20) NOT NULL,
\`first name\` LONGTEXT,
\`last name\` LONGTEXT,
\`title\` LONGTEXT,
\`start date\` LONGTEXT,
\`end date\` LONGTEXT,
PRIMARY KEY (id)
);
SQL
dolt sql -q "insert into employees values (0, 'tim', 'sehn', 'ceo', '', '')"
dolt add test employees
dolt commit -m "test tables created"
dolt sql -q "insert into test values (2, 11, 0, 0, 0, 0)"
dolt sql -q "insert into employees values (1, 'brian', 'hendriks', 'founder', '', '')"
run dolt diff --stat
[ "$status" -eq 0 ]
[[ "$output" =~ "diff --dolt a/test b/test" ]] || false
[[ "$output" =~ "--- a/test @" ]] || false
[[ "$output" =~ "+++ b/test @" ]] || false
[[ "$output" =~ "diff --dolt a/employees b/employees" ]] || false
[[ "$output" =~ "--- a/employees @" ]] || false
[[ "$output" =~ "+++ b/employees @" ]] || false
}
@test "diff: with where clause" {
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)"
@@ -1047,18 +840,6 @@ SQL
[[ "$output" =~ "where pk=4" ]] || false
}
@test "diff: diff stat incorrect primary key set change regression test" {
dolt sql -q "create table testdrop (col1 varchar(20), id int primary key, col2 varchar(20))"
dolt add .
dolt sql -q "insert into testdrop values ('test1', 1, 'test2')"
dolt commit -am "Add testdrop table"
dolt sql -q "alter table testdrop drop column col1"
run dolt diff --stat
[ $status -eq 0 ]
[[ $output =~ "1 Row Modified (100.00%)" ]]
}
@test "diff: with where clause errors" {
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)"