Skip to content

Commit

Permalink
Merge pull request #20 from fivetran/MagicBot/add-union-schema
Browse files Browse the repository at this point in the history
Feature: Union schema compatibility
  • Loading branch information
fivetran-catfritz authored Oct 12, 2023
2 parents 868e593 + 76a5429 commit 08c6129
Show file tree
Hide file tree
Showing 32 changed files with 264 additions and 62 deletions.
3 changes: 2 additions & 1 deletion .buildkite/hooks/pre-command
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,5 @@ export CI_SNOWFLAKE_DBT_USER=$(gcloud secrets versions access latest --secret="C
export CI_SNOWFLAKE_DBT_WAREHOUSE=$(gcloud secrets versions access latest --secret="CI_SNOWFLAKE_DBT_WAREHOUSE" --project="dbt-package-testing-363917")
export CI_DATABRICKS_DBT_HOST=$(gcloud secrets versions access latest --secret="CI_DATABRICKS_DBT_HOST" --project="dbt-package-testing-363917")
export CI_DATABRICKS_DBT_HTTP_PATH=$(gcloud secrets versions access latest --secret="CI_DATABRICKS_DBT_HTTP_PATH" --project="dbt-package-testing-363917")
export CI_DATABRICKS_DBT_TOKEN=$(gcloud secrets versions access latest --secret="CI_DATABRICKS_DBT_TOKEN" --project="dbt-package-testing-363917")
export CI_DATABRICKS_DBT_TOKEN=$(gcloud secrets versions access latest --secret="CI_DATABRICKS_DBT_TOKEN" --project="dbt-package-testing-363917")
export CI_DATABRICKS_DBT_CATALOG=$(gcloud secrets versions access latest --secret="CI_DATABRICKS_DBT_CATALOG" --project="dbt-package-testing-363917")
1 change: 1 addition & 0 deletions .buildkite/pipeline.yml
Original file line number Diff line number Diff line change
Expand Up @@ -69,5 +69,6 @@ steps:
- "CI_DATABRICKS_DBT_HOST"
- "CI_DATABRICKS_DBT_HTTP_PATH"
- "CI_DATABRICKS_DBT_TOKEN"
- "CI_DATABRICKS_DBT_CATALOG"
commands: |
bash .buildkite/scripts/run_models.sh databricks
16 changes: 12 additions & 4 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,16 @@
# dbt_snapchat_ads_source v0.UPDATE.UPDATE
# dbt_snapchat_ads_source v0.6.0
[PR #20](https://github.com/fivetran/dbt_snapchat_ads_source/pull/20) includes the following updates:
## Feature update 🎉
- Unioning capability! This adds the ability to union source data from multiple snapchat_ads connectors. Refer to the [Union Multiple Connectors README section](https://github.com/fivetran/dbt_snapchat_ads_source/blob/main/README.md#union-multiple-connectors) for more details.

## Under the Hood:
- Incorporated the new `fivetran_utils.drop_schemas_automation` macro into the end of each Buildkite integration test job. ([#17](https://github.com/fivetran/dbt_snapchat_ads_source/pull/17))
- Updated the pull request [templates](/.github). ([#17](https://github.com/fivetran/dbt_snapchat_ads_source/pull/17))
## Under the hood 🚘
- Updated tmp models to union source data using the `fivetran_utils.union_data` macro.
- To distinguish which source each field comes from, added `source_relation` column in each staging model and applied the `fivetran_utils.source_relation` macro.
- Updated tests to account for the new `source_relation` column.

[PR #17](https://github.com/fivetran/dbt_snapchat_ads_source/pull/17) includes the following updates:
- Incorporated the new `fivetran_utils.drop_schemas_automation` macro into the end of each Buildkite integration test job.
- Updated the pull request [templates](/.github).

# dbt_snapchat_ads_source v0.5.0
## 🚨 Breaking Changes 🚨:
Expand Down
16 changes: 12 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ If you are **not** using the [Snapchat Ads transformation package](https://gith
```yaml
packages:
- package: fivetran/snapchat_ads_source
version: [">=0.5.0", "<0.6.0"] # we recommend using ranges to capture non-breaking changes automatically
version: [">=0.6.0", "<0.7.0"] # we recommend using ranges to capture non-breaking changes automatically
```
## Step 3: Configure your variables

Expand All @@ -54,7 +54,17 @@ vars:
```

## (Optional) Step 4: Additional configurations
<details><summary>Expand for configurations</summary>
### Union multiple connectors
If you have multiple snapchat_ads connectors in Fivetran and would like to use this package on all of them simultaneously, we have provided functionality to do so. The package will union all of the data together and pass the unioned table into the transformations. You will be able to see which source it came from in the `source_relation` column of each model. To use this functionality, you will need to set either the `snapchat_ads_union_schemas` OR `snapchat_ads_union_databases` variables (cannot do both) in your root `dbt_project.yml` file:

```yml
vars:
snapchat_ads_union_schemas: ['snapchat_ads_usa','snapchat_ads_canada'] # use this if the data is in different schemas/datasets of the same database/project
snapchat_ads_union_databases: ['snapchat_ads_usa','snapchat_ads_canada'] # use this if the data is in different databases/projects but uses the same schema name
```
Please be aware that the native `source.yml` connection set up in the package will not function when the union schema/database feature is utilized. Although the data will be correctly combined, you will not observe the sources linked to the package models in the Directed Acyclic Graph (DAG). This happens because the package includes only one defined `source.yml`.

To connect your multiple schema/database sources to the package models, follow the steps outlined in the [Union Data Defined Sources Configuration](https://github.com/fivetran/dbt_fivetran_utils/tree/releases/v0.4.latest#union_data-source) section of the Fivetran Utils documentation for the union_data macro. This will ensure a proper configuration and correct visualization of connections in the DAG.

### Passing Through Additional Metrics
By default, this package will select `clicks`, `impressions`, and `cost` from the source reporting tables to store into the staging models. If you would like to pass through additional metrics to the staging models, add the below configurations to your `dbt_project.yml` file. These variables allow for the pass-through fields to be aliased (`alias`) if desired, but not required. Use the below format for declaring the respective pass-through variables:
Expand Down Expand Up @@ -91,8 +101,6 @@ models:
+schema: my_new_schema_name # leave blank for just the target_schema
```

</details>

## (Optional) Step 5: Orchestrate your models with Fivetran Transformations for dbt Core™

<details><summary>Expand for more details</summary>
Expand Down
2 changes: 1 addition & 1 deletion dbt_project.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name: 'snapchat_ads_source'
version: '0.5.0'
version: '0.6.0'
config-version: 2
require-dbt-version: [">=1.3.0", "<2.0.0"]
vars:
Expand Down
2 changes: 1 addition & 1 deletion docs/catalog.json

Large diffs are not rendered by default.

8 changes: 4 additions & 4 deletions docs/index.html

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion docs/manifest.json

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion docs/run_results.json

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions integration_tests/ci/sample.profiles.yml
Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,10 @@ integration_tests:
schema: snapchat_ads_source_integration_tests
threads: 8
databricks:
catalog: null
catalog: "{{ env_var('CI_DATABRICKS_DBT_CATALOG') }}"
host: "{{ env_var('CI_DATABRICKS_DBT_HOST') }}"
http_path: "{{ env_var('CI_DATABRICKS_DBT_HTTP_PATH') }}"
schema: snapchat_ads_source_integration_tests
threads: 2
threads: 8
token: "{{ env_var('CI_DATABRICKS_DBT_TOKEN') }}"
type: databricks
2 changes: 1 addition & 1 deletion integration_tests/dbt_project.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name: 'snapchat_ads_source_integration_tests'

version: '0.5.0'
version: '0.6.0'
profile: 'integration_tests'
config-version: 2

Expand Down
6 changes: 5 additions & 1 deletion models/docs.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
{% docs _fivetran_synced -%} When the record was last synced by Fivetran. {%- enddocs %}

{% docs is_most_recent_record %} Boolean representing whether a record is the most recent version of that record. {% enddocs %}
{% docs is_most_recent_record %} Boolean representing whether a record is the most recent version of that record. {% enddocs %}

{% docs source_relation %}
The source of the record if the unioning functionality is being used. If not this field will be empty.
{% enddocs %}
2 changes: 1 addition & 1 deletion models/src_snapchat.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
version: 2

sources:
- name: snapchat_ads
- name: snapchat_ads # This source will only be used if you are using a single tiktok_ads source connector. If multiple sources are being unioned, their tables will be directly referenced via adapter.get_relation.
schema: "{{ var('snapchat_ads_schema', 'snapchat_ads') }}"
database: "{% if target.type != 'spark'%}{{ var('snapchat_ads_database', target.database) }}{% endif %}"
loader: Fivetran
Expand Down
27 changes: 27 additions & 0 deletions models/stg_snapchat.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,12 @@ models:
tests:
- dbt_utils.unique_combination_of_columns:
combination_of_columns:
- source_relation
- ad_account_id
- _fivetran_synced
columns:
- name: source_relation
description: "{{ doc('source_relation') }}"
- name: _fivetran_synced
description: "{{ doc('_fivetran_synced') }}"
tests:
Expand Down Expand Up @@ -37,9 +40,12 @@ models:
tests:
- dbt_utils.unique_combination_of_columns:
combination_of_columns:
- source_relation
- ad_id
- _fivetran_synced
columns:
- name: source_relation
description: "{{ doc('source_relation') }}"
- name: _fivetran_synced
description: "{{ doc('_fivetran_synced') }}"
tests:
Expand All @@ -66,9 +72,12 @@ models:
tests:
- dbt_utils.unique_combination_of_columns:
combination_of_columns:
- source_relation
- ad_id
- date_hour
columns:
- name: source_relation
description: "{{ doc('source_relation') }}"
- name: ad_id
description: The ID of the ad that the report relates to in Snapchat.
tests:
Expand Down Expand Up @@ -117,9 +126,12 @@ models:
tests:
- dbt_utils.unique_combination_of_columns:
combination_of_columns:
- source_relation
- ad_squad_id
- _fivetran_synced
columns:
- name: source_relation
description: "{{ doc('source_relation') }}"
- name: _fivetran_synced
description: "{{ doc('_fivetran_synced') }}"
tests:
Expand All @@ -144,9 +156,12 @@ models:
tests:
- dbt_utils.unique_combination_of_columns:
combination_of_columns:
- source_relation
- ad_squad_id
- date_hour
columns:
- name: source_relation
description: "{{ doc('source_relation') }}"
- name: ad_squad_id
description: ID of the ad squad.
tests:
Expand Down Expand Up @@ -195,9 +210,12 @@ models:
tests:
- dbt_utils.unique_combination_of_columns:
combination_of_columns:
- source_relation
- campaign_id
- _fivetran_synced
columns:
- name: source_relation
description: "{{ doc('source_relation') }}"
- name: _fivetran_synced
description: "{{ doc('_fivetran_synced') }}"
tests:
Expand All @@ -222,9 +240,12 @@ models:
tests:
- dbt_utils.unique_combination_of_columns:
combination_of_columns:
- source_relation
- campaign_id
- date_hour
columns:
- name: source_relation
description: "{{ doc('source_relation') }}"
- name: campaign_id
description: ID of campaign
tests:
Expand Down Expand Up @@ -273,9 +294,12 @@ models:
tests:
- dbt_utils.unique_combination_of_columns:
combination_of_columns:
- source_relation
- creative_id
- _fivetran_synced
columns:
- name: source_relation
description: "{{ doc('source_relation') }}"
- name: _fivetran_synced
description: "{{ doc('_fivetran_synced') }}"
tests:
Expand All @@ -302,10 +326,13 @@ models:
tests:
- dbt_utils.unique_combination_of_columns:
combination_of_columns:
- source_relation
- creative_id
- param_key
- updated_at
columns:
- name: source_relation
description: "{{ doc('source_relation') }}"
- name: creative_id
description: The ID of the related creative in Snapchat.
tests:
Expand Down
13 changes: 10 additions & 3 deletions models/stg_snapchat_ads__ad_account_history.sql
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,19 @@ fields as (
staging_columns=get_ad_account_history_columns()
)
}}

{{ fivetran_utils.source_relation(
union_schema_variable='snapchat_ads_union_schemas',
union_database_variable='snapchat_ads_union_databases')
}}

from base
),

final as (

select

select
source_relation,
id as ad_account_id,
name as ad_account_name,
cast (created_at as {{ dbt.type_timestamp() }}) as created_at,
Expand All @@ -29,7 +36,7 @@ final as (
timezone,
cast (_fivetran_synced as {{ dbt.type_timestamp() }}) as _fivetran_synced,
cast (updated_at as {{ dbt.type_timestamp() }}) as updated_at,
row_number() over (partition by id order by _fivetran_synced desc) = 1 as is_most_recent_record
row_number() over (partition by source_relation, id order by _fivetran_synced desc) = 1 as is_most_recent_record
from fields
)

Expand Down
13 changes: 10 additions & 3 deletions models/stg_snapchat_ads__ad_history.sql
Original file line number Diff line number Diff line change
Expand Up @@ -15,20 +15,27 @@ fields as (
staging_columns=get_ad_history_columns()
)
}}

{{ fivetran_utils.source_relation(
union_schema_variable='snapchat_ads_union_schemas',
union_database_variable='snapchat_ads_union_databases')
}}

from base
),

final as (

select

select
source_relation,
id as ad_id,
name as ad_name,
cast (created_at as {{ dbt.type_timestamp() }}) as created_at,
ad_squad_id,
creative_id,
cast (_fivetran_synced as {{ dbt.type_timestamp() }}) as _fivetran_synced,
cast (updated_at as {{ dbt.type_timestamp() }}) as updated_at,
row_number() over (partition by id order by _fivetran_synced desc) = 1 as is_most_recent_record
row_number() over (partition by source_relation, id order by _fivetran_synced desc) = 1 as is_most_recent_record
from fields
)

Expand Down
11 changes: 9 additions & 2 deletions models/stg_snapchat_ads__ad_hourly_report.sql
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,19 @@ fields as (
staging_columns=get_ad_hourly_report_columns()
)
}}

{{ fivetran_utils.source_relation(
union_schema_variable='snapchat_ads_union_schemas',
union_database_variable='snapchat_ads_union_databases')
}}

from base
),

final as (

select

select
source_relation,
ad_id,
cast (date as {{ dbt.type_timestamp() }}) as date_hour,
attachment_quartile_1,
Expand Down
11 changes: 9 additions & 2 deletions models/stg_snapchat_ads__ad_squad_history.sql
Original file line number Diff line number Diff line change
Expand Up @@ -15,19 +15,26 @@ fields as (
staging_columns=get_ad_squad_history_columns()
)
}}

