Documentation
¶
Overview ¶
sql_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.
Index ¶
- func ArgsToOrder(args []string) (cleanedOrderList []string, err *errors.ServiceError)
- func GetTableName(g2 *gorm.DB) string
- func IsDBConnectionError(err error) bool
- func IsInvalidColumnError(err error) bool
- func MarkForRollback(ctx context.Context, err error)
- func Migrate(g2 *gorm.DB) error
- func MigrateWithLock(ctx context.Context, factory SessionFactory) error
- func NewAdvisoryLockContext(ctx context.Context, connection SessionFactory, id string, lockType LockType) (context.Context, string, error)
- func NewContext(ctx context.Context, connection SessionFactory) (context.Context, error)
- func Resolve(ctx context.Context)
- func TSLToSQL(node *tsl.TSLNode, cfg WalkConfig) (string, []any, *errors.ServiceError)
- func TransactionMiddleware(next http.Handler, connection SessionFactory, requestTimeout time.Duration) http.Handler
- func Unlock(ctx context.Context, callerUUID string)
- type AdvisoryLock
- type LockType
- type SessionFactory
- type WalkConfig
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func ArgsToOrder ¶ added in v0.3.1
func ArgsToOrder(args []string) (cleanedOrderList []string, err *errors.ServiceError)
ArgsToOrder 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.
func GetTableName ¶
func IsDBConnectionError ¶ added in v0.2.1
IsDBConnectionError indicates whether err is an infrastructure failure (network unreachable, connection refused, connection dropped)
func IsInvalidColumnError ¶ added in v0.3.1
IsInvalidColumnError reports whether err is an "undefined_column" (42703) or "ambiguous_column" (42702) error, both indicate an invalid field in a search or order query
func MarkForRollback ¶
MarkForRollback flags the transaction stored in the context for rollback and logs whatever error caused the rollback
func MigrateWithLock ¶ added in v0.2.0
func MigrateWithLock(ctx context.Context, factory SessionFactory) error
MigrateWithLock runs migrations with an advisory lock to prevent concurrent migrations
func NewAdvisoryLockContext ¶ added in v0.2.0
func NewAdvisoryLockContext( ctx context.Context, connection SessionFactory, id string, lockType LockType, ) (context.Context, string, error)
NewAdvisoryLockContext returns a new context with AdvisoryLock stored in it. Upon error, the original context is still returned along with an error.
IMPORTANT: 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.
CONCURRENCY: 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.
func NewContext ¶
NewContext returns a new context with transaction stored in it. Upon error, the original context is still returned along with an error
func Resolve ¶
Resolve commits or rolls back the transaction based on the rollback flag. Should only be called by TransactionMiddleware for write operations.
func TSLToSQL ¶ added in v0.3.1
func TSLToSQL(node *tsl.TSLNode, cfg WalkConfig) (string, []any, *errors.ServiceError)
TSLToSQL 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.
func TransactionMiddleware ¶
func TransactionMiddleware(next http.Handler, connection SessionFactory, requestTimeout time.Duration) http.Handler
TransactionMiddleware creates a database transaction for write operations only.
Write 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.
Trade-off: List operations (COUNT + SELECT) may show inconsistent pagination totals under concurrent deletes, but this is an acceptable cosmetic issue.
The requestTimeout is applied to all requests (read and write) to prevent queries from blocking indefinitely when the database is under pressure.
Types ¶
type AdvisoryLock ¶ added in v0.2.0
type AdvisoryLock struct {
// contains filtered or unexported fields
}
AdvisoryLock represents a postgres advisory lock
begin # start a Tx select pg_advisory_xact_lock(id, lockType) # obtain the lock (blocking) end # end the Tx and release the lock
ownerUUID 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).
type LockType ¶ added in v0.2.0
type LockType string
LockType represents the type of advisory lock
const ( // Migrations lock type for database migrations Migrations LockType = "Migrations" // MigrationsLockID is the advisory lock ID used for migration coordination MigrationsLockID = "migrations" )
type SessionFactory ¶
Source Files
¶
Directories
¶
| Path | Synopsis |
|---|---|
|
Package db_context dbContext provides a wrapper around db context handling to allow access to the db context without requiring importing the db package, thus avoiding cyclic imports
|
Package db_context dbContext provides a wrapper around db context handling to allow access to the db context without requiring importing the db package, thus avoiding cyclic imports |