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

     1  // The MIT License
     2  //
     3  // Copyright (c) 2021 Datadog, Inc.
     4  //
     5  // Copyright (c) 2020 Temporal Technologies Inc.  All rights reserved.
     6  //
     7  // Copyright (c) 2020 Uber Technologies, Inc.
     8  //
     9  // Permission is hereby granted, free of charge, to any person obtaining a copy
    10  // of this software and associated documentation files (the "Software"), to deal
    11  // in the Software without restriction, including without limitation the rights
    12  // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
    13  // copies of the Software, and to permit persons to whom the Software is
    14  // furnished to do so, subject to the following conditions:
    15  //
    16  // The above copyright notice and this permission notice shall be included in
    17  // all copies or substantial portions of the Software.
    18  //
    19  // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
    20  // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
    21  // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
    22  // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
    23  // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
    24  // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
    25  // THE SOFTWARE.
    26  
    27  package sqlite
    28  
    29  import (
    30  	"context"
    31  	"fmt"
    32  	"sync"
    33  
    34  	"github.com/jmoiron/sqlx"
    35  
    36  	"go.temporal.io/server/common/persistence/sql/sqlplugin"
    37  	sqliteschema "go.temporal.io/server/schema/sqlite"
    38  )
    39  
    40  // db represents a logical connection to sqlite database
    41  type db struct {
    42  	dbKind sqlplugin.DbKind
    43  	dbName string
    44  
    45  	mu      sync.RWMutex
    46  	onClose []func()
    47  
    48  	db        *sqlx.DB
    49  	tx        *sqlx.Tx
    50  	conn      sqlplugin.Conn
    51  	converter DataConverter
    52  }
    53  
    54  var _ sqlplugin.AdminDB = (*db)(nil)
    55  var _ sqlplugin.DB = (*db)(nil)
    56  var _ sqlplugin.Tx = (*db)(nil)
    57  
    58  // newDB returns an instance of DB, which is a logical
    59  // connection to the underlying sqlite database
    60  func newDB(
    61  	dbKind sqlplugin.DbKind,
    62  	dbName string,
    63  	xdb *sqlx.DB,
    64  	tx *sqlx.Tx,
    65  ) *db {
    66  	mdb := &db{
    67  		dbKind:  dbKind,
    68  		dbName:  dbName,
    69  		onClose: make([]func(), 0),
    70  		db:      xdb,
    71  		tx:      tx,
    72  	}
    73  	mdb.conn = xdb
    74  	if tx != nil {
    75  		mdb.conn = tx
    76  	}
    77  	mdb.converter = &converter{}
    78  	return mdb
    79  }
    80  
    81  // BeginTx starts a new transaction and returns a reference to the Tx object
    82  func (mdb *db) BeginTx(ctx context.Context) (sqlplugin.Tx, error) {
    83  	xtx, err := mdb.db.BeginTxx(ctx, nil)
    84  	if err != nil {
    85  		return nil, err
    86  	}
    87  	return newDB(mdb.dbKind, mdb.dbName, mdb.db, xtx), nil
    88  }
    89  
    90  // Commit commits a previously started transaction
    91  func (mdb *db) Commit() error {
    92  	return mdb.tx.Commit()
    93  }
    94  
    95  // Rollback triggers rollback of a previously started transaction
    96  func (mdb *db) Rollback() error {
    97  	return mdb.tx.Rollback()
    98  }
    99  
   100  func (mdb *db) OnClose(hook func()) {
   101  	mdb.mu.Lock()
   102  	mdb.onClose = append(mdb.onClose, hook)
   103  	mdb.mu.Unlock()
   104  }
   105  
   106  // Close closes the connection to the sqlite db
   107  func (mdb *db) Close() error {
   108  	mdb.mu.RLock()
   109  	defer mdb.mu.RUnlock()
   110  
   111  	for _, hook := range mdb.onClose {
   112  		// de-registers the database from conn pool
   113  		hook()
   114  	}
   115  
   116  	// database connection will be automatically closed by the hook handler when all references are removed
   117  	return nil
   118  }
   119  
   120  // PluginName returns the name of the plugin
   121  func (mdb *db) PluginName() string {
   122  	return PluginName
   123  }
   124  
   125  // DbName returns the name of the database
   126  func (mdb *db) DbName() string {
   127  	return mdb.dbName
   128  }
   129  
   130  // ExpectedVersion returns expected version.
   131  func (mdb *db) ExpectedVersion() string {
   132  	switch mdb.dbKind {
   133  	case sqlplugin.DbKindMain:
   134  		return sqliteschema.Version
   135  	case sqlplugin.DbKindVisibility:
   136  		return sqliteschema.VisibilityVersion
   137  	default:
   138  		panic(fmt.Sprintf("unknown db kind %v", mdb.dbKind))
   139  	}
   140  }
   141  
   142  // VerifyVersion verify schema version is up to date
   143  func (mdb *db) VerifyVersion() error {
   144  	return nil
   145  	// TODO(jlegrone): implement this
   146  	// expectedVersion := mdb.ExpectedVersion()
   147  	// return schema.VerifyCompatibleVersion(mdb, mdb.dbName, expectedVersion)
   148  }