As fans of dbt , we’re quite happy with the new Jinja2 template support for EXECUTE IMMEDIATE FROM !
This time our intern got the following assignement :
Create a sql script that using Jinja2 templating that does the following based on my input :
- Deployment Differentiation:
- Dynamically adapts to different deployment types (
prod
and non-prod
). - Uses distinct environment names (
prod1
,prod2
for production anddev
,qa
,staging
for non-production).
- Dynamically adapts to different deployment types (
- Schema Creation:
- Creates specific schemas (
finance
,sales
,hr
for production anddevelopment
,testing
,support
for non-production).
- Creates specific schemas (
- Database Setup:
- Creates databases named according to the environment (
{{ environment }}_db
).
- Creates databases named according to the environment (
- Schema-Specific Table Creation:
- Creates multiple tables within each schema according to the environment (
{{ environment }}_{{schema}}_employees
for example):orders
table with columnsid
,item
,quantity
, andorder_date
.customers
table with columnsid
,name
, andemail
.products
table with columnsproduct_id
,product_name
, andprice
.employees
table with columnsemployee_id
,employee_name
, andposition
- Creates multiple tables within each schema according to the environment (
- Production-Specific Setup:
- Creates a separate
prod_analytics_db
database. - Within this database, creates an
analytics_reports
table with columnsreport_id
,report_name
, andcreated_date
.
- Creates a separate
Professor Frosties input should be similar to the following :
EXECUTE IMMEDIATE FROM @setup_stage/setup_env_made_by_intern.sql
USING (DEPLOYMENT_TYPE => ‘prod’);
mferle says
A bit tedious to debug because of going back and forth with uploading the file to the stage, but not that difficult overall. Very handy to have jinja templating for deployments!
ChrisHastie says
This one took me longer than usual. Haven’t used jinja before and it was a fun learning experience! Thanks for keeping the interesting challenges coming