Files
tasks/common/persistence/sql/sqlplugin/sqlite/admin.go
Stephan BehnkeandGitHub c9e360a3fb go fix (#9337)
## What changed?

Integrate Go 1.26's new `go fix` into workflow.

NOTE that the changes caused our linter to fire; a [separate
commit](https://github.com/temporalio/temporal/pull/9337/commits/1b23f787aef370098b35829c4d17dcc06230b4c1)
addresses those.

## Why?

Ensure Go code is standardized/modernized.

## How did you test it?
- [ ] built
- [ ] run locally and tested manually
- [x] covered by existing tests
- [ ] added new unit test(s)
- [ ] added new functional test(s)
2026-02-18 09:12:19 -08:00

107 lines
3.5 KiB
Go

package sqlite
import (
"fmt"
"time"
)
const (
readSchemaVersionQuery = `SELECT curr_version from schema_version where version_partition=0 and db_name=?`
writeSchemaVersionQuery = `REPLACE into schema_version(version_partition, db_name, creation_time, curr_version, min_compatible_version) VALUES (0,?,?,?,?)`
writeSchemaUpdateHistoryQuery = `INSERT into schema_update_history(version_partition, year, month, update_time, old_version, new_version, manifest_md5, description) VALUES(0,?,?,?,?,?,?,?)`
createSchemaVersionTableQuery = `CREATE TABLE schema_version(version_partition INT not null, ` +
`db_name VARCHAR(255) not null, ` +
`creation_time DATETIME(6), ` +
`curr_version VARCHAR(64), ` +
`min_compatible_version VARCHAR(64), ` +
`PRIMARY KEY (version_partition, db_name));`
createSchemaUpdateHistoryTableQuery = `CREATE TABLE schema_update_history(` +
`version_partition INT not null, ` +
`year int not null, ` +
`month int not null, ` +
`update_time DATETIME(6) not null, ` +
`description VARCHAR(255), ` +
`manifest_md5 VARCHAR(64), ` +
`new_version VARCHAR(64), ` +
`old_version VARCHAR(64), ` +
`PRIMARY KEY (version_partition, year, month, update_time));`
listTablesQuery = "SELECT name FROM sqlite_master WHERE type='table'"
dropTableQuery = "DROP TABLE %v"
)
// CreateSchemaVersionTables sets up the schema version tables
func (mdb *db) CreateSchemaVersionTables() error {
if err := mdb.Exec(createSchemaVersionTableQuery); err != nil {
return err
}
return mdb.Exec(createSchemaUpdateHistoryTableQuery)
}
// ReadSchemaVersion returns the current schema version for the keyspace
func (mdb *db) ReadSchemaVersion(database string) (string, error) {
var version string
err := mdb.db.Get(&version, readSchemaVersionQuery, database)
return version, err
}
// UpdateSchemaVersion updates the schema version for the keyspace
func (mdb *db) UpdateSchemaVersion(database string, newVersion string, minCompatibleVersion string) error {
return mdb.Exec(writeSchemaVersionQuery, database, time.Now().UTC(), newVersion, minCompatibleVersion)
}
// WriteSchemaUpdateLog adds an entry to the schema update history table
func (mdb *db) WriteSchemaUpdateLog(oldVersion string, newVersion string, manifestMD5 string, desc string) error {
now := time.Now().UTC()
return mdb.Exec(writeSchemaUpdateHistoryQuery, now.Year(), int(now.Month()), now, oldVersion, newVersion, manifestMD5, desc)
}
// Exec executes a sql statement
func (mdb *db) Exec(stmt string, args ...any) error {
_, err := mdb.db.Exec(stmt, args...)
return err
}
// ListTables returns a list of tables in this database
func (mdb *db) ListTables(database string) ([]string, error) {
var tables []string
err := mdb.db.Select(&tables, listTablesQuery)
return tables, err
}
// DropTable drops a given table from the database
func (mdb *db) DropTable(name string) error {
return mdb.Exec(fmt.Sprintf(dropTableQuery, name))
}
// DropAllTables drops all tables from this database
func (mdb *db) DropAllTables(database string) error {
tables, err := mdb.ListTables(database)
if err != nil {
return err
}
for _, tab := range tables {
if err := mdb.DropTable(tab); err != nil {
return err
}
}
return nil
}
// CreateDatabase creates a database if it doesn't exist
func (mdb *db) CreateDatabase(name string) error {
// SQLite does not need to create database
return nil
}
// DropDatabase drops a database
func (mdb *db) DropDatabase(name string) error {
// // SQLite does not need to drop database
return nil
}