mirror of
https://github.com/hanzoai/tasks.git
synced 2026-11-03 03:56:04 +00:00
Full module path rebrand from Temporal to Hanzo Tasks: - go.mod module: github.com/hanzoai/tasks - All internal Go imports updated (1906 files) - cmd/server renamed to cmd/tasksd - Binary name: tasksd (was temporal-server) - Docker images: hanzoai/tasks (was temporalio/server) - README rewritten with Hanzo Tasks branding - LLM.md created with integration notes - Makefile, goreleaser, CI workflows updated - go.temporal.io/sdk and go.temporal.io/api imports preserved (separate repos) Fork of Temporal.io (MIT license).
111 lines
3.0 KiB
Go
111 lines
3.0 KiB
Go
package sql
|
|
|
|
import (
|
|
"github.com/hanzoai/tasks/common/config"
|
|
"github.com/hanzoai/tasks/common/log"
|
|
"github.com/hanzoai/tasks/common/metrics"
|
|
"github.com/hanzoai/tasks/common/persistence/sql"
|
|
"github.com/hanzoai/tasks/common/persistence/sql/sqlplugin"
|
|
"github.com/hanzoai/tasks/common/resolver"
|
|
"github.com/hanzoai/tasks/tools/common/schema"
|
|
)
|
|
|
|
type (
|
|
// Connection is the connection to database
|
|
Connection struct {
|
|
dbName string
|
|
adminDb sqlplugin.AdminDB
|
|
}
|
|
)
|
|
|
|
const dbType = "sql"
|
|
|
|
var _ schema.DB = (*Connection)(nil)
|
|
|
|
// NewConnection creates a new connection to database
|
|
func NewConnection(cfg *config.SQL, logger log.Logger) (*Connection, error) {
|
|
db, err := sql.NewSQLAdminDB(sqlplugin.DbKindUnknown, cfg, resolver.NewNoopResolver(), logger, metrics.NoopMetricsHandler)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return &Connection{
|
|
adminDb: db,
|
|
dbName: cfg.DatabaseName,
|
|
}, nil
|
|
}
|
|
|
|
// CreateSchemaVersionTables sets up the schema version tables
|
|
func (c *Connection) CreateSchemaVersionTables() error {
|
|
return c.adminDb.CreateSchemaVersionTables()
|
|
}
|
|
|
|
// ReadSchemaVersion returns the current schema version for the keyspace
|
|
func (c *Connection) ReadSchemaVersion() (string, error) {
|
|
return c.adminDb.ReadSchemaVersion(c.dbName)
|
|
}
|
|
|
|
// UpdateSchemaVersion updates the schema version for the keyspace
|
|
func (c *Connection) UpdateSchemaVersion(newVersion string, minCompatibleVersion string) error {
|
|
return c.adminDb.UpdateSchemaVersion(c.dbName, newVersion, minCompatibleVersion)
|
|
}
|
|
|
|
// WriteSchemaUpdateLog adds an entry to the schema update history table
|
|
func (c *Connection) WriteSchemaUpdateLog(oldVersion string, newVersion string, manifestMD5 string, desc string) error {
|
|
return c.adminDb.WriteSchemaUpdateLog(oldVersion, newVersion, manifestMD5, desc)
|
|
}
|
|
|
|
// Exec executes a sql statement
|
|
func (c *Connection) Exec(stmt string, args ...any) error {
|
|
return c.adminDb.Exec(stmt, args...)
|
|
}
|
|
|
|
// ListTables returns a list of tables in this database
|
|
func (c *Connection) ListTables() ([]string, error) {
|
|
return c.adminDb.ListTables(c.dbName)
|
|
}
|
|
|
|
// DropTable drops a given table from the database
|
|
func (c *Connection) DropTable(name string) error {
|
|
return c.adminDb.DropTable(name)
|
|
}
|
|
|
|
// DropAllTables drops all tables from this database
|
|
func (c *Connection) DropAllTables() error {
|
|
tables, err := c.ListTables()
|
|
if err != nil {
|
|
return err
|
|
}
|
|
for _, tab := range tables {
|
|
if err := c.DropTable(tab); err != nil {
|
|
return err
|
|
}
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// CreateDatabase creates a database if it doesn't exist
|
|
func (c *Connection) CreateDatabase(name string) error {
|
|
return c.adminDb.CreateDatabase(name)
|
|
}
|
|
|
|
// DropDatabase drops a database
|
|
func (c *Connection) DropDatabase(name string) error {
|
|
return c.adminDb.DropDatabase(name)
|
|
}
|
|
|
|
// Close closes the sql client
|
|
func (c *Connection) Close() {
|
|
if c.adminDb != nil {
|
|
err := c.adminDb.Close()
|
|
if err != nil {
|
|
panic("cannot close connection")
|
|
}
|
|
}
|
|
}
|
|
|
|
// Type gives the type of db
|
|
func (c *Connection) Type() string {
|
|
return dbType
|
|
}
|