in tasks.py [0:0]
def apply_config_parameters_to_all_procedures(c, env_name="prod"):
"""
Applies parameters from a configuration file to .sqlx stored procedure template files,
generating new .sql files for each template.
Args:
c (object): An object providing context for task execution (e.g., task runner).
env_name (str, optional): Name of the environment to use for configuration. Defaults to "prod".
Raises:
FileNotFoundError: If the specified configuration file is not found.
ValueError: If the configuration file is invalid or missing required keys.
TemplateError: If an error occurs during template rendering.
Example usage:
apply_config_parameters_to_all_procedures(my_task_context, env_name="dev")
Customization:
1) Add a new parameter to .sqlx template file: Add the placeholder:
a) Insert the new parameter's placeholder within the template file, surrounded
by Jinja2's delimiters (e.g., {{ new_parameter }}) where you want its value to appear
in the rendered output.
b) Add a new key-value pair for the new parameter under the appropriate section of
your config.yaml.tftpl YAML configuration file. Ensure the key matches the placeholder
name in the template.
c) No code changes required: The existing code already retrieves configuration values
and renders them into templates, so it should handle the new parameter without modification.
"""
# Load configuration file according to environment name
current_path = Path(__file__).parent.resolve()
conf = yaml.safe_load(Path.joinpath(current_path,"config", "{}.yaml".format(env_name)).read_text())
# Fetch all procedure configs <key,value> to be applied
procedure_dict = conf['bigquery']['procedure']
# Locate file path for all templates to be used
template_path = Path.joinpath(current_path,"sql","procedure")
templateLoader = FileSystemLoader(searchpath=template_path)
templateEnv = Environment(loader=templateLoader)
# For each file name, apply the config values to the template
for template_file in template_path.iterdir():
if template_file.is_file() and template_file.resolve().suffix == '.sqlx':
template = templateEnv.get_template(template_file.name)
new_sql = template.render(procedure_dict[template_file.stem])
rendered_sql_file = Path.joinpath(template_path, template_file.resolve().stem+".sql")
with rendered_sql_file.open("w+", encoding ="utf-8") as f:
f.write(new_sql)
print("New SQL file rendered at {}".format(rendered_sql_file))