github.com/cockroachdb/cockroach@v20.2.0-alpha.1+incompatible/pkg/sql/database.go (about)

     1  // Copyright 2015 The Cockroach Authors.
     2  //
     3  // Use of this software is governed by the Business Source License
     4  // included in the file licenses/BSL.txt.
     5  //
     6  // As of the Change Date specified in that file, in accordance with
     7  // the Business Source License, use of this software will be governed
     8  // by the Apache License, Version 2.0, included in the file
     9  // licenses/APL.txt.
    10  
    11  package sql
    12  
    13  import (
    14  	"context"
    15  
    16  	"github.com/cockroachdb/cockroach/pkg/kv"
    17  	"github.com/cockroachdb/cockroach/pkg/sql/catalog/descs"
    18  	"github.com/cockroachdb/cockroach/pkg/sql/pgwire/pgcode"
    19  	"github.com/cockroachdb/cockroach/pkg/sql/pgwire/pgerror"
    20  	"github.com/cockroachdb/cockroach/pkg/sql/sqlbase"
    21  	"github.com/cockroachdb/cockroach/pkg/util/log"
    22  )
    23  
    24  //
    25  // This file contains routines for low-level access to stored database
    26  // descriptors, as well as accessors for the database cache.
    27  //
    28  // For higher levels in the SQL layer, these interface are likely not
    29  // suitable; consider instead schema_accessors.go and resolver.go.
    30  //
    31  
    32  // renameDatabase implements the DatabaseDescEditor interface.
    33  func (p *planner) renameDatabase(
    34  	ctx context.Context, oldDesc *sqlbase.DatabaseDescriptor, newName string,
    35  ) error {
    36  	oldName := oldDesc.Name
    37  	oldDesc.SetName(newName)
    38  	if err := oldDesc.Validate(); err != nil {
    39  		return err
    40  	}
    41  
    42  	if exists, _, err := sqlbase.LookupDatabaseID(ctx, p.txn, p.ExecCfg().Codec, newName); err == nil && exists {
    43  		return pgerror.Newf(pgcode.DuplicateDatabase,
    44  			"the new database name %q already exists", newName)
    45  	} else if err != nil {
    46  		return err
    47  	}
    48  
    49  	newKey := sqlbase.MakeDatabaseNameKey(ctx, p.ExecCfg().Settings, newName).Key(p.ExecCfg().Codec)
    50  
    51  	descID := oldDesc.GetID()
    52  	descKey := sqlbase.MakeDescMetadataKey(p.ExecCfg().Codec, descID)
    53  	descDesc := sqlbase.WrapDescriptor(oldDesc)
    54  
    55  	b := &kv.Batch{}
    56  	if p.ExtendedEvalContext().Tracing.KVTracingEnabled() {
    57  		log.VEventf(ctx, 2, "CPut %s -> %d", newKey, descID)
    58  		log.VEventf(ctx, 2, "Put %s -> %s", descKey, descDesc)
    59  	}
    60  	b.CPut(newKey, descID, nil)
    61  	b.Put(descKey, descDesc)
    62  	err := sqlbase.RemoveDatabaseNamespaceEntry(
    63  		ctx, p.txn, p.ExecCfg().Codec, oldName, p.ExtendedEvalContext().Tracing.KVTracingEnabled(),
    64  	)
    65  	if err != nil {
    66  		return err
    67  	}
    68  
    69  	p.Tables().AddUncommittedDatabase(oldName, descID, descs.DBDropped)
    70  	p.Tables().AddUncommittedDatabase(newName, descID, descs.DBCreated)
    71  
    72  	return p.txn.Run(ctx, b)
    73  }