go.temporal.io/server@v1.23.0/common/persistence/sql/sqlplugin/mysql/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 mysql 26 27 import ( 28 "context" 29 "fmt" 30 31 "github.com/go-sql-driver/mysql" 32 "github.com/jmoiron/sqlx" 33 34 "go.temporal.io/server/common/persistence/schema" 35 "go.temporal.io/server/common/persistence/sql/sqlplugin" 36 mysqlschemaV57 "go.temporal.io/server/schema/mysql/v57" 37 ) 38 39 // db represents a logical connection to mysql database 40 type db struct { 41 dbKind sqlplugin.DbKind 42 dbName string 43 44 db *sqlx.DB 45 tx *sqlx.Tx 46 conn sqlplugin.Conn 47 converter DataConverter 48 } 49 50 var _ sqlplugin.AdminDB = (*db)(nil) 51 var _ sqlplugin.DB = (*db)(nil) 52 var _ sqlplugin.Tx = (*db)(nil) 53 54 // ErrDupEntryCode MySQL Error 1062 indicates a duplicate primary key i.e. the row already exists, 55 // so we don't do the insert and return a ConditionalUpdate error. 56 const ErrDupEntryCode = 1062 57 58 func (mdb *db) IsDupEntryError(err error) bool { 59 sqlErr, ok := err.(*mysql.MySQLError) 60 return ok && sqlErr.Number == ErrDupEntryCode 61 } 62 63 // newDB returns an instance of DB, which is a logical 64 // connection to the underlying mysql database 65 func newDB( 66 dbKind sqlplugin.DbKind, 67 dbName string, 68 xdb *sqlx.DB, 69 tx *sqlx.Tx, 70 ) *db { 71 mdb := &db{ 72 dbKind: dbKind, 73 dbName: dbName, 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 (mdb *db) BeginTx(ctx context.Context) (sqlplugin.Tx, error) { 87 xtx, err := mdb.db.BeginTxx(ctx, nil) 88 if err != nil { 89 return nil, err 90 } 91 return newDB(mdb.dbKind, mdb.dbName, mdb.db, xtx), nil 92 } 93 94 // Commit commits a previously started transaction 95 func (mdb *db) Commit() error { 96 return mdb.tx.Commit() 97 } 98 99 // Rollback triggers rollback of a previously started transaction 100 func (mdb *db) Rollback() error { 101 return mdb.tx.Rollback() 102 } 103 104 // Close closes the connection to the mysql db 105 func (mdb *db) Close() error { 106 return mdb.db.Close() 107 } 108 109 // PluginName returns the name of the mysql plugin 110 func (mdb *db) PluginName() string { 111 return PluginName 112 } 113 114 // DbName returns the name of the database 115 func (mdb *db) DbName() string { 116 return mdb.dbName 117 } 118 119 // ExpectedVersion returns expected version. 120 func (mdb *db) ExpectedVersion() string { 121 switch mdb.dbKind { 122 case sqlplugin.DbKindMain: 123 return mysqlschemaV57.Version 124 case sqlplugin.DbKindVisibility: 125 return mysqlschemaV57.VisibilityVersion 126 default: 127 panic(fmt.Sprintf("unknown db kind %v", mdb.dbKind)) 128 } 129 } 130 131 // VerifyVersion verify schema version is up to date 132 func (mdb *db) VerifyVersion() error { 133 expectedVersion := mdb.ExpectedVersion() 134 return schema.VerifyCompatibleVersion(mdb, mdb.dbName, expectedVersion) 135 }