github.com/hasnat/dolt/go@v0.0.0-20210628190320-9eb5d843fbb7/libraries/doltcore/sqle/dfunctions/dolt_reset.go (about)

     1  // Copyright 2020 Dolthub, Inc.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package dfunctions
    16  
    17  import (
    18  	"fmt"
    19  	"strings"
    20  
    21  	"github.com/dolthub/go-mysql-server/sql"
    22  
    23  	"github.com/dolthub/dolt/go/cmd/dolt/cli"
    24  	"github.com/dolthub/dolt/go/libraries/doltcore/env"
    25  	"github.com/dolthub/dolt/go/libraries/doltcore/env/actions"
    26  	"github.com/dolthub/dolt/go/libraries/doltcore/sqle"
    27  )
    28  
    29  const DoltResetFuncName = "dolt_reset"
    30  
    31  type DoltResetFunc struct {
    32  	children []sql.Expression
    33  }
    34  
    35  func (d DoltResetFunc) Eval(ctx *sql.Context, row sql.Row) (interface{}, error) {
    36  	dbName := ctx.GetCurrentDatabase()
    37  
    38  	if len(dbName) == 0 {
    39  		return 1, fmt.Errorf("Empty database name.")
    40  	}
    41  
    42  	dSess := sqle.DSessFromSess(ctx.Session)
    43  	dbData, ok := dSess.GetDbData(dbName)
    44  
    45  	if !ok {
    46  		return 1, fmt.Errorf("Could not load database %s", dbName)
    47  	}
    48  
    49  	ap := cli.CreateResetArgParser()
    50  	args, err := getDoltArgs(ctx, row, d.Children())
    51  
    52  	if err != nil {
    53  		return 1, err
    54  	}
    55  
    56  	apr, err := ap.Parse(args)
    57  	if err != nil {
    58  		return 1, err
    59  	}
    60  
    61  	// Check if problems with args first.
    62  	if apr.ContainsAll(cli.HardResetParam, cli.SoftResetParam) {
    63  		return 1, fmt.Errorf("error: --%s and --%s are mutually exclusive options.", cli.HardResetParam, cli.SoftResetParam)
    64  	}
    65  
    66  	// Get all the needed roots.
    67  	working, staged, head, err := env.GetRoots(ctx, dbData.Ddb, dbData.Rsr)
    68  	if err != nil {
    69  		return 1, err
    70  	}
    71  
    72  	if apr.Contains(cli.HardResetParam) {
    73  		// Get the commitSpec for the branch if it exists
    74  		arg := ""
    75  		if apr.NArg() > 1 {
    76  			return 1, fmt.Errorf("--hard supports at most one additional param")
    77  		} else if apr.NArg() == 1 {
    78  			arg = apr.Arg(0)
    79  		}
    80  
    81  		h, err := actions.ResetHardTables(ctx, dbData, arg, working, staged, head)
    82  		if err != nil {
    83  			return 1, err
    84  		}
    85  
    86  		// In this case we preserve untracked tables.
    87  		if h == "" {
    88  			headHash, err := dbData.Rsr.CWBHeadHash(ctx)
    89  			if err != nil {
    90  				return 1, err
    91  			}
    92  
    93  			h = headHash.String()
    94  			if err := ctx.SetSessionVariable(ctx, sqle.HeadKey(dbName), h); err != nil {
    95  				return 1, err
    96  			}
    97  
    98  			workingHash := dbData.Rsr.WorkingHash()
    99  			if err := ctx.SetSessionVariable(ctx, sqle.WorkingKey(dbName), workingHash.String()); err != nil {
   100  				return 1, err
   101  			}
   102  		} else {
   103  			if err := setHeadAndWorkingSessionRoot(ctx, h); err != nil {
   104  				return 1, err
   105  			}
   106  		}
   107  	} else {
   108  		_, err = actions.ResetSoftTables(ctx, dbData, apr, staged, head)
   109  		if err != nil {
   110  			return 1, err
   111  		}
   112  	}
   113  
   114  	return 0, nil
   115  }
   116  
   117  func (d DoltResetFunc) Resolved() bool {
   118  	for _, child := range d.Children() {
   119  		if !child.Resolved() {
   120  			return false
   121  		}
   122  	}
   123  	return true
   124  }
   125  
   126  func (d DoltResetFunc) String() string {
   127  	childrenStrings := make([]string, len(d.children))
   128  
   129  	for i, child := range d.children {
   130  		childrenStrings[i] = child.String()
   131  	}
   132  
   133  	return fmt.Sprintf("DOLT_RESET(%s)", strings.Join(childrenStrings, ","))
   134  }
   135  
   136  func (d DoltResetFunc) Type() sql.Type {
   137  	return sql.Int8
   138  }
   139  
   140  func (d DoltResetFunc) IsNullable() bool {
   141  	for _, child := range d.Children() {
   142  		if child.IsNullable() {
   143  			return true
   144  		}
   145  	}
   146  	return false
   147  }
   148  
   149  func (d DoltResetFunc) Children() []sql.Expression {
   150  	return d.children
   151  }
   152  
   153  func (d DoltResetFunc) WithChildren(ctx *sql.Context, children ...sql.Expression) (sql.Expression, error) {
   154  	return NewDoltResetFunc(ctx, children...)
   155  }
   156  
   157  func NewDoltResetFunc(ctx *sql.Context, args ...sql.Expression) (sql.Expression, error) {
   158  	return DoltResetFunc{children: args}, nil
   159  }