go.temporal.io/server@v1.23.0/common/persistence/sql/sqlplugin/postgresql/plugin.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 "fmt" 29 "strings" 30 31 "github.com/jmoiron/sqlx" 32 "go.temporal.io/api/serviceerror" 33 34 "go.temporal.io/server/common/config" 35 "go.temporal.io/server/common/persistence/sql" 36 "go.temporal.io/server/common/persistence/sql/sqlplugin" 37 "go.temporal.io/server/common/persistence/sql/sqlplugin/postgresql/driver" 38 "go.temporal.io/server/common/persistence/sql/sqlplugin/postgresql/session" 39 "go.temporal.io/server/common/resolver" 40 ) 41 42 const ( 43 // PluginName is the name of the plugin 44 PluginName = "postgres" 45 PluginNamePGX = "postgres_pgx" 46 ) 47 48 var ( 49 defaultDatabaseNames = []string{ 50 "postgres", // normal PostgreSQL default DB name 51 "defaultdb", // special behavior for Aiven: #1389 52 } 53 ) 54 55 type plugin struct { 56 d driver.Driver 57 } 58 59 var _ sqlplugin.Plugin = (*plugin)(nil) 60 61 func init() { 62 sql.RegisterPlugin(PluginName, &plugin{&driver.PQDriver{}}) 63 sql.RegisterPlugin(PluginNamePGX, &plugin{&driver.PGXDriver{}}) 64 65 } 66 67 // CreateDB initialize the db object 68 func (d *plugin) CreateDB( 69 dbKind sqlplugin.DbKind, 70 cfg *config.SQL, 71 r resolver.ServiceResolver, 72 ) (sqlplugin.DB, error) { 73 conn, err := d.createDBConnection(cfg, r) 74 if err != nil { 75 return nil, err 76 } 77 db := newDB(dbKind, cfg.DatabaseName, d.d, conn, nil) 78 return db, nil 79 } 80 81 // CreateAdminDB initialize the adminDB object 82 func (d *plugin) CreateAdminDB( 83 dbKind sqlplugin.DbKind, 84 cfg *config.SQL, 85 r resolver.ServiceResolver, 86 ) (sqlplugin.AdminDB, error) { 87 conn, err := d.createDBConnection(cfg, r) 88 if err != nil { 89 return nil, err 90 } 91 db := newDB(dbKind, cfg.DatabaseName, d.d, conn, nil) 92 return db, nil 93 } 94 95 // CreateDBConnection creates a returns a reference to a logical connection to the 96 // underlying SQL database. The returned object is to tied to a single 97 // SQL database and the object can be used to perform CRUD operations on 98 // the tables in the database 99 func (d *plugin) createDBConnection( 100 cfg *config.SQL, 101 resolver resolver.ServiceResolver, 102 ) (*sqlx.DB, error) { 103 if cfg.DatabaseName != "" { 104 postgresqlSession, err := session.NewSession(cfg, d.d, resolver) 105 if err != nil { 106 return nil, err 107 } 108 return postgresqlSession.DB, nil 109 } 110 111 // database name not provided 112 // try defaults 113 defer func() { cfg.DatabaseName = "" }() 114 115 var errors []error 116 for _, databaseName := range defaultDatabaseNames { 117 cfg.DatabaseName = databaseName 118 if postgresqlSession, err := session.NewSession( 119 cfg, 120 d.d, 121 resolver, 122 ); err == nil { 123 return postgresqlSession.DB, nil 124 } else { 125 errors = append(errors, err) 126 } 127 } 128 return nil, serviceerror.NewUnavailable( 129 fmt.Sprintf("unable to connect to DB, tried default DB names: %v, errors: %v", strings.Join(defaultDatabaseNames, ","), errors), 130 ) 131 }