diff --git a/dbt/include/postgres/macros/adapters.sql b/dbt/include/postgres/macros/adapters.sql index 4787ff24..33db8987 100644 --- a/dbt/include/postgres/macros/adapters.sql +++ b/dbt/include/postgres/macros/adapters.sql @@ -44,8 +44,7 @@ date_trunc('{{ partition_by_granularity }}', min({{ partition_by_field }})), date_trunc('{{ partition_by_granularity }}', max({{ partition_by_field }})), '1 {{ partition_by_granularity }}'::interval - ) as begin_date, - (select floor(random() * 100 + 1)::int) as rand -- Random number to avoid name colissions + ) as begin_date from ( {{ sql }} ) model_subq @@ -109,11 +108,7 @@ {% set existing_partitions_query %} select inhrelid::regclass::text as from_table_name, - replace( - inhrelid::regclass::text, - '{{ from_relation.schema }}.{{ from_relation.identifier }}', -- Current partition name - '{{ to_relation.identifier }}' -- New partition name - ) as to_table_name + regexp_substr(inhrelid::regclass::text, '[^_]*$') as partition_suffix -- Get the string after the last underscore from pg_catalog.pg_inherits where inhparent = '{{ from_relation.schema }}.{{ from_relation.identifier }}'::regclass; {% endset %} @@ -121,7 +116,9 @@ -- Rename the existing partitions {% for existing_partition in existing_partitions_results.rows %} - alter table {{ existing_partition["from_table_name"] }} rename to {{ existing_partition["to_table_name"] }}; + {%- set partition_relation = make_intermediate_relation(to_relation, existing_partition['partition_suffix']) %} + + alter table {{ existing_partition["from_table_name"] }} rename to {{ partition_relation.identifier }}; {% endfor %} {%- endcall %} {% endmacro %}