Skip to content

Commit

Permalink
Merge pull request #8297 from dolthub/macneale4/workspace-edit
Browse files Browse the repository at this point in the history
dolt_workspace_* update and delete support

This change adds the ability to update dolt_workspace_ tables. Updates can take two forms:
 1)The "staging" column of the table. may be toggled from it's current state. If setting from false to true, the working value will be written into the staged table. Setting from true to false will remove the row from staging, and leave the value in working as is.
 2) You can delete any row which has a "staged" column of false. This will revert the workspace changes and return them to the original value.
  • Loading branch information
macneale4 authored Aug 27, 2024
2 parents 0f3566d + a3fbbc2 commit 6c81204
Show file tree
Hide file tree
Showing 15 changed files with 1,049 additions and 55 deletions.
2 changes: 1 addition & 1 deletion go/libraries/doltcore/merge/schema_merge_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1882,7 +1882,7 @@ func makeRootWithTable(t *testing.T, ddb *doltdb.DoltDB, eo editor.Options, tbl
require.NoError(t, err)
noop := func(ctx *sql.Context, dbName string, root doltdb.RootValue) (err error) { return }
sess := writer.NewWriteSession(ddb.Format(), ws, gst, eo)
wr, err := sess.GetTableWriter(sql.NewContext(ctx), doltdb.TableName{Name: tbl.ns.name}, "test", noop)
wr, err := sess.GetTableWriter(sql.NewContext(ctx), doltdb.TableName{Name: tbl.ns.name}, "test", noop, false)
require.NoError(t, err)

sctx := sql.NewEmptyContext()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -764,7 +764,7 @@ func getTableWriter(ctx *sql.Context, engine *gms.Engine, tableName, databaseNam
ds := dsess.DSessFromSess(ctx.Session)
setter := ds.SetWorkingRoot

tableWriter, err := writeSession.GetTableWriter(ctx, doltdb.TableName{Name: tableName}, databaseName, setter)
tableWriter, err := writeSession.GetTableWriter(ctx, doltdb.TableName{Name: tableName}, databaseName, setter, false)
if err != nil {
return nil, nil, err
}
Expand Down
9 changes: 8 additions & 1 deletion go/libraries/doltcore/sqle/database.go
Original file line number Diff line number Diff line change
Expand Up @@ -399,11 +399,18 @@ func (db Database) getTableInsensitive(ctx *sql.Context, head *doltdb.Commit, ds
return dt, true, nil
case strings.HasPrefix(lwrName, doltdb.DoltWorkspaceTablePrefix):
sess := dsess.DSessFromSess(ctx.Session)

ws, err := sess.WorkingSet(ctx, db.RevisionQualifiedName())
if err != nil {
return nil, false, err
}

roots, _ := sess.GetRoots(ctx, db.RevisionQualifiedName())
head := roots.Head

userTable := tblName[len(doltdb.DoltWorkspaceTablePrefix):]

dt, err := dtables.NewWorkspaceTable(ctx, userTable, roots)
dt, err := dtables.NewWorkspaceTable(ctx, tblName, userTable, head, ws)
if err != nil {
return nil, false, err
}
Expand Down
22 changes: 22 additions & 0 deletions go/libraries/doltcore/sqle/dsess/session.go
Original file line number Diff line number Diff line change
Expand Up @@ -952,6 +952,28 @@ func (d *DoltSession) SetWorkingRoot(ctx *sql.Context, dbName string, newRoot do
return d.SetWorkingSet(ctx, dbName, existingWorkingSet.WithWorkingRoot(newRoot))
}

// SetStagingRoot sets the staging root for the session's current database. This is useful when editing the staged
// table without messing with the HEAD or working trees.
func (d *DoltSession) SetStagingRoot(ctx *sql.Context, dbName string, newRoot doltdb.RootValue) error {
branchState, _, err := d.lookupDbState(ctx, dbName)
if err != nil {
return err
}

existingWorkingSet := branchState.WorkingSet()
if existingWorkingSet == nil {
return doltdb.ErrOperationNotSupportedInDetachedHead
}
if rootsEqual(branchState.roots().Staged, newRoot) {
return nil
}

if branchState.readOnly {
return fmt.Errorf("cannot set root on read-only session")
}
return d.SetWorkingSet(ctx, dbName, existingWorkingSet.WithStagedRoot(newRoot))
}

// SetRoots sets new roots for the session for the database named. Typically, clients should only set the working root,
// via setRoot. This method is for clients that need to update more of the session state, such as the dolt_ functions.
// Unlike setting the working root, this method always marks the database state dirty.
Expand Down
2 changes: 1 addition & 1 deletion go/libraries/doltcore/sqle/dsess/table_writer.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ import (
// It's responsible for creating and managing the lifecycle of TableWriter's.
type WriteSession interface {
// GetTableWriter creates a TableWriter and adds it to the WriteSession.
GetTableWriter(ctx *sql.Context, table doltdb.TableName, db string, setter SessionRootSetter) (TableWriter, error)
GetTableWriter(ctx *sql.Context, table doltdb.TableName, db string, setter SessionRootSetter, targetStaging bool) (TableWriter, error)

// GetWorkingSet returns the session's current working set.
GetWorkingSet() *doltdb.WorkingSet
Expand Down
2 changes: 1 addition & 1 deletion go/libraries/doltcore/sqle/dtables/docs_table.go
Original file line number Diff line number Diff line change
Expand Up @@ -258,7 +258,7 @@ func (iw *docsWriter) StatementBegin(ctx *sql.Context) {
}

if ws := dbState.WriteSession(); ws != nil {
tableWriter, err := ws.GetTableWriter(ctx, doltdb.TableName{Name: doltdb.DocTableName}, dbName, dSess.SetWorkingRoot)
tableWriter, err := ws.GetTableWriter(ctx, doltdb.TableName{Name: doltdb.DocTableName}, dbName, dSess.SetWorkingRoot, false)
if err != nil {
iw.errDuringStatementBegin = err
return
Expand Down
2 changes: 1 addition & 1 deletion go/libraries/doltcore/sqle/dtables/ignore_table.go
Original file line number Diff line number Diff line change
Expand Up @@ -274,7 +274,7 @@ func (iw *ignoreWriter) StatementBegin(ctx *sql.Context) {
}

if ws := dbState.WriteSession(); ws != nil {
tableWriter, err := ws.GetTableWriter(ctx, doltdb.TableName{Name: doltdb.IgnoreTableName}, dbName, dSess.SetWorkingRoot)
tableWriter, err := ws.GetTableWriter(ctx, doltdb.TableName{Name: doltdb.IgnoreTableName}, dbName, dSess.SetWorkingRoot, false)
if err != nil {
iw.errDuringStatementBegin = err
return
Expand Down
Loading

0 comments on commit 6c81204

Please sign in to comment.