Execution hooks
Configure before_all and after_all execution hooks to run SQL, macros, or statement files at the start and end of vulcan plan and vulcan run, with use cases and comparison to model-level pre/post stat
Run SQL statements, SQL files, or macros automatically at the start and end of vulcan plan and vulcan run commands. Automate setup and cleanup tasks: create temporary tables, grant permissions, log pipeline runs, clean up after execution.
Two hooks run at different times:
before_all
Before any model is processed
Set up tables, initialize logging, validate prerequisites
after_all
After all models are processed
Grant privileges, cleanup, send notifications, update metadata
The before_all hook runs once at the beginning, before Vulcan processes any models. Use it for setup tasks. The after_all hook runs once at the end, after Vulcan processes all models. Use it for cleanup or post-processing.
Each hook entry can take one of these forms:
Inline SQL string
CREATE SCHEMA IF NOT EXISTS analytics
The statement is short and easy to read in config.yaml.
Macro call
"@grant_select_privileges()"
The hook needs runtime context or reusable Python logic.
File object
{file: ./statements/select_1.txt}
You want to make the file reference explicit.
File path string
./statements/select_3.sql
The hook should run SQL from a file.
Basic configuration
before_all:
- CREATE TABLE IF NOT EXISTS audit_log (model VARCHAR, started_at TIMESTAMP)
- INSERT INTO audit_log VALUES ('pipeline', CURRENT_TIMESTAMP)
- file: ./statements/select_1.txt
after_all:
- "@grant_select_privileges()"
- UPDATE audit_log SET completed_at = CURRENT_TIMESTAMP WHERE model = 'pipeline'
- ./statements/select_3.sqlfrom vulcan.core.config import Config
config = Config(
before_all=[
"CREATE TABLE IF NOT EXISTS audit_log (model VARCHAR, started_at TIMESTAMP)",
"INSERT INTO audit_log VALUES ('pipeline', CURRENT_TIMESTAMP)"
],
after_all=[
"@grant_select_privileges()",
"UPDATE audit_log SET completed_at = CURRENT_TIMESTAMP WHERE model = 'pipeline'"
],
)File-backed statements
Use file-backed hooks when setup or cleanup SQL is too long for config.yaml, or when multiple environments share the same statement file.
Statement files can use .txt or .sql extensions and may contain multiple SQL statements:
Vulcan resolves file paths relative to the project root. Keep long setup scripts in a folder such as statements/ so config.yaml stays readable.
Using macros in hooks
Hooks execute Vulcan macros using the @macro_name() syntax. Macros have access to runtime context, so hooks can be dynamic. They see what views were created, what schemas are used, and what environment you are running in. Write hooks that adapt to your pipeline state.
Available context variables
Macros invoked in hooks have access to:
evaluator.views
list[str]
All view names created in the virtual layer
evaluator.schemas
list[str]
All schema names used by models
evaluator.this_env
str
Current environment name (for example, prod, dev)
evaluator.gateway
str
Current gateway name
Use cases
Two common ways to use before_all and after_all hooks.
1. Granting privileges on views
Creating many views and granting permissions model-by-model gets tedious. Use after_all to grant access to all views at once:
2. Environment-specific execution
Use different behavior in different environments. Grant certain permissions only in production. Run cleanup tasks only in development. The @IF macro conditionally executes statements based on the current environment:
3. Pipeline audit logging
Track when your pipeline runs and how long it takes. Log the start time in before_all and the completion time in after_all:
4. Schema and database setup
Before models run, make sure all schemas they depend on exist. Instead of creating them manually or remembering the order, let before_all handle it:
5. Data quality gates
Validate source data before processing. Use before_all to run validation checks. If checks fail, the pipeline stops before processing bad data:
6. Refresh materialized views
If materialized views depend on your Vulcan models, refresh them after models update. Let after_all handle it:
7. Notification integration
Notify your team when the pipeline finishes. Use after_all to send notifications. This example logs to a table. Extend it to call an external API or send emails:
Execution order
Best practices
Use macros for complex logic. If hook logic gets complicated, move it to a Python macro. This keeps your YAML config clean and makes the logic easier to test and maintain.
Make hooks idempotent. Hooks may run multiple times if a plan fails and gets retried. Make sure they are safe to run repeatedly. Use IF NOT EXISTS, ON CONFLICT, or similar patterns.
Use environment checks. Not everything should run in every environment. Use @IF(@this_env = 'prod', ...) to gate production-only operations so you do not accidentally run them in development.
Handle failures gracefully. Think about what happens if a hook fails. Will it break your entire pipeline? Use transactions where appropriate, and handle failures appropriately.
Document your hooks. Add comments explaining why each hook exists and what it does.
Test in development first. Always test hooks in a development environment before running them in production. Hooks run automatically, so mistakes can be costly.
Comparison with model-level hooks
When should you use execution hooks versus model-level hooks? The difference:
Feature
before_all / after_all
Model pre_statements / post_statements
Scope
Entire pipeline
Single model
Runs
Once per plan/run
Once per model execution
Access to
All views, schemas, environment
Model-specific context
Use for
Global setup, cleanup, privileges
Model-specific operations
Use execution hooks (before_all/after_all) for operations that apply to your entire pipeline: setting up assertion tables, granting permissions on all views, sending completion notifications.
Use model-level hooks (pre_statements/post_statements) for operations specific to individual models: creating temporary tables that only one model needs, running model-specific validations.
Last updated
Was this helpful?