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

     1  // Copyright 2018 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 testcat
    12  
    13  import (
    14  	"github.com/cockroachdb/cockroach/pkg/sql/sem/tree"
    15  	"github.com/cockroachdb/errors"
    16  )
    17  
    18  // DropTable is a partial implementation of the DROP TABLE statement.
    19  func (tc *Catalog) DropTable(stmt *tree.DropTable) {
    20  	for i := range stmt.Names {
    21  		tn := &stmt.Names[i]
    22  
    23  		// Update the table name to include catalog and schema if not provided.
    24  		tc.qualifyTableName(tn)
    25  
    26  		// Ensure that table with that name exists.
    27  		t := tc.Table(tn)
    28  
    29  		// Clean up FKs from tables referenced by t.
    30  		for _, fk := range t.outboundFKs {
    31  			for _, ds := range tc.testSchema.dataSources {
    32  				if ds.ID() == fk.referencedTableID {
    33  					ref := ds.(*Table)
    34  					oldFKs := ref.inboundFKs
    35  					ref.inboundFKs = nil
    36  					for i := range oldFKs {
    37  						if oldFKs[i].originTableID != t.ID() {
    38  							ref.inboundFKs = append(ref.inboundFKs, oldFKs[i])
    39  						}
    40  					}
    41  					break
    42  				}
    43  			}
    44  		}
    45  
    46  		if len(t.inboundFKs) > 0 {
    47  			panic(errors.Newf("table %s is referenced by FK constraints", tn))
    48  		}
    49  
    50  		// Remove the table from the catalog.
    51  		delete(tc.testSchema.dataSources, tn.FQString())
    52  	}
    53  }