go.temporal.io/server@v1.23.0/common/persistence/sql/sqlplugin/sqlite/admin.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 "fmt" 31 "time" 32 ) 33 34 const ( 35 readSchemaVersionQuery = `SELECT curr_version from schema_version where version_partition=0 and db_name=?` 36 37 writeSchemaVersionQuery = `REPLACE into schema_version(version_partition, db_name, creation_time, curr_version, min_compatible_version) VALUES (0,?,?,?,?)` 38 39 writeSchemaUpdateHistoryQuery = `INSERT into schema_update_history(version_partition, year, month, update_time, old_version, new_version, manifest_md5, description) VALUES(0,?,?,?,?,?,?,?)` 40 41 createSchemaVersionTableQuery = `CREATE TABLE schema_version(version_partition INT not null, ` + 42 `db_name VARCHAR(255) not null, ` + 43 `creation_time DATETIME(6), ` + 44 `curr_version VARCHAR(64), ` + 45 `min_compatible_version VARCHAR(64), ` + 46 `PRIMARY KEY (version_partition, db_name));` 47 48 createSchemaUpdateHistoryTableQuery = `CREATE TABLE schema_update_history(` + 49 `version_partition INT not null, ` + 50 `year int not null, ` + 51 `month int not null, ` + 52 `update_time DATETIME(6) not null, ` + 53 `description VARCHAR(255), ` + 54 `manifest_md5 VARCHAR(64), ` + 55 `new_version VARCHAR(64), ` + 56 `old_version VARCHAR(64), ` + 57 `PRIMARY KEY (version_partition, year, month, update_time));` 58 59 listTablesQuery = "SELECT name FROM sqlite_master WHERE type='table'" 60 61 dropTableQuery = "DROP TABLE %v" 62 ) 63 64 // CreateSchemaVersionTables sets up the schema version tables 65 func (mdb *db) CreateSchemaVersionTables() error { 66 if err := mdb.Exec(createSchemaVersionTableQuery); err != nil { 67 return err 68 } 69 return mdb.Exec(createSchemaUpdateHistoryTableQuery) 70 } 71 72 // ReadSchemaVersion returns the current schema version for the keyspace 73 func (mdb *db) ReadSchemaVersion(database string) (string, error) { 74 var version string 75 err := mdb.db.Get(&version, readSchemaVersionQuery, database) 76 return version, err 77 } 78 79 // UpdateSchemaVersion updates the schema version for the keyspace 80 func (mdb *db) UpdateSchemaVersion(database string, newVersion string, minCompatibleVersion string) error { 81 return mdb.Exec(writeSchemaVersionQuery, database, time.Now().UTC(), newVersion, minCompatibleVersion) 82 } 83 84 // WriteSchemaUpdateLog adds an entry to the schema update history table 85 func (mdb *db) WriteSchemaUpdateLog(oldVersion string, newVersion string, manifestMD5 string, desc string) error { 86 now := time.Now().UTC() 87 return mdb.Exec(writeSchemaUpdateHistoryQuery, now.Year(), int(now.Month()), now, oldVersion, newVersion, manifestMD5, desc) 88 } 89 90 // Exec executes a sql statement 91 func (mdb *db) Exec(stmt string, args ...interface{}) error { 92 _, err := mdb.db.Exec(stmt, args...) 93 return err 94 } 95 96 // ListTables returns a list of tables in this database 97 func (mdb *db) ListTables(database string) ([]string, error) { 98 var tables []string 99 err := mdb.db.Select(&tables, listTablesQuery) 100 return tables, err 101 } 102 103 // DropTable drops a given table from the database 104 func (mdb *db) DropTable(name string) error { 105 return mdb.Exec(fmt.Sprintf(dropTableQuery, name)) 106 } 107 108 // DropAllTables drops all tables from this database 109 func (mdb *db) DropAllTables(database string) error { 110 tables, err := mdb.ListTables(database) 111 if err != nil { 112 return err 113 } 114 for _, tab := range tables { 115 if err := mdb.DropTable(tab); err != nil { 116 return err 117 } 118 } 119 return nil 120 } 121 122 // CreateDatabase creates a database if it doesn't exist 123 func (mdb *db) CreateDatabase(name string) error { 124 // SQLite does not need to create database 125 return nil 126 } 127 128 // DropDatabase drops a database 129 func (mdb *db) DropDatabase(name string) error { 130 // // SQLite does not need to drop database 131 return nil 132 }