The query_blocker extension adds Database#block_queries. Inside the block passed to block_queries, any attempts to execute a query/statement on the database will raise a Sequel::QueryBlocker::BlockedQuery
exception.
DB.extension :query_blocker DB.block_queries do ds = DB[:table] # No exception ds = ds.where(column: 1) # No exception ds.all # Exception raised end
To handle concurrency, you can pass a :scope option:
# Current Thread DB.block_queries(scope: :thread){} # Current Fiber DB.block_queries(scope: :fiber){} # Specific Thread DB.block_queries(scope: Thread.current){} # Specific Fiber DB.block_queries(scope: Fiber.current){}
Database#block_queries is useful for blocking queries inside the block. However, there may be cases where you want to allow queries in specific places inside a block_queries block. You can use Database#allow_queries for that:
DB.block_queries do DB.allow_queries do DB[:table].all # Query allowed end DB[:table].all # Exception raised end
When mixing block_queries and allow_queries with scopes, the narrowest scope has priority. So if you are blocking with :thread scope, and allowing with :fiber scope, queries in the current fiber will be allowed, but queries in different fibers of the current thread will be blocked.
Note that this should catch all queries executed through the Database instance. Whether it catches queries executed directly on a connection object depends on the adapter in use.
Related module: Sequel::QueryBlocker
Required files
- fiber