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