github.com/dolthub/dolt/go@v0.40.5-0.20240520175717-68db7794bea6/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/schema"
    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  	// We want to include all Full-Text tables for every move
    43  	for _, td := range tblDeltas {
    44  		var ftIndexes []schema.Index
    45  		if tblSet.Contains(td.ToName) && td.ToSch.Indexes().ContainsFullTextIndex() {
    46  			for _, idx := range td.ToSch.Indexes().AllIndexes() {
    47  				if !idx.IsFullText() {
    48  					continue
    49  				}
    50  				ftIndexes = append(ftIndexes, idx)
    51  			}
    52  		} else if tblSet.Contains(td.FromName) && td.FromSch.Indexes().ContainsFullTextIndex() {
    53  			for _, idx := range td.FromSch.Indexes().AllIndexes() {
    54  				if !idx.IsFullText() {
    55  					continue
    56  				}
    57  				ftIndexes = append(ftIndexes, idx)
    58  			}
    59  		}
    60  		for _, ftIndex := range ftIndexes {
    61  			props := ftIndex.FullTextProperties()
    62  			tblSet.Add(
    63  				props.ConfigTable,
    64  				props.PositionTable,
    65  				props.DocCountTable,
    66  				props.GlobalCountTable,
    67  				props.RowCountTable,
    68  			)
    69  		}
    70  	}
    71  
    72  	tblsToDrop := set.NewStrSet(nil)
    73  
    74  	for _, td := range tblDeltas {
    75  		if td.IsDrop() {
    76  			if !tblSet.Contains(td.FromName) {
    77  				continue
    78  			}
    79  
    80  			tblsToDrop.Add(td.FromName)
    81  			stagedFKs.RemoveKeys(td.FromFks...)
    82  		}
    83  	}
    84  	for _, td := range tblDeltas {
    85  		if !td.IsDrop() {
    86  			if !tblSet.Contains(td.ToName) {
    87  				continue
    88  			}
    89  
    90  			if td.IsRename() {
    91  				// rename table before adding the new version so we don't have
    92  				// two copies of the same table
    93  				dest, err = dest.RenameTable(ctx, td.FromName, td.ToName)
    94  				if err != nil {
    95  					return nil, err
    96  				}
    97  			}
    98  
    99  			dest, err = dest.PutTable(ctx, doltdb.TableName{Name: td.ToName}, td.ToTable)
   100  			if err != nil {
   101  				return nil, err
   102  			}
   103  
   104  			stagedFKs.RemoveKeys(td.FromFks...)
   105  			err = stagedFKs.AddKeys(td.ToFks...)
   106  			if err != nil {
   107  				return nil, err
   108  			}
   109  		}
   110  	}
   111  
   112  	dest, err = dest.PutForeignKeyCollection(ctx, stagedFKs)
   113  	if err != nil {
   114  		return nil, err
   115  	}
   116  
   117  	// RemoveTables also removes that table's ForeignKeys
   118  	dest, err = dest.RemoveTables(ctx, false, false, tblsToDrop.AsSlice()...)
   119  	if err != nil {
   120  		return nil, err
   121  	}
   122  
   123  	return dest, nil
   124  }
   125  
   126  func validateTablesExist(ctx context.Context, currRoot doltdb.RootValue, unknown []string) error {
   127  	notExist := []string{}
   128  	for _, tbl := range unknown {
   129  		if has, err := currRoot.HasTable(ctx, tbl); err != nil {
   130  			return err
   131  		} else if !has {
   132  			notExist = append(notExist, tbl)
   133  		}
   134  	}
   135  
   136  	if len(notExist) > 0 {
   137  		return NewTblNotExistError(notExist)
   138  	}
   139  
   140  	return nil
   141  }
   142  
   143  // RemoveDocsTable takes a slice of table names and returns a new slice with DocTableName removed.
   144  func RemoveDocsTable(tbls []string) []string {
   145  	var result []string
   146  	for _, tblName := range tbls {
   147  		if tblName != doltdb.DocTableName {
   148  			result = append(result, tblName)
   149  		}
   150  	}
   151  	return result
   152  }