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