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

Bug fix: dolt_commit_diff support for detached head mode #8566

Merged
merged 1 commit into from
Nov 15, 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
23 changes: 20 additions & 3 deletions go/libraries/doltcore/sqle/database.go
Original file line number Diff line number Diff line change
Expand Up @@ -357,11 +357,16 @@ func (db Database) getTableInsensitive(ctx *sql.Context, head *doltdb.Commit, ds
}
}

ws, err := ds.WorkingSet(ctx, db.RevisionQualifiedName())
if err != nil {
// Grab the staged root, if we have a valid working set, so we can show the staged changes
// in the system table, too. If we're in a detached head mode, just reuse the working root.
stagedRoot, err := workingSetStagedRoot(ctx, db.RevisionQualifiedName())
if err == doltdb.ErrOperationNotSupportedInDetachedHead {
stagedRoot = root
} else if err != nil {
return nil, false, err
}
dt, err := dtables.NewCommitDiffTable(ctx, db.Name(), tname, db.ddb, root, ws.StagedRoot())

dt, err := dtables.NewCommitDiffTable(ctx, db.Name(), tname, db.ddb, root, stagedRoot)
if err != nil {
return nil, false, err
}
Expand Down Expand Up @@ -702,6 +707,18 @@ func (db Database) getTableInsensitive(ctx *sql.Context, head *doltdb.Commit, ds
return resolveOverriddenNonexistentTable(ctx, tblName, db)
}

// workingSetStagedRoot returns the staged root for the current session in the database
// named |dbName|. If a working set is not available (e.g. if a commit or tag is checked
// out), this function returns an ErrOperationNotSupportedInDetachedHead error.
func workingSetStagedRoot(ctx *sql.Context, dbName string) (doltdb.RootValue, error) {
ds := dsess.DSessFromSess(ctx.Session)
ws, err := ds.WorkingSet(ctx, dbName)
if err != nil {
return nil, err
}
return ws.StagedRoot(), nil
}

// resolveAsOf resolves given expression to a commit, if one exists.
func resolveAsOf(ctx *sql.Context, db Database, asOf interface{}) (*doltdb.Commit, doltdb.RootValue, error) {
head, err := db.rsr.CWBHeadRef()
Expand Down
29 changes: 29 additions & 0 deletions go/libraries/doltcore/sqle/enginetest/dolt_queries_diff.go
Original file line number Diff line number Diff line change
Expand Up @@ -4984,6 +4984,35 @@ var CommitDiffSystemTableScriptTests = []queries.ScriptTest{
},
},
},
{
// When in a detached head mode, dolt_commit_diff should still work, even though it doesn't have a staged root
Name: "detached head",
SetUpScript: []string{
"CREATE TABLE t (pk int primary key, c1 varchar(100));",
"CALL dolt_commit('-Am', 'create table t');",
"SET @commit1 = hashof('HEAD');",
"INSERT INTO t VALUES (1, 'one');",
"CALL dolt_commit('-Am', 'insert 1');",
"SET @commit2 = hashof('HEAD');",
"CALL dolt_tag('v1', @commit2);",
},
Assertions: []queries.ScriptTestAssertion{
{
Query: "use mydb/v1;",
Expected: []sql.Row{},
},
{
// With no working set, this query should still compute the diff between two commits
Query: "SELECT COUNT(*) AS table_diff_num FROM dolt_commit_diff_t WHERE from_commit=@commit1 AND to_commit=@commit2;",
Expected: []sql.Row{{1}},
},
{
// With no working set, STAGED should reference the current root of the checked out tag
Query: "SELECT COUNT(*) AS table_diff_num FROM dolt_commit_diff_t WHERE from_commit=@commit1 AND to_commit='STAGED';",
Expected: []sql.Row{{1}},
},
},
},

{
// When a column is dropped and recreated with a different type, we expect only the new column
Expand Down
Loading