go.temporal.io/server@v1.23.0/common/persistence/sql/sqlplugin/postgresql/db.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  	"go.temporal.io/server/common/persistence/schema"
    33  	"go.temporal.io/server/common/persistence/sql/sqlplugin"
    34  	"go.temporal.io/server/common/persistence/sql/sqlplugin/postgresql/driver"
    35  	postgresqlschemaV96 "go.temporal.io/server/schema/postgresql/v96"
    36  )
    37  
    38  func (pdb *db) IsDupEntryError(err error) bool {
    39  	return pdb.dbDriver.IsDupEntryError(err)
    40  }
    41  
    42  func (pdb *db) IsDupDatabaseError(err error) bool {
    43  	return pdb.dbDriver.IsDupDatabaseError(err)
    44  }
    45  
    46  // db represents a logical connection to postgresql database
    47  type db struct {
    48  	dbKind   sqlplugin.DbKind
    49  	dbName   string
    50  	dbDriver driver.Driver
    51  
    52  	db        *sqlx.DB
    53  	tx        *sqlx.Tx
    54  	conn      sqlplugin.Conn
    55  	converter DataConverter
    56  }
    57  
    58  var _ sqlplugin.DB = (*db)(nil)
    59  var _ sqlplugin.Tx = (*db)(nil)
    60  
    61  // newDB returns an instance of DB, which is a logical
    62  // connection to the underlying postgresql database
    63  func newDB(
    64  	dbKind sqlplugin.DbKind,
    65  	dbName string,
    66  	dbDriver driver.Driver,
    67  	xdb *sqlx.DB,
    68  	tx *sqlx.Tx,
    69  ) *db {
    70  	mdb := &db{
    71  		dbKind:   dbKind,
    72  		dbName:   dbName,
    73  		dbDriver: dbDriver,
    74  		db:       xdb,
    75  		tx:       tx,
    76  	}
    77  	mdb.conn = xdb
    78  	if tx != nil {
    79  		mdb.conn = tx
    80  	}
    81  	mdb.converter = &converter{}
    82  	return mdb
    83  }
    84  
    85  // BeginTx starts a new transaction and returns a reference to the Tx object
    86  func (pdb *db) BeginTx(ctx context.Context) (sqlplugin.Tx, error) {
    87  	xtx, err := pdb.db.BeginTxx(ctx, nil)
    88  	if err != nil {
    89  		return nil, err
    90  	}
    91  	return newDB(pdb.dbKind, pdb.dbName, pdb.dbDriver, pdb.db, xtx), nil
    92  }
    93  
    94  // Commit commits a previously started transaction
    95  func (pdb *db) Commit() error {
    96  	return pdb.tx.Commit()
    97  }
    98  
    99  // Rollback triggers rollback of a previously started transaction
   100  func (pdb *db) Rollback() error {
   101  	return pdb.tx.Rollback()
   102  }
   103  
   104  // Close closes the connection to the mysql db
   105  func (pdb *db) Close() error {
   106  	return pdb.db.Close()
   107  }
   108  
   109  // PluginName returns the name of the mysql plugin
   110  func (pdb *db) PluginName() string {
   111  	return PluginName
   112  }
   113  
   114  // DbName returns the name of the database
   115  func (pdb *db) DbName() string {
   116  	return pdb.dbName
   117  }
   118  
   119  // ExpectedVersion returns expected version.
   120  func (pdb *db) ExpectedVersion() string {
   121  	switch pdb.dbKind {
   122  	case sqlplugin.DbKindMain:
   123  		return postgresqlschemaV96.Version
   124  	case sqlplugin.DbKindVisibility:
   125  		return postgresqlschemaV96.VisibilityVersion
   126  	default:
   127  		panic(fmt.Sprintf("unknown db kind %v", pdb.dbKind))
   128  	}
   129  }
   130  
   131  // VerifyVersion verify schema version is up to date
   132  func (pdb *db) VerifyVersion() error {
   133  	expectedVersion := pdb.ExpectedVersion()
   134  	return schema.VerifyCompatibleVersion(pdb, pdb.dbName, expectedVersion)
   135  }