go.temporal.io/server@v1.23.0/common/persistence/sql/sqlplugin/postgresql/db_v12.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 postgresql
    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  	"go.temporal.io/server/common/persistence/sql/sqlplugin/postgresql/driver"
    36  	postgresqlschemaV12 "go.temporal.io/server/schema/postgresql/v12"
    37  )
    38  
    39  // dbV12 represents a logical connection to mysql database
    40  type dbV12 struct {
    41  	db
    42  }
    43  
    44  var _ sqlplugin.DB = (*dbV12)(nil)
    45  var _ sqlplugin.Tx = (*dbV12)(nil)
    46  
    47  // newDB returns an instance of DB, which is a logical
    48  // connection to the underlying postgresql database
    49  func newDBV12(
    50  	dbKind sqlplugin.DbKind,
    51  	dbName string,
    52  	dbDriver driver.Driver,
    53  	xdb *sqlx.DB,
    54  	tx *sqlx.Tx,
    55  ) *dbV12 {
    56  	mdb := &dbV12{
    57  		db: db{
    58  			dbKind:   dbKind,
    59  			dbName:   dbName,
    60  			dbDriver: dbDriver,
    61  			db:       xdb,
    62  			tx:       tx,
    63  		},
    64  	}
    65  	mdb.conn = xdb
    66  	if tx != nil {
    67  		mdb.conn = tx
    68  	}
    69  	mdb.converter = &converter{}
    70  	return mdb
    71  }
    72  
    73  // BeginTx starts a new transaction and returns a reference to the Tx object
    74  func (pdb *dbV12) BeginTx(ctx context.Context) (sqlplugin.Tx, error) {
    75  	xtx, err := pdb.db.db.BeginTxx(ctx, nil)
    76  	if err != nil {
    77  		return nil, err
    78  	}
    79  	return newDB(pdb.dbKind, pdb.dbName, pdb.dbDriver, pdb.db.db, xtx), nil
    80  }
    81  
    82  // PluginName returns the name of the mysql plugin
    83  func (pdb *dbV12) PluginName() string {
    84  	return PluginNameV12
    85  }
    86  
    87  // ExpectedVersion returns expected version.
    88  func (pdb *dbV12) ExpectedVersion() string {
    89  	switch pdb.dbKind {
    90  	case sqlplugin.DbKindMain:
    91  		return postgresqlschemaV12.Version
    92  	case sqlplugin.DbKindVisibility:
    93  		return postgresqlschemaV12.VisibilityVersion
    94  	default:
    95  		panic(fmt.Sprintf("unknown db kind %v", pdb.dbKind))
    96  	}
    97  }
    98  
    99  // VerifyVersion verify schema version is up to date
   100  func (pdb *dbV12) VerifyVersion() error {
   101  	expectedVersion := pdb.ExpectedVersion()
   102  	return schema.VerifyCompatibleVersion(pdb, pdb.dbName, expectedVersion)
   103  }