I have a schema with tables that have the same structure for different accounts that I am dynamically unioning using Jinja.
I would like to make the source configuration dynamic so if we add new accounts in the future, the source config (freshness, tests) will automatically run on these tables.
You can probably just skip the step of creating sources for all of these tables. You lose a little bit of observability, but gain a lot of productivity.
dbt_utils has a macro called get_relations_by_pattern to easily build new Relations for matching tables in your database: dbt-labs/dbt-utils: get_relations_by_pattern. (github.com). The name is a little misleading – you don’t use this to get relations that are already defined in the graph, you use it to construct new Relation objects out of tables in your database.
Then in lieu of your source freshness test, you can create a custom data test that checks for the same thing. The union_relations macro adds a column called _dbt_source_relation that contains the name of the source table, which is useful here:
-- tests/test_advertising_data_freshness
select _dbt_source_relation, max(updated_at)
from {{ ref('stg__advertising_data') }}
group by 1
having max(updated_at) < sysdate() - interval '12 hours'