Skip to content

Commit

Permalink
Merge pull request #45 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 committed Oct 12, 2023
2 parents 6c4c1ef + 4fb2bb6 commit 536a8a6
Show file tree
Hide file tree
Showing 34 changed files with 291 additions and 60 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")
3 changes: 2 additions & 1 deletion .buildkite/pipeline.yml
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ steps:
commands: |
bash .buildkite/scripts/run_models.sh redshift
- label: ":bricks: Run Tests - Databricks"
- label: ":databricks: Run Tests - Databricks"
key: "run_dbt_databricks"
plugins:
- docker#v3.13.0:
Expand All @@ -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
17 changes: 17 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,20 @@
# dbt_apple_search_ads_source v0.3.0
[PR #45](https://github.com/fivetran/dbt_apple_search_ads_source/pull/45) includes the following updates:
## Breaking changes
- Updated the source identifier format for consistency with their respective source names and compatibility with the `fivetran_utils.union_data` macro. If you are using the previous identifier, be sure to update to the current version! Identifiers now are:

| current | previous |
|----------|----------|
|apple_search_ads_ad_level_report_identifier | apple_search_ads_ad_report_identifier |

## Feature update 🎉
- Unioning capability! This adds the ability to union source data from multiple apple_search_ads connectors. Refer to the [Union Multiple Connectors README section](https://github.com/fivetran/dbt_apple_search_ads_source/blob/main/README.md#union-multiple-connectors) for more details.

## 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.

# dbt_apple_search_ads_source v0.2.2
## Bug Fix
- The `apple_search_ads__search_term_passthrough_metrics` variable was moved to be within the return call of the `get_search_term_report_columns` macro. The previous placement caused an issue where the passthrough metrics would not properly persist to downstream models. ([PR #42](https://github.com/fivetran/dbt_apple_search_ads_source/pull/42))
Expand Down
16 changes: 12 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ If you are **not** using the [Apple Search Ads transformation package](https://
```yaml
packages:
- package: fivetran/apple_search_ads_source
version: [">=0.2.0", "<0.3.0"] # we recommend using ranges to capture non-breaking changes automatically
version: [">=0.3.0", "<0.4.0"] # we recommend using ranges to capture non-breaking changes automatically
```
## Step 3: Define database and schema variables
By default, this package runs using your destination and the `apple_search_ads` schema. If this is not where your Apple Search Ads data is (for example, if your Apple Search Ads schema is named `apple_search_ads_fivetran`), add the following configuration to your root `dbt_project.yml` file:
Expand All @@ -51,7 +51,17 @@ vars:
```

## (Optional) Step 4: Additional configurations
<details><summary>Expand for configurations</summary>
### Union multiple connectors
If you have multiple apple_search_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 `apple_search_ads_union_schemas` OR `apple_search_ads_union_databases` variables (cannot do both) in your root `dbt_project.yml` file:

```yml
vars:
apple_search_ads_union_schemas: ['apple_search_ads_usa','apple_search_ads_canada'] # use this if the data is in different schemas/datasets of the same database/project
apple_search_ads_union_databases: ['apple_search_ads_usa','apple_search_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 @@ -108,8 +118,6 @@ vars:
apple_search_ads_<default_source_table_name>_identifier: your_table_name
```

</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: 'apple_search_ads_source'
version: '0.2.2'
version: '0.3.0'

config-version: 2
require-dbt-version: [">=1.3.0", "<2.0.0"]
Expand Down
2 changes: 1 addition & 1 deletion docs/catalog.json

Large diffs are not rendered by default.

6 changes: 3 additions & 3 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: apple_ads_source_integrations_tests_1
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: apple_ads_source_integrations_tests_1
threads: 2
threads: 8
token: "{{ env_var('CI_DATABRICKS_DBT_TOKEN') }}"
type: databricks
4 changes: 2 additions & 2 deletions integration_tests/dbt_project.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
config-version: 2

name: 'apple_search_ads_source_integration_tests'
version: '0.2.2'
version: '0.3.0'

profile: 'integration_tests'

Expand All @@ -13,7 +13,7 @@ vars:
apple_search_ads_ad_group_history_identifier: "ad_group_history_data"
apple_search_ads_ad_group_report_identifier: "ad_group_report_data"
apple_search_ads_ad_history_identifier: "ad_history_data"
apple_search_ads_ad_report_identifier: "ad_report_data"
apple_search_ads_ad_level_report_identifier: "ad_report_data"
apple_search_ads_keyword_history_identifier: "keyword_history_data"
apple_search_ads_keyword_report_identifier: "keyword_report_data"
apple_search_ads_search_term_report_identifier: "search_term_report_data"
Expand Down
3 changes: 3 additions & 0 deletions models/docs.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{% docs source_relation %}
The source of the record if the unioning functionality is being used. If not this field will be empty.
{% enddocs %}
4 changes: 2 additions & 2 deletions models/src_apple_search_ads.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
version: 2

sources:
- name: apple_search_ads
- name: apple_search_ads # This source will only be used if you are using a single apple_search_ads source connector. If multiple sources are being unioned, their tables will be directly referenced via adapter.get_relation.
schema: "{{ var('apple_search_ads_schema', 'apple_search_ads') }}"
database: "{% if target.type != 'spark' %}{{ var('apple_search_ads_database', target.database) }}{% endif %}"

Expand Down Expand Up @@ -82,7 +82,7 @@ sources:

- name: ad_level_report
description: Each record represents the daily performance of an ad in Apple Search Ads.
identifier: "{{ var('apple_search_ads_ad_report_identifier', 'ad_level_report') }}"
identifier: "{{ var('apple_search_ads_ad_level_report_identifier', 'ad_level_report') }}"
columns:
- name: date
description: Date for daily report aggregation.
Expand Down
30 changes: 30 additions & 0 deletions models/stg_apple_search_ads.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_group_id
- modified_at
columns:
- name: source_relation
description: "{{ doc('source_relation') }}"
- name: modified_at
description: Timestamp of when the ad group object was last modified.
tests:
Expand Down Expand Up @@ -37,9 +40,12 @@ models:
tests:
- dbt_utils.unique_combination_of_columns:
combination_of_columns:
- source_relation
- ad_group_id
- date_day
columns:
- name: source_relation
description: "{{ doc('source_relation') }}"
- name: ad_group_id
description: Ad group ID associated with the record.
tests:
Expand All @@ -66,9 +72,12 @@ models:
tests:
- dbt_utils.unique_combination_of_columns:
combination_of_columns:
- source_relation
- ad_id
- modified_at
columns:
- name: source_relation
description: "{{ doc('source_relation') }}"
- name: created_at
description: Timestamp of when ad was created.
- name: modified_at
Expand All @@ -95,10 +104,13 @@ models:
tests:
- dbt_utils.unique_combination_of_columns:
combination_of_columns:
- source_relation
- ad_id
- date_day
- ad_group_id
columns:
- name: source_relation
description: "{{ doc('source_relation') }}"
- name: date_day
description: Date for daily report aggregation.
tests:
Expand Down Expand Up @@ -131,9 +143,12 @@ models:
tests:
- dbt_utils.unique_combination_of_columns:
combination_of_columns:
- source_relation
- campaign_id
- modified_at
columns:
- name: source_relation
description: "{{ doc('source_relation') }}"
- name: modified_at
description: Timestamp of when the campaign object was last modified.
tests:
Expand All @@ -160,9 +175,12 @@ models:
tests:
- dbt_utils.unique_combination_of_columns:
combination_of_columns:
- source_relation
- campaign_id
- date_day
columns:
- name: source_relation
description: "{{ doc('source_relation') }}"
- name: campaign_id
description: Campaign ID associated with the record.
tests:
Expand Down Expand Up @@ -190,9 +208,12 @@ models:
tests:
- dbt_utils.unique_combination_of_columns:
combination_of_columns:
- source_relation
- keyword_id
- modified_at
columns:
- name: source_relation
description: "{{ doc('source_relation') }}"
- name: modified_at
description: Timestamp of when the keyword object was last modified.
tests:
Expand Down Expand Up @@ -223,9 +244,12 @@ models:
tests:
- dbt_utils.unique_combination_of_columns:
combination_of_columns:
- source_relation
- keyword_id
- date_day
columns:
- name: source_relation
description: "{{ doc('source_relation') }}"
- name: keyword_id
description: Keyword ID associated with the record.
tests:
Expand All @@ -252,8 +276,11 @@ models:
tests:
- dbt_utils.unique_combination_of_columns:
combination_of_columns:
- source_relation
- organization_id
columns:
- name: source_relation
description: "{{ doc('source_relation') }}"
- name: organization_id
description: Organization ID associated with the record.
tests:
Expand All @@ -272,9 +299,12 @@ models:
tests:
- dbt_utils.unique_combination_of_columns:
combination_of_columns:
- source_relation
- _fivetran_id
- date_day
columns:
- name: source_relation
description: "{{ doc('source_relation') }}"
- name: _fivetran_id
description: A Fivetran internal unique id that helps us avoid duplicate rows in primary keyless tables.
tests:
Expand Down
12 changes: 10 additions & 2 deletions models/stg_apple_search_ads__ad_group_history.sql
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,19 @@ fields as (
)
}}


{{ fivetran_utils.source_relation(
union_schema_variable='apple_search_ads_union_schemas',
union_database_variable='apple_search_ads_union_databases')
}}

from base
),

final as (
select

select
source_relation,
modification_time as modified_at,
organization_id,
campaign_id,
Expand All @@ -29,7 +37,7 @@ final as (
status as ad_group_status,
start_time as start_at,
end_time as end_at,
row_number() over (partition by id order by modification_time desc) = 1 as is_most_recent_record
row_number() over (partition by source_relation, id order by modification_time desc) = 1 as is_most_recent_record
from fields
)

Expand Down
9 changes: 8 additions & 1 deletion models/stg_apple_search_ads__ad_group_report.sql
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,19 @@ fields as (
staging_columns=get_ad_group_report_columns()
)
}}

{{ fivetran_utils.source_relation(
union_schema_variable='apple_search_ads_union_schemas',
union_database_variable='apple_search_ads_union_databases')
}}

from base
),

final as (

select
source_relation,
date as date_day,
ad_group_id,
impressions,
Expand Down
13 changes: 10 additions & 3 deletions models/stg_apple_search_ads__ad_history.sql
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,19 @@ fields as (
staging_columns=get_ad_history_columns()
)
}}

{{ fivetran_utils.source_relation(
union_schema_variable='apple_search_ads_union_schemas',
union_database_variable='apple_search_ads_union_databases')
}}

from base
),

final as (

select

select
source_relation,
creation_time as created_at,
modification_time as modified_at,
org_id as organization_id,
Expand All @@ -29,7 +36,7 @@ final as (
name as ad_name,
id as ad_id,
status as ad_status,
row_number() over (partition by id order by modification_time desc) = 1 as is_most_recent_record
row_number() over (partition by source_relation, id order by modification_time desc) = 1 as is_most_recent_record
from fields
)

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

{{ fivetran_utils.source_relation(
union_schema_variable='apple_search_ads_union_schemas',
union_database_variable='apple_search_ads_union_databases')
}}

from base
),

final as (

select

select
source_relation,
date as date_day,
campaign_id,
ad_group_id,
Expand Down
Loading

0 comments on commit 536a8a6

Please sign in to comment.