Skip to content

Commit

Permalink
Merge pull request #389 from fishtown-analytics/development
Browse files Browse the repository at this point in the history
merge dev into master so docs are up-to-date
  • Loading branch information
drewbanin authored Apr 20, 2017
2 parents 9bb3a2a + 053e4a5 commit fe4647f
Show file tree
Hide file tree
Showing 4 changed files with 94 additions and 2 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@

### Changes

- Changed: different syntax for "relationships" schema tests ([#339](https://github.com/fishtown-analytics/dbt/pull/339))
- Added: `already_exists` context function ([#372](https://github.com/fishtown-analytics/dbt/pull/372))
- Graph refactor: fix common issues with load order ([#292](https://github.com/fishtown-analytics/dbt/pull/292))
- Graph refactor: multiple references to an ephemeral models should share a CTE ([#316](https://github.com/fishtown-analytics/dbt/pull/316))
Expand Down
2 changes: 1 addition & 1 deletion dbt/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ def ref_invalid_args(model, args):


def ref_bad_context(model, target_model_name, target_model_package):
ref_string = "{{ ref('" + target_model_name + "}') }}}}"
ref_string = "{{ ref('" + target_model_name + "') }}"

if target_model_package is not None:
ref_string = ("{{ ref('" + target_model_package +
Expand Down
91 changes: 91 additions & 0 deletions docs/guide/context-variables.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,97 @@ Besides `{{ this }}`, there are a number of other helpful context variables avai
| invocation_id | A UUID generated for this dbt run (useful for auditing) |


## adapter

`adapter` makes available some internal dbt functions that are useful for implementing custom logic in your
dbt models. For example, you could write your own advanced incremental model using code like:

```sql
-- some_model.sql
select * from {{ref('raw_table')}}
{% if adapter.already_exists(this.schema, this.name) %}
where id > (select max(id) from {{this}})
{% endif %}
```

Functions are documented below.

---

#### adapter.already_exists

__Args__:

* `schema`: The schema to test
* `table`: The relation to look for

Returns true if a relation named like `table` exists in schema `schema`, false otherwise.

__Example__:
```sql
select * from {{ref('raw_table')}}
{% if adapter.already_exists(this.schema, this.name) %}
where id > (select max(id) from {{this}})
{% endif %}
```

([Source on Github](https://github.com/fishtown-analytics/dbt/blob/v0.8.0/dbt/wrapper.py#L165-L167))

---

#### adapter.get_columns_in_table

__Args__:

* `schema_name`: The schema to test
* `table_name`: The relation from which to select columns

Returns a list of [Columns](https://github.com/fishtown-analytics/dbt/blob/v0.8.0/dbt/schema.py#L37) in a table. Useful for writing `INSERT ... SELECT` queries.

__Example__:

```sql
{% set dest_columns = adapter.get_columns_in_table(schema, identifier) %}
{% set dest_cols_csv = dest_columns | map(attribute='quoted') | join(', ') %}
insert into {{ this }} ({{ dest_cols_csv }}) (
select {{ dest_cols_csv }}
from {{ref('another_table')}}
);
```

([Source on Github](https://github.com/fishtown-analytics/dbt/blob/v0.8.0/dbt/wrapper.py#L169-L171))

---

#### adapter.get_missing_columns

__Args__:

* `from_schema`: The schema for the `from_table`
* `from_table`: The `from_table` to check for differences
* `to_schema`: The schema for the `to_table`
* `to_table`: The `to_table` to check for differences

Returns the set of [Columns](https://github.com/fishtown-analytics/dbt/blob/v0.8.0/dbt/schema.py#L37) that is the difference of the columns in the `from_table`
and the columns in the `to_table`, i.e. (`set(from_table.columns) - set(to_table.columns)`).
Useful for detecting new columns in a source table.

__Example__:

```sql
{% for col in adapter.get_missing_columns(this.schema, 'source_table', this.schema, this.name) %}
alter table {{this}} add column "{{col.name}}" {{col.data_type}};
{% endfor %}
```

([Source on Github](https://github.com/fishtown-analytics/dbt/blob/v0.8.0/dbt/wrapper.py#L173-L177))

---

## Arbitrary configuration variables

Variables can be passed from your `dbt_project.yml` file into models during compilation.
Expand Down
2 changes: 1 addition & 1 deletion docs/guide/testing.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ This validates that all records in a child table have a corresponding record in
people:
constraints:
relationships:
- {from: account_id, to: accounts, field: id}
- {from: account_id, to: ref('accounts'), field: id}
```

### Accepted values
Expand Down

0 comments on commit fe4647f

Please sign in to comment.