go.temporal.io/server@v1.23.0/common/persistence/sql/sqlplugin/postgresql/admin.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 "time" 30 ) 31 32 const ( 33 readSchemaVersionQuery = `SELECT curr_version from schema_version where version_partition=0 and db_name=$1` 34 35 writeSchemaVersionQuery = `INSERT into schema_version(version_partition, db_name, creation_time, curr_version, min_compatible_version) VALUES (0,$1,$2,$3,$4) 36 ON CONFLICT (version_partition, db_name) DO UPDATE 37 SET creation_time = excluded.creation_time, 38 curr_version = excluded.curr_version, 39 min_compatible_version = excluded.min_compatible_version;` 40 41 writeSchemaUpdateHistoryQuery = `INSERT into schema_update_history(version_partition, year, month, update_time, old_version, new_version, manifest_md5, description) VALUES(0,$1,$2,$3,$4,$5,$6,$7)` 42 43 createSchemaVersionTableQuery = `CREATE TABLE IF NOT EXISTS schema_version(` + 44 `version_partition INT not null, ` + 45 `db_name VARCHAR(255) not null, ` + 46 `creation_time TIMESTAMP, ` + 47 `curr_version VARCHAR(64), ` + 48 `min_compatible_version VARCHAR(64), ` + 49 `PRIMARY KEY (version_partition, db_name));` 50 51 createSchemaUpdateHistoryTableQuery = `CREATE TABLE IF NOT EXISTS schema_update_history(` + 52 `version_partition INT not null, ` + 53 `year int not null, ` + 54 `month int not null, ` + 55 `update_time TIMESTAMP not null, ` + 56 `description VARCHAR(255), ` + 57 `manifest_md5 VARCHAR(64), ` + 58 `new_version VARCHAR(64), ` + 59 `old_version VARCHAR(64), ` + 60 `PRIMARY KEY (version_partition, year, month, update_time));` 61 62 // NOTE we have to use %v because somehow postgresql doesn't work with ? here 63 // It's a small bug in sqlx library 64 // TODO https://github.com/uber/cadence/issues/2893 65 createDatabaseQuery = `CREATE DATABASE "%v"` 66 67 dropDatabaseQuery = "DROP DATABASE IF EXISTS %v" 68 69 listTablesQuery = "select table_name from information_schema.tables where table_schema='public'" 70 71 dropTableQuery = "DROP TABLE %v" 72 ) 73 74 // CreateSchemaVersionTables sets up the schema version tables 75 func (pdb *db) CreateSchemaVersionTables() error { 76 if err := pdb.Exec(createSchemaVersionTableQuery); err != nil { 77 return err 78 } 79 return pdb.Exec(createSchemaUpdateHistoryTableQuery) 80 } 81 82 // ReadSchemaVersion returns the current schema version for the keyspace 83 func (pdb *db) ReadSchemaVersion(database string) (string, error) { 84 var version string 85 err := pdb.db.Get(&version, readSchemaVersionQuery, database) 86 return version, err 87 } 88 89 // UpdateSchemaVersion updates the schema version for the keyspace 90 func (pdb *db) UpdateSchemaVersion(database string, newVersion string, minCompatibleVersion string) error { 91 return pdb.Exec(writeSchemaVersionQuery, database, time.Now().UTC(), newVersion, minCompatibleVersion) 92 } 93 94 // WriteSchemaUpdateLog adds an entry to the schema update history table 95 func (pdb *db) WriteSchemaUpdateLog(oldVersion string, newVersion string, manifestMD5 string, desc string) error { 96 now := time.Now().UTC() 97 return pdb.Exec(writeSchemaUpdateHistoryQuery, now.Year(), int(now.Month()), now, oldVersion, newVersion, manifestMD5, desc) 98 } 99 100 // Exec executes a sql statement 101 func (pdb *db) Exec(stmt string, args ...interface{}) error { 102 _, err := pdb.db.Exec(stmt, args...) 103 return err 104 } 105 106 // ListTables returns a list of tables in this database 107 func (pdb *db) ListTables(database string) ([]string, error) { 108 var tables []string 109 err := pdb.db.Select(&tables, listTablesQuery) 110 return tables, err 111 } 112 113 // DropTable drops a given table from the database 114 func (pdb *db) DropTable(name string) error { 115 return pdb.Exec(fmt.Sprintf(dropTableQuery, name)) 116 } 117 118 // DropAllTables drops all tables from this database 119 func (pdb *db) DropAllTables(database string) error { 120 tables, err := pdb.ListTables(database) 121 if err != nil { 122 return err 123 } 124 for _, tab := range tables { 125 if err := pdb.DropTable(tab); err != nil { 126 return err 127 } 128 } 129 return nil 130 } 131 132 // CreateDatabase creates a database if it doesn't exist 133 func (pdb *db) CreateDatabase(name string) error { 134 if err := pdb.Exec(fmt.Sprintf(createDatabaseQuery, name)); err != nil { 135 if pdb.IsDupDatabaseError(err) { 136 return nil 137 } 138 return err 139 } 140 141 return nil 142 } 143 144 // DropDatabase drops a database 145 func (pdb *db) DropDatabase(name string) error { 146 return pdb.Exec(fmt.Sprintf(dropDatabaseQuery, name)) 147 }