Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

adding --name-only option for dolt diff #7802

Merged
merged 8 commits into from
Apr 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions go/cmd/dolt/cli/cli_context.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ package cli
import (
"context"
"errors"

"github.com/dolthub/go-mysql-server/sql"

"github.com/dolthub/dolt/go/cmd/dolt/errhand"
Expand Down
52 changes: 35 additions & 17 deletions go/cmd/dolt/commands/diff.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,27 +48,29 @@ type diffOutput int
type diffPart int

const (
SchemaOnlyDiff diffPart = 1 // 0b0001
DataOnlyDiff diffPart = 2 // 0b0010
Stat diffPart = 4 // 0b0100
Summary diffPart = 8 // 0b1000
SchemaOnlyDiff diffPart = 1 // 0b0000 0001
DataOnlyDiff diffPart = 2 // 0b0000 0010
NameOnlyDiff diffPart = 4 // 0b0000 0100
Stat diffPart = 8 // 0b0000 1000
Summary diffPart = 16 // 0b0001 0000

SchemaAndDataDiff = SchemaOnlyDiff | DataOnlyDiff

TabularDiffOutput diffOutput = 1
SQLDiffOutput diffOutput = 2
JsonDiffOutput diffOutput = 3

DataFlag = "data"
SchemaFlag = "schema"
StatFlag = "stat"
SummaryFlag = "summary"
whereParam = "where"
limitParam = "limit"
SkinnyFlag = "skinny"
MergeBase = "merge-base"
DiffMode = "diff-mode"
ReverseFlag = "reverse"
DataFlag = "data"
SchemaFlag = "schema"
NameOnlyFlag = "name-only"
StatFlag = "stat"
SummaryFlag = "summary"
whereParam = "where"
limitParam = "limit"
SkinnyFlag = "skinny"
MergeBase = "merge-base"
DiffMode = "diff-mode"
ReverseFlag = "reverse"
)

var diffDocs = cli.CommandDocumentationContent{
Expand Down Expand Up @@ -174,6 +176,7 @@ func (cmd DiffCmd) ArgParser() *argparser.ArgParser {
ap.SupportsFlag(MergeBase, "", "Uses merge base of the first commit and second commit (or HEAD if not supplied) as the first commit")
ap.SupportsString(DiffMode, "", "diff mode", "Determines how to display modified rows with tabular output. Valid values are row, line, in-place, context. Defaults to context.")
ap.SupportsFlag(ReverseFlag, "R", "Reverses the direction of the diff.")
ap.SupportsFlag(NameOnlyFlag, "", "Only shows table names.")
return ap
}

Expand Down Expand Up @@ -216,6 +219,12 @@ func (cmd DiffCmd) validateArgs(apr *argparser.ArgParseResults) errhand.VerboseE
}
}

if apr.Contains(NameOnlyFlag) {
if apr.Contains(SchemaFlag) || apr.Contains(DataFlag) || apr.Contains(StatFlag) || apr.Contains(SummaryFlag) {
return errhand.BuildDError("invalid Arguments: --name-only cannot be combined with --schema, --data, --stat, or --summary").Build()
}
}

f, _ := apr.GetValue(FormatFlag)
switch strings.ToLower(f) {
case "tabular", "sql", "json", "":
Expand All @@ -238,6 +247,8 @@ func parseDiffDisplaySettings(apr *argparser.ArgParseResults) *diffDisplaySettin
displaySettings.diffParts = Stat
} else if apr.Contains(SummaryFlag) {
displaySettings.diffParts = Summary
} else if apr.Contains(NameOnlyFlag) {
displaySettings.diffParts = NameOnlyDiff
}

displaySettings.skinny = apr.Contains(SkinnyFlag)
Expand Down Expand Up @@ -1034,9 +1045,11 @@ func diffUserTable(
fromTable := tableSummary.FromTableName
toTable := tableSummary.ToTableName

err := dw.BeginTable(tableSummary.FromTableName, tableSummary.ToTableName, tableSummary.IsAdd(), tableSummary.IsDrop())
if err != nil {
return errhand.VerboseErrorFromError(err)
if dArgs.diffParts&NameOnlyDiff == 0 {
err := dw.BeginTable(tableSummary.FromTableName, tableSummary.ToTableName, tableSummary.IsAdd(), tableSummary.IsDrop())
if err != nil {
return errhand.VerboseErrorFromError(err)
}
}

var fromTableInfo, toTableInfo *diff.TableInfo
Expand All @@ -1055,6 +1068,11 @@ func diffUserTable(
tableName = toTable
}

if dArgs.diffParts&NameOnlyDiff != 0 {
cli.Println(tableName)
return errhand.VerboseErrorFromError(nil)
}

if dArgs.diffParts&Stat != 0 {
var areTablesKeyless = false

Expand Down
52 changes: 52 additions & 0 deletions integration-tests/bats/diff.bats
Original file line number Diff line number Diff line change
Expand Up @@ -1709,3 +1709,55 @@ SQL
[[ "$output" =~ "CREATE TRIGGER trigger1 BEFORE INSERT ON mytable FOR EACH ROW SET new.v1 = -2*new.v1;" ]] || false
[[ "$output" =~ "CREATE VIEW view1 AS SELECT v1 FROM mytable;" ]] || false
}

@test "diff: table-only option" {
dolt sql <<SQL
create table t1 (i int);
create table t2 (i int);
create table t3 (i int);
SQL

dolt add .
dolt commit -m "commit 1"

dolt sql <<SQL
drop table t1;
alter table t2 add column j int;
insert into t3 values (1);
create table t4 (i int);
SQL

run dolt diff --summary
[ $status -eq 0 ]
[ "${lines[0]}" = "+------------+-----------+-------------+---------------+" ]
[ "${lines[1]}" = "| Table name | Diff type | Data change | Schema change |" ]
[ "${lines[2]}" = "+------------+-----------+-------------+---------------+" ]
[ "${lines[3]}" = "| t1 | dropped | false | true |" ]
[ "${lines[4]}" = "| t2 | modified | false | true |" ]
[ "${lines[5]}" = "| t3 | modified | true | false |" ]
[ "${lines[6]}" = "| t4 | added | false | true |" ]
[ "${lines[7]}" = "+------------+-----------+-------------+---------------+" ]

run dolt diff --name-only
[ $status -eq 0 ]
[ "${lines[0]}" = "t1" ]
[ "${lines[1]}" = "t2" ]
[ "${lines[2]}" = "t3" ]
[ "${lines[3]}" = "t4" ]

run dolt diff --name-only --schema
[ $status -eq 1 ]
[[ $output =~ "invalid Arguments" ]] || false

run dolt diff --name-only --data
[ $status -eq 1 ]
[[ $output =~ "invalid Arguments" ]] || false

run dolt diff --name-only --stat
[ $status -eq 1 ]
[[ $output =~ "invalid Arguments" ]] || false

run dolt diff --name-only --summary
[ $status -eq 1 ]
[[ $output =~ "invalid Arguments" ]] || false
}
Loading