go.temporal.io/server@v1.23.0/common/persistence/sql/sqlplugin/mysql/db_v8.go (about)

     1  // The MIT License
     2  //
     3  // Copyright (c) 2020 Temporal Technologies Inc.  All rights reserved.
     4  //
     5  // Copyright (c) 2020 Uber Technologies, Inc.
     6  //
     7  // Permission is hereby granted, free of charge, to any person obtaining a copy
     8  // of this software and associated documentation files (the "Software"), to deal
     9  // in the Software without restriction, including without limitation the rights
    10  // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
    11  // copies of the Software, and to permit persons to whom the Software is
    12  // furnished to do so, subject to the following conditions:
    13  //
    14  // The above copyright notice and this permission notice shall be included in
    15  // all copies or substantial portions of the Software.
    16  //
    17  // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
    18  // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
    19  // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
    20  // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
    21  // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
    22  // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
    23  // THE SOFTWARE.
    24  
    25  package mysql
    26  
    27  import (
    28  	"context"
    29  	"fmt"
    30  
    31  	"github.com/jmoiron/sqlx"
    32  
    33  	"go.temporal.io/server/common/persistence/schema"
    34  	"go.temporal.io/server/common/persistence/sql/sqlplugin"
    35  	mysqlschemaV8 "go.temporal.io/server/schema/mysql/v8"
    36  )
    37  
    38  // db represents a logical connection to mysql database
    39  type dbV8 struct {
    40  	db
    41  }
    42  
    43  var _ sqlplugin.AdminDB = (*dbV8)(nil)
    44  var _ sqlplugin.DB = (*dbV8)(nil)
    45  var _ sqlplugin.Tx = (*dbV8)(nil)
    46  
    47  // newDB returns an instance of DB, which is a logical
    48  // connection to the underlying mysql database
    49  func newDBV8(
    50  	dbKind sqlplugin.DbKind,
    51  	dbName string,
    52  	xdb *sqlx.DB,
    53  	tx *sqlx.Tx,
    54  ) *dbV8 {
    55  	mdb := &dbV8{
    56  		db: db{
    57  			dbKind: dbKind,
    58  			dbName: dbName,
    59  			db:     xdb,
    60  			tx:     tx,
    61  		},
    62  	}
    63  	mdb.conn = xdb
    64  	if tx != nil {
    65  		mdb.conn = tx
    66  	}
    67  	mdb.converter = &converter{}
    68  	return mdb
    69  }
    70  
    71  // BeginTx starts a new transaction and returns a reference to the Tx object
    72  func (mdb *dbV8) BeginTx(ctx context.Context) (sqlplugin.Tx, error) {
    73  	xtx, err := mdb.db.db.BeginTxx(ctx, nil)
    74  	if err != nil {
    75  		return nil, err
    76  	}
    77  	return newDBV8(mdb.dbKind, mdb.dbName, mdb.db.db, xtx), nil
    78  }
    79  
    80  // PluginName returns the name of the mysql plugin
    81  func (mdb *dbV8) PluginName() string {
    82  	return PluginNameV8
    83  }
    84  
    85  // ExpectedVersion returns expected version.
    86  func (mdb *dbV8) ExpectedVersion() string {
    87  	switch mdb.dbKind {
    88  	case sqlplugin.DbKindMain:
    89  		return mysqlschemaV8.Version
    90  	case sqlplugin.DbKindVisibility:
    91  		return mysqlschemaV8.VisibilityVersion
    92  	default:
    93  		panic(fmt.Sprintf("unknown db kind %v", mdb.dbKind))
    94  	}
    95  }
    96  
    97  // VerifyVersion verify schema version is up to date
    98  func (mdb *dbV8) VerifyVersion() error {
    99  	expectedVersion := mdb.ExpectedVersion()
   100  	return schema.VerifyCompatibleVersion(mdb, mdb.dbName, expectedVersion)
   101  }