Skip to content

Commit

Permalink
Merge pull request #16 from fivetran/feature/union_source_data
Browse files Browse the repository at this point in the history
Feature/union source data
  • Loading branch information
fivetran-reneeli authored Nov 3, 2021
2 parents c262e5b + 9408a75 commit 7633675
Show file tree
Hide file tree
Showing 14 changed files with 113 additions and 38 deletions.
15 changes: 15 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# dbt_xero v0.3.0

## Features
- Allow for multiple sources by unioning source tables across multiple Xero connectors.
([#11](https://github.com/fivetran/dbt_xero_source/pull/11))
- Refer to the unioning multiple Xero connectors section of the [README](https://github.com/fivetran/dbt_xero/tree/main#unioning-multiple-xero-connectors) for more details.

## Under the Hood
- Unioning: The unioning occurs in Xero source package using the `fivetran_utils.union_data` macro. ([#16](https://github.com/fivetran/dbt_xero/pull/16))
- Unique tests: Because columns that were previously used for unique tests may now have duplicate fields across multiple sources, these columns are combined with the new `source_relation` column for unique tests and tested using the `dbt_utils.unique_combination_of_columns` macro. ([#16](https://github.com/fivetran/dbt_xero/pull/16))
- Source Relation column: To distinguish which source each field comes from, we added a new `source_relation` column in each staging model and applied the `fivetran_utils.source_relation` macro. ([#16](https://github.com/fivetran/dbt_xero/pull/16))
- Utils Materialization: We have made the default materialization of the `utils` folder to be ephemeral. ([#16](https://github.com/fivetran/dbt_xero/pull/16))

# dbt_xero v0.1.0 -> v0.2.0
Refer to the relevant release notes on the Github repository for specific details for the previous releases. Thank you!
15 changes: 14 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ Include in your `packages.yml`
```yaml
packages:
- package: fivetran/xero
version: [">=0.2.0", "<0.3.0"]
version: [">=0.3.0", "<0.4.0"]
```
## Configuration
Expand All @@ -47,6 +47,19 @@ vars:
xero_database: your_database_name
```

### Unioning Multiple Xero Connectors
If you have multiple Xero 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** (**note that you cannot use both**) the `union_schemas` or `union_databases` variables:

```yml
# dbt_project.yml
...
config-version: 2
vars:
xero_source:
union_schemas: ['xero_us','xero_ca'] # use this if the data is in different schemas/datasets of the same database/project
union_databases: ['xero_us','xero_ca'] # use this if the data is in different databases/projects but uses the same schema name
```

### Disabling and Enabling Models

When setting up your Xero connection in Fivetran, it is possible that not every table this package expects will be synced. This can occur because you either don't use that functionality in Xero or have actively decided to not sync some tables. In order to disable the relevant functionality in the package, you will need to add the relevant variables.
Expand Down
6 changes: 4 additions & 2 deletions dbt_project.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name: 'xero'
version: '0.2.0'
version: '0.3.0'
config-version: 2
require-dbt-version: [">=0.20.0"]

Expand All @@ -18,4 +18,6 @@ vars:
models:
xero:
+materialized: table
+schema: xero
+schema: xero
utilities:
+materialized: ephemeral
2 changes: 1 addition & 1 deletion docs/catalog.json

Large diffs are not rendered by default.

24 changes: 12 additions & 12 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.

2 changes: 1 addition & 1 deletion integration_tests/dbt_project.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name: 'xero_integration_tests'
version: '0.2.0'
version: '0.3.0'
profile: 'integration_tests'
config-version: 2

Expand Down
41 changes: 37 additions & 4 deletions models/xero.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ models:
- name: profit_and_loss_id
description: Unique identifier for each record in the profit and loss report model.
tests:
- unique
- unique
- not_null
- name: date_month
description: The month of activity.
Expand All @@ -23,9 +23,19 @@ models:
description: The class of account, e.g. ASSET, EQUITY, EXPENSE, LIABILITY, REVENUE
- name: net_amount
description: The net movement amount.
- name: source_relation
description: >
The source where this data was pulled from. If you are making use of the `union_schemas` variable, this will be the source schema.
If you are making use of the `union_databases` variable, this will be the source database. If you are not unioining together multiple
sources, this will be an empty string.
- name: xero__invoice_line_items
description: Each record represents an invoice line item, enriched with account, contact and invoice information.
tests:
- dbt_utils.unique_combination_of_columns:
combination_of_columns:
- line_item_id
- source_relation
columns:
- name: _fivetran_synced
description: "{{ doc('_fivetran_synced') }}"
Expand All @@ -46,7 +56,6 @@ models:
- name: line_item_id
description: Xero identifier for the line item.
tests:
- unique
- not_null
- name: quantity
description: The quantity of the line item.
Expand Down Expand Up @@ -96,14 +105,22 @@ models:
description: The class of account, e.g. ASSET, EQUITY, EXPENSE, LIABILITY, REVENUE
- name: contact_name
description: The name of the associated contact.
- name: source_relation
description: >
The source where this data was pulled from. If you are making use of the `union_schemas` variable, this will be the source schema.
If you are making use of the `union_databases` variable, this will be the source database. If you are not unioining together multiple
sources, this will be an empty string.
- name: xero__general_ledger
description: Each record represents a journal line item. This ledger is used to create the balance sheet and profit and loss.
tests:
- dbt_utils.unique_combination_of_columns:
combination_of_columns:
- journal_line_id
- source_relation
columns:
- name: journal_line_id
description: Xero identifier for the journal line.
tests:
- unique
- name: journal_id
description: Xero identifier for the journal entry
tests:
Expand Down Expand Up @@ -158,9 +175,20 @@ models:
description: ID of the associated contact, where relevant.
- name: contact_name
description: Name of the associated contact, where relevant.
- name: source_relation
description: >
The source where this data was pulled from. If you are making use of the `union_schemas` variable, this will be the source schema.
If you are making use of the `union_databases` variable, this will be the source database. If you are not unioining together multiple
sources, this will be an empty string.
- name: xero__balance_sheet_report
description: Each record represents the state of the balance sheet for a given account on a given month.
tests:
- dbt_utils.unique_combination_of_columns:
combination_of_columns:
- source_relation
- date_month
- account_name
columns:
- name: date_month
description: The month a given record is reporting on.
Expand All @@ -176,3 +204,8 @@ models:
description: The type of the related account.
- name: net_amount
description: The net balance amount.
- name: source_relation
description: >
The source where this data was pulled from. If you are making use of the `union_schemas` variable, this will be the source schema.
If you are making use of the `union_databases` variable, this will be the source database. If you are not unioining together multiple
sources, this will be an empty string.
3 changes: 2 additions & 1 deletion models/xero__balance_sheet_report.sql
Original file line number Diff line number Diff line change
Expand Up @@ -48,12 +48,13 @@ with calendar as (
when ledger.account_class in ('ASSET','EQUITY','LIABILITY') then ledger.account_class
else 'EQUITY'
end as account_class,
ledger.source_relation,
sum(ledger.net_amount) as net_amount
from calendar
inner join ledger
on calendar.date_month >= cast({{ dbt_utils.date_trunc('month', 'ledger.journal_date') }} as date)
cross join year_end
{{ dbt_utils.group_by(6) }}
{{ dbt_utils.group_by(7) }}

)

Expand Down
23 changes: 15 additions & 8 deletions models/xero__general_ledger.sql
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ with journals as (
journals.reference,
journals.source_id,
journals.source_type,
journals.source_relation,

journal_lines.journal_line_id,
journal_lines.account_code,
Expand All @@ -69,9 +70,11 @@ with journals as (

from journals
left join journal_lines
on journals.journal_id = journal_lines.journal_id
on (journals.journal_id = journal_lines.journal_id
and journals.source_relation = journal_lines.source_relation)
left join accounts
on accounts.account_id = journal_lines.account_id
on (accounts.account_id = journal_lines.account_id
and accounts.source_relation = journal_lines.source_relation)

), first_contact as (

Expand All @@ -87,14 +90,17 @@ with journals as (

) as contact_id
from joined
left join invoices
using (invoice_id)
left join invoices
on (joined.invoice_id = invoices.invoice_id
and joined.source_relation = invoices.source_relation)
left join bank_transactions
using (bank_transaction_id)
on (joined.bank_transaction_id = bank_transactions.bank_transaction_id
and joined.source_relation = bank_transactions.source_relation)

{% if var('xero__using_credit_note', True) %}
left join credit_notes
using (credit_note_id)
left join credit_notes
on (joined.credit_note_id = credit_notes.credit_note_id
and joined.source_relation = credit_notes.source_relation)
{% endif %}

), second_contact as (
Expand All @@ -104,7 +110,8 @@ with journals as (
contacts.contact_name
from first_contact
left join contacts
using (contact_id)
on (first_contact.contact_id = contacts.contact_id
and first_contact.source_relation = contacts.source_relation)

)

Expand Down
9 changes: 6 additions & 3 deletions models/xero__invoice_line_items.sql
Original file line number Diff line number Diff line change
Expand Up @@ -47,11 +47,14 @@ with line_items as (

from line_items
left join invoices
using (invoice_id)
on (line_items.invoice_id = invoices.invoice_id
and line_items.source_relation = invoices.source_relation)
left join accounts
using (account_code)
on (line_items.account_code = accounts.account_code
and line_items.source_relation = accounts.source_relation)
left join contacts
using (contact_id)
on (invoices.contact_id = contacts.contact_id
and invoices.source_relation = contacts.source_relation)

)

Expand Down
5 changes: 3 additions & 2 deletions models/xero__profit_and_loss_report.sql
Original file line number Diff line number Diff line change
Expand Up @@ -11,19 +11,20 @@ with calendar as (
), joined as (

select
{{ dbt_utils.surrogate_key(['calendar.date_month','ledger.account_id']) }} as profit_and_loss_id,
{{ dbt_utils.surrogate_key(['calendar.date_month','ledger.account_id','ledger.source_relation']) }} as profit_and_loss_id,
calendar.date_month,
ledger.account_id,
ledger.account_name,
ledger.account_code,
ledger.account_type,
ledger.account_class,
ledger.source_relation,
coalesce(sum(ledger.net_amount * -1),0) as net_amount
from calendar
left join ledger
on calendar.date_month = cast({{ dbt_utils.date_trunc('month', 'ledger.journal_date') }} as date)
where ledger.account_class in ('REVENUE','EXPENSE')
{{ dbt_utils.group_by(7) }}
{{ dbt_utils.group_by(8) }}

)

Expand Down
2 changes: 1 addition & 1 deletion packages.yml
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
packages:
- package: fivetran/xero_source
version: [">=0.2.0","<0.3.0"]
version: [">=0.3.0","<0.4.0"]

0 comments on commit 7633675

Please sign in to comment.