vitess.io/vitess@v0.16.2/go/vt/schemamanager/plain_controller.go (about)

     1  /*
     2  Copyright 2019 The Vitess Authors.
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8      http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  
    17  package schemamanager
    18  
    19  import (
    20  	"encoding/json"
    21  	"strings"
    22  
    23  	"context"
    24  
    25  	"vitess.io/vitess/go/vt/log"
    26  )
    27  
    28  // PlainController implements Controller interface.
    29  type PlainController struct {
    30  	sqls     []string
    31  	keyspace string
    32  }
    33  
    34  // NewPlainController creates a new PlainController instance.
    35  func NewPlainController(sqls []string, keyspace string) *PlainController {
    36  	controller := &PlainController{
    37  		sqls:     make([]string, 0, 32),
    38  		keyspace: keyspace,
    39  	}
    40  
    41  	for _, sql := range sqls {
    42  		s := strings.TrimSpace(sql)
    43  		if s != "" {
    44  			controller.sqls = append(controller.sqls, s)
    45  		}
    46  	}
    47  	return controller
    48  }
    49  
    50  // Open is a no-op.
    51  func (controller *PlainController) Open(ctx context.Context) error {
    52  	return nil
    53  }
    54  
    55  // Read reads schema changes
    56  func (controller *PlainController) Read(ctx context.Context) ([]string, error) {
    57  	return controller.sqls, nil
    58  }
    59  
    60  // Close is a no-op.
    61  func (controller *PlainController) Close() {
    62  }
    63  
    64  // Keyspace returns keyspace to apply schema.
    65  func (controller *PlainController) Keyspace() string {
    66  	return controller.keyspace
    67  }
    68  
    69  // OnReadSuccess is called when schemamanager successfully
    70  // reads all sql statements.
    71  func (controller *PlainController) OnReadSuccess(ctx context.Context) error {
    72  	log.Info("Successfully read all schema changes.")
    73  	return nil
    74  }
    75  
    76  // OnReadFail is called when schemamanager fails to read all sql statements.
    77  func (controller *PlainController) OnReadFail(ctx context.Context, err error) error {
    78  	log.Errorf("Failed to read schema changes, error: %v\n", err)
    79  	return err
    80  }
    81  
    82  // OnValidationSuccess is called when schemamanager successfully validates all sql statements.
    83  func (controller *PlainController) OnValidationSuccess(ctx context.Context) error {
    84  	log.Info("Successfully validated all SQL statements.")
    85  	return nil
    86  }
    87  
    88  // OnValidationFail is called when schemamanager fails to validate sql statements.
    89  func (controller *PlainController) OnValidationFail(ctx context.Context, err error) error {
    90  	log.Errorf("Failed to validate SQL statements, error: %v\n", err)
    91  	return err
    92  }
    93  
    94  // OnExecutorComplete  is called when schemamanager finishes applying schema changes.
    95  func (controller *PlainController) OnExecutorComplete(ctx context.Context, result *ExecuteResult) error {
    96  	out, _ := json.MarshalIndent(result, "", "  ")
    97  	log.Infof("Executor finished, result: %s\n", string(out))
    98  	return nil
    99  }
   100  
   101  var _ Controller = (*PlainController)(nil)