{"modulePath":"github.com/openshift-hyperfleet/hyperfleet-api","version":"v0.4.1","isLatest":true,"isStandardLibrary":false,"goos":"all","goarch":"all","docs":"# package db\n\nsql\\_tsl.go implements a custom TSL-to-SQL walker instead of using the library's built-in SQL emitter. We need this because label and condition queries are resolved as scalar subqueries against separate tables (resource\\_labels, resource\\_conditions), JSONB spec fields require dynamic CAST wrapping for numeric comparisons, and certain operators (NOT on labels/conditions) must be rejected at walk time to prevent semantically broken SQL. The built-in walker has no hooks for any of this.\n\n## Functions\n\n```go\nfunc ArgsToOrder(args []string) (cleanedOrderList []string, err *errors.ServiceError)\n```\nArgsToOrder validates and cleans order arguments. Returns a cleaned list of order clauses in the format \\[\"field direction\", ...] Empty or whitespace-only strings are silently skipped.\n\n```go\nfunc GetTableName(g2 *gorm.DB) string\n```\n\n```go\nfunc IsDBConnectionError(err error) bool\n```\nIsDBConnectionError indicates whether err is an infrastructure failure (network unreachable, connection refused, connection dropped)\n\n```go\nfunc IsInvalidColumnError(err error) bool\n```\nIsInvalidColumnError reports whether err is an \"undefined\\_column\" (42703) or \"ambiguous\\_column\" (42702) error, both indicate an invalid field in a search or order query\n\n```go\nfunc MarkForRollback(ctx context.Context, err error)\n```\nMarkForRollback flags the transaction stored in the context for rollback and logs whatever error caused the rollback\n\n```go\nfunc Migrate(g2 *gorm.DB) error\n```\n\n```go\nfunc MigrateWithLock(ctx context.Context, factory SessionFactory) error\n```\nMigrateWithLock runs migrations with an advisory lock to prevent concurrent migrations\n\n```go\nfunc NewAdvisoryLockContext(\n\tctx context.Context, connection SessionFactory, id string, lockType LockType,\n) (context.Context, string, error)\n```\nNewAdvisoryLockContext returns a new context with AdvisoryLock stored in it. Upon error, the original context is still returned along with an error.\n\nIMPORTANT: Advisory locks are for cross-pod coordination (e.g., migrations, scheduled jobs), NOT for database row-level concurrency. For row-level concurrency, use SELECT FOR UPDATE.\n\nCONCURRENCY: The returned context must not be shared across goroutines that call NewAdvisoryLockContext or Unlock concurrently, as the internal lock map is not protected by a mutex. Each goroutine should derive its own context chain.\n\n```go\nfunc NewContext(ctx context.Context, connection SessionFactory) (context.Context, error)\n```\nNewContext returns a new context with transaction stored in it. Upon error, the original context is still returned along with an error\n\n```go\nfunc Resolve(ctx context.Context)\n```\nResolve commits or rolls back the transaction based on the rollback flag. Should only be called by TransactionMiddleware for write operations.\n\n```go\nfunc TSLToSQL(node *tsl.TSLNode, cfg WalkConfig) (string, []any, *errors.ServiceError)\n```\nTSLToSQL walks the TSL tree (read-only) and emits a parameterized SQL WHERE fragment. Labels and conditions are resolved inline as scalar subqueries; JSONB mapping, CAST wrapping, and table-name prefixing all happen during emission.\n\n```go\nfunc TransactionMiddleware(next http.Handler, connection SessionFactory, requestTimeout time.Duration) http.Handler\n```\nTransactionMiddleware creates a database transaction for write operations only.\n\nWrite methods (POST/PUT/PATCH/DELETE) get GORM transactions for ACID guarantees. Read methods (GET) skip transaction creation for performance, reducing connection pool pressure and latency under high adapter polling load.\n\nTrade-off: List operations (COUNT + SELECT) may show inconsistent pagination totals under concurrent deletes, but this is an acceptable cosmetic issue.\n\nThe requestTimeout is applied to all requests (read and write) to prevent queries from blocking indefinitely when the database is under pressure.\n\n```go\nfunc Unlock(ctx context.Context, callerUUID string)\n```\nUnlock searches current locks and unlocks the one matching its owner id.\n\n## Types\n\n```go\ntype AdvisoryLock struct {\n\t// contains filtered or unexported fields\n}\n```\nAdvisoryLock represents a postgres advisory lock\n\n\tbegin                                       # start a Tx\n\tselect pg_advisory_xact_lock(id, lockType)  # obtain the lock (blocking)\n\tend                                         # end the Tx and release the lock\n\nownerUUID is a way to own the lock. Only the very first service call that owns the lock will have the correct ownerUUID. This is necessary to allow functions to call other service functions as part of the same lock (id, lockType).\n\n```go\ntype LockType string\n```\nLockType represents the type of advisory lock\n\n```go\nconst (\n\t// Migrations lock type for database migrations\n\tMigrations LockType = \"Migrations\"\n\n\t// MigrationsLockID is the advisory lock ID used for migration coordination\n\tMigrationsLockID = \"migrations\"\n)\n```\n\n```go\ntype SessionFactory interface {\n\tInit(*config.DatabaseConfig)\n\tDirectDB() *sql.DB\n\tNew(ctx context.Context) *gorm.DB\n\tCheckConnection() error\n\tClose() error\n\tResetDB()\n\tNewListener(ctx context.Context, channel string, callback func(id string))\n\tGetAdvisoryLockTimeout() int\n}\n```\n\n```go\ntype WalkConfig struct {\n\tResolveRelated func(name string) (string, error)\n\tTableName      string\n}\n```\nWalkConfig provides table context and a hook for related-table resolution.\n\n","path":"github.com/openshift-hyperfleet/hyperfleet-api/pkg/db","name":"db","synopsis":"sql_tsl.go implements a custom TSL-to-SQL walker instead of using the library's built-in SQL emitter.","isRedistributable":true}