{{ fivetran_utils.source_relation(
union_schema_variable='snapchat_ads_union_schemas',
union_database_variable='snapchat_ads_union_databases')
}}

from base
),

final as (

select
select
source_relation,
id as ad_squad_id,
name as ad_squad_name,
cast (created_at as {{ dbt.type_timestamp() }}) as created_at,
campaign_id,
cast (_fivetran_synced as {{ dbt.type_timestamp() }}) as _fivetran_synced,
cast (updated_at as {{ dbt.type_timestamp() }}) as updated_at,
row_number() over (partition by id order by _fivetran_synced desc) = 1 as is_most_recent_record
row_number() over (partition by source_relation, id order by _fivetran_synced desc) = 1 as is_most_recent_record
from fields
)

Expand Down
11 changes: 9 additions & 2 deletions models/stg_snapchat_ads__ad_squad_hourly_report.sql
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,19 @@ fields as (
staging_columns=get_ad_squad_hourly_report_columns()
)
}}

{{ fivetran_utils.source_relation(
union_schema_variable='snapchat_ads_union_schemas',
union_database_variable='snapchat_ads_union_databases')
}}

from base
),

final as (

select

select
source_relation,
ad_squad_id,
cast (date as {{ dbt.type_timestamp() }}) as date_hour,
attachment_quartile_1,
Expand Down
Loading

0 comments on commit 08c6129

Please sign in to comment.