github.com/weaviate/weaviate@v1.24.6/usecases/schema/delete.go (about)

     1  //                           _       _
     2  // __      _____  __ ___   ___  __ _| |_ ___
     3  // \ \ /\ / / _ \/ _` \ \ / / |/ _` | __/ _ \
     4  //  \ V  V /  __/ (_| |\ V /| | (_| | ||  __/
     5  //   \_/\_/ \___|\__,_| \_/ |_|\__,_|\__\___|
     6  //
     7  //  Copyright © 2016 - 2024 Weaviate B.V. All rights reserved.
     8  //
     9  //  CONTACT: hello@weaviate.io
    10  //
    11  
    12  package schema
    13  
    14  import (
    15  	"context"
    16  
    17  	"github.com/pkg/errors"
    18  	"github.com/weaviate/weaviate/entities/models"
    19  )
    20  
    21  // DeleteClass from the schema
    22  func (m *Manager) DeleteClass(ctx context.Context, principal *models.Principal, class string) error {
    23  	err := m.Authorizer.Authorize(principal, "delete", "schema/objects")
    24  	if err != nil {
    25  		return err
    26  	}
    27  
    28  	return m.deleteClass(ctx, class)
    29  }
    30  
    31  func (m *Manager) deleteClass(ctx context.Context, className string) error {
    32  	m.Lock()
    33  	defer m.Unlock()
    34  
    35  	tx, err := m.cluster.BeginTransaction(ctx, DeleteClass,
    36  		DeleteClassPayload{className}, DefaultTxTTL)
    37  	if err != nil {
    38  		// possible causes for errors could be nodes down (we expect every node to
    39  		// be up for a schema transaction) or concurrent transactions from other
    40  		// nodes
    41  		return errors.Wrap(err, "open cluster-wide transaction")
    42  	}
    43  
    44  	if err := m.cluster.CommitWriteTransaction(ctx, tx); err != nil {
    45  		// Only log the commit error, but do not abort the changes locally. Once
    46  		// we've told others to commit, we also need to commit ourselves!
    47  		//
    48  		// The idea is that if we abort our changes we are guaranteed to create an
    49  		// inconsistency as soon as any other node honored the commit. This would
    50  		// for example be the case in a 3-node cluster where node 1 is the
    51  		// coordinator, node 2 honored the commit and node 3 died during the commit
    52  		// phase.
    53  		//
    54  		// In this scenario it is far more desirable to make sure that node 1 and
    55  		// node 2 stay in sync, as node 3 - who may or may not have missed the
    56  		// update - can use a local WAL from the first TX phase to replay any
    57  		// missing changes once it's back.
    58  		m.logger.WithError(err).Errorf("not every node was able to commit")
    59  	}
    60  
    61  	return m.deleteClassApplyChanges(ctx, className)
    62  }
    63  
    64  func (m *Manager) deleteClassApplyChanges(ctx context.Context, className string) error {
    65  	if err := m.repo.DeleteClass(ctx, className); err != nil {
    66  		m.logger.WithField("action", "delete_class").
    67  			WithField("class", className).Errorf("schema: %v", err)
    68  		return err
    69  	}
    70  
    71  	if ok := m.schemaCache.detachClass(className); !ok {
    72  		return nil
    73  	}
    74  
    75  	if err := m.migrator.DropClass(ctx, className); err != nil {
    76  		m.logger.WithField("action", "delete_class").
    77  			WithField("class", className).Errorf("migrator: %v", err)
    78  	}
    79  
    80  	m.schemaCache.deleteClassState(className)
    81  
    82  	m.logger.WithField("action", "delete_class").WithField("class", className).Debug("")
    83  	m.triggerSchemaUpdateCallbacks()
    84  
    85  	return nil
    86  }