github.com/dolthub/dolt/go@v0.40.5-0.20240520175717-68db7794bea6/libraries/doltcore/sqle/dprocedures/dolt_undrop.go (about)

     1  // Copyright 2023 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 dprocedures
    16  
    17  import (
    18  	"fmt"
    19  
    20  	"github.com/dolthub/go-mysql-server/sql"
    21  
    22  	"github.com/dolthub/dolt/go/libraries/doltcore/sqle/dsess"
    23  	"github.com/dolthub/dolt/go/libraries/utils/errors"
    24  )
    25  
    26  // doltUndrop restores a database that has been dropped and moved to a temporary holding area.
    27  func doltUndrop(ctx *sql.Context, args ...string) (sql.RowIter, error) {
    28  	doltSession := dsess.DSessFromSess(ctx.Session)
    29  	provider := doltSession.Provider()
    30  
    31  	switch len(args) {
    32  	case 0:
    33  		availableDatabases, err := provider.ListDroppedDatabases(ctx)
    34  		if err != nil {
    35  			return nil, err
    36  		}
    37  		return nil, fmt.Errorf("no database name specified. %s", errors.CreateUndropErrorMessage(availableDatabases))
    38  
    39  	case 1:
    40  		if err := provider.UndropDatabase(ctx, args[0]); err != nil {
    41  			return nil, err
    42  		}
    43  		return rowToIter(int64(0)), nil
    44  
    45  	default:
    46  		return nil, fmt.Errorf("dolt_undrop called with too many arguments: " +
    47  			"dolt_undrop only accepts one argument - the name of the dropped database to restore")
    48  	}
    49  }