github.com/hasnat/dolt/go@v0.0.0-20210628190320-9eb5d843fbb7/libraries/doltcore/env/actions/table.go (about)

     1  // Copyright 2019 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 actions
    16  
    17  import (
    18  	"context"
    19  
    20  	"github.com/dolthub/dolt/go/libraries/doltcore/ref"
    21  
    22  	"github.com/dolthub/dolt/go/libraries/doltcore/diff"
    23  	"github.com/dolthub/dolt/go/libraries/doltcore/doltdb"
    24  	"github.com/dolthub/dolt/go/libraries/utils/set"
    25  )
    26  
    27  // MoveTablesBetweenRoots copies tables with names in tbls from the src RootValue to the dest RootValue.
    28  // It matches tables between roots by column tags.
    29  func MoveTablesBetweenRoots(ctx context.Context, tbls []string, src, dest *doltdb.RootValue) (*doltdb.RootValue, error) {
    30  	tblSet := set.NewStrSet(tbls)
    31  
    32  	stagedFKs, err := dest.GetForeignKeyCollection(ctx)
    33  	if err != nil {
    34  		return nil, err
    35  	}
    36  
    37  	tblDeltas, err := diff.GetTableDeltas(ctx, dest, src)
    38  	if err != nil {
    39  		return nil, err
    40  	}
    41  
    42  	tblsToDrop := set.NewStrSet(nil)
    43  
    44  	for _, td := range tblDeltas {
    45  		if td.IsDrop() {
    46  			if !tblSet.Contains(td.FromName) {
    47  				continue
    48  			}
    49  
    50  			tblsToDrop.Add(td.FromName)
    51  			stagedFKs.RemoveKeys(td.FromFks...)
    52  		}
    53  	}
    54  	for _, td := range tblDeltas {
    55  		if !td.IsDrop() {
    56  			if !tblSet.Contains(td.ToName) {
    57  				continue
    58  			}
    59  
    60  			if td.IsRename() {
    61  				// rename table before adding the new version so we don't have
    62  				// two copies of the same table
    63  				dest, err = dest.RenameTable(ctx, td.FromName, td.ToName)
    64  				if err != nil {
    65  					return nil, err
    66  				}
    67  			}
    68  
    69  			dest, err = dest.PutTable(ctx, td.ToName, td.ToTable)
    70  			if err != nil {
    71  				return nil, err
    72  			}
    73  
    74  			stagedFKs.RemoveKeys(td.FromFks...)
    75  			err = stagedFKs.AddKeys(td.ToFks...)
    76  			if err != nil {
    77  				return nil, err
    78  			}
    79  
    80  			ss, _, err := src.GetSuperSchema(ctx, td.ToName)
    81  			if err != nil {
    82  				return nil, err
    83  			}
    84  
    85  			dest, err = dest.PutSuperSchema(ctx, td.ToName, ss)
    86  			if err != nil {
    87  				return nil, err
    88  			}
    89  		}
    90  	}
    91  
    92  	dest, err = dest.PutForeignKeyCollection(ctx, stagedFKs)
    93  	if err != nil {
    94  		return nil, err
    95  	}
    96  
    97  	// RemoveTables also removes that table's ForeignKeys
    98  	dest, err = dest.RemoveTables(ctx, tblsToDrop.AsSlice()...)
    99  	if err != nil {
   100  		return nil, err
   101  	}
   102  
   103  	return dest, nil
   104  }
   105  
   106  func validateTablesExist(ctx context.Context, currRoot *doltdb.RootValue, unknown []string) error {
   107  	notExist := []string{}
   108  	for _, tbl := range unknown {
   109  		if has, err := currRoot.HasTable(ctx, tbl); err != nil {
   110  			return err
   111  		} else if !has {
   112  			notExist = append(notExist, tbl)
   113  		}
   114  	}
   115  
   116  	if len(notExist) > 0 {
   117  		return NewTblNotExistError(notExist)
   118  	}
   119  
   120  	return nil
   121  }
   122  
   123  // RemoveDocsTable takes a slice of table names and returns a new slice with DocTableName removed.
   124  func RemoveDocsTable(tbls []string) []string {
   125  	var result []string
   126  	for _, tblName := range tbls {
   127  		if tblName != doltdb.DocTableName {
   128  			result = append(result, tblName)
   129  		}
   130  	}
   131  	return result
   132  }
   133  
   134  // GetRemoteBranchRef returns the ref of a branch and ensures it matched with name. It will also return boolean value
   135  // representing whether there is not match or not and an error if there is one.
   136  func GetRemoteBranchRef(ctx context.Context, ddb *doltdb.DoltDB, name string) (ref.DoltRef, bool, error) {
   137  	remoteRefFilter := map[ref.RefType]struct{}{ref.RemoteRefType: {}}
   138  	refs, err := ddb.GetRefsOfType(ctx, remoteRefFilter)
   139  
   140  	if err != nil {
   141  		return nil, false, err
   142  	}
   143  
   144  	for _, rf := range refs {
   145  		if remRef, ok := rf.(ref.RemoteRef); ok && remRef.GetBranch() == name {
   146  			return rf, true, nil
   147  		}
   148  	}
   149  
   150  	return nil, false, nil
   151  }