Skip to content

Commit

Permalink
Merge branch 'main' into feature/custom-warehouse-macro
Browse files Browse the repository at this point in the history
  • Loading branch information
javiCalvo committed Mar 21, 2022
2 parents 86e1cc7 + 032d2bb commit d934788
Show file tree
Hide file tree
Showing 4 changed files with 141 additions and 4 deletions.
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,13 @@
### Fixes
- Add unique\_id field to docs generation test catalogs; a follow-on PR to core PR ([#4168](https://github.com/dbt-labs/dbt-core/pull/4618))

### Under the hood
- Add `query_id` for a query to `run_result.json` ([#40](https://github.com/dbt-labs/dbt-snowflake/pull/40))

### Contributors
- [@joshuataylor](https://github.com/joshuataylor) ([#40](https://github.com/dbt-labs/dbt-snowflake/pull/40))
- [@devoted](https://github.com/devoted) ([#40](https://github.com/dbt-labs/dbt-snowflake/pull/40))

## dbt-snowflake 1.0.0 (December 3rd, 2021)

## dbt-snowflake 1.0.0rc2 (November 24, 2021)
Expand Down
118 changes: 118 additions & 0 deletions CONTRIBUTING.MD
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
# Contributing to `dbt-snowflake`

1. [About this document](#about-this-document)
3. [Getting the code](#getting-the-code)
5. [Running `dbt-snowflake` in development](#running-dbt-snowflake-in-development)
6. [Testing](#testing)
7. [Updating Docs](#updating-docs)
7. [Submitting a Pull Request](#submitting-a-pull-request)

## About this document
This document is a guide for anyone interested in contributing to the `dbt-snowflake` repository. It outlines how to create issues and submit pull requests (PRs).

This is not intended as a guide for using `dbt-snowflake` in a project. For configuring and using this adapter, see [Snowflake Profile](https://docs.getdbt.com/reference/warehouse-profiles/snowflake-profile), and [Snowflake Configs](https://docs.getdbt.com/reference/resource-configs/snowflake-configs).

We assume users have a Linux or MacOS system. You should have familiarity with:

- Python `virturalenv`s
- Python modules
- `pip`
- common command line utilities like `git`.

In addition to this guide, we highly encourage you to read the [dbt-core](https://github.com/dbt-labs/dbt-core/blob/main/CONTRIBUTING.md). Almost all information there is applicable here!

### Signing the CLA

Please note that all contributors to `dbt-snowflake` must sign the [Contributor License Agreement](https://docs.getdbt.com/docs/contributor-license-agreements)(CLA) before their pull request(s) can be merged into the `dbt-snowflake` codebase. Given this, `dbt-snowflake` maintainers will unfortunately be unable to merge your contribution(s) until you've signed the CLA. You are, however, welcome to open issues and comment on existing ones.

## Getting the code

`git` is needed in order to download and modify the `dbt-snowflake` code. There are several ways to install Git. For MacOS, we suggest installing [Xcode](https://developer.apple.com/support/xcode/) or [Xcode Command Line Tools](https://mac.install.guide/commandlinetools/index.html).

### External contributors

If you are not a member of the `dbt-labs` GitHub organization, you can contribute to `dbt-snowflake` by forking the `dbt-snowflake` repository. For more on forking, check out the [GitHub docs on forking](https://help.github.com/en/articles/fork-a-repo). In short, you will need to:

1. fork the `dbt-snowflake` repository
2. clone your fork locally
3. check out a new branch for your proposed changes
4. push changes to your fork
5. open a pull request of your forked repository against `dbt-labs/dbt-snowflake`

### dbt Labs contributors

If you are a member of the `dbt Labs` GitHub organization, you will have push access to the `dbt-snowflake` repo. Rather than forking `dbt-snowflake` to make your changes, clone the repository like normal, and check out feature branches.

## Running `dbt-snowflake` in development

### Installation

1. Ensure you have the latest version of `pip` installed by running `pip install --upgrade pip` in terminal.

2. Configure and activate a `virtualenv` as described in [Setting up an environment](https://github.com/dbt-labs/dbt-core/blob/HEAD/CONTRIBUTING.md#setting-up-an-environment).

3. Install `dbt-core` in the active `virtualenv`. To confirm you installed dbt correctly, run `dbt --version` and `which dbt`.

4. Install `dbt-snowflake` and development dependencies in the active `virtualenv`. Run `pip install -e . -r dev-requirements.txt`.

When `dbt-snowflake` is installed this way, any changes you make to the `dbt-snowflake` source code will be reflected immediately (i.e. in your next local dbt invocation against a Snowflake target).

## Testing

### Initial setup

`dbt-snowflake` contains [unit](https://github.com/dbt-labs/dbt-snowflake/tree/main/tests/unit) and [integration](https://github.com/dbt-labs/dbt-snowflake/tree/main/tests/integration) tests. Integration tests require an actual Snowflake warehouse to test against. There are two primary ways to do this:

- This repo has CI/CD GitHub Actions set up. Both unit and integration tests will run against an already configured Snowflake warehouse during PR checks.

- You can also run integration tests "locally" by configuring a `test.env` file with appropriate `ENV` variables.

```
cp test.env.example test.env
$EDITOR test.env
```

WARNING: The parameters in your `test.env` file must link to a valid Snowflake account. The `test.env` file you create is git-ignored, but please be _extra_ careful to never check in credentials or other sensitive information when developing.


### "Local" test commands
There are a few methods for running tests locally.

#### `tox`
`tox` automatically runs unit tests against several Python versions using its own virtualenvs. Run `tox -p` to run unit tests for Python 3.7, Python 3.8, Python 3.9, and `flake8` in parallel. Run `tox -e py37` to invoke tests on Python version 3.7 only (use py37, py38, or py39). Tox recipes are found in `tox.ini`.

#### `pytest`
You may run a specific test or group of tests using `pytest` directly. Activate a Python virtualenv active with dev dependencies installed. Then, run tests like so:

```sh
# Note: replace $strings with valid names

# run all snowflake integration tests in a directory
python -m pytest -m profile_snowflake tests/integration/$test_directory
# run all snowflake integration tests in a module
python -m pytest -m profile_snowflake tests/integration/$test_dir_and_filename.py
# run all snowflake integration tests in a class
python -m pytest -m profile_snowflake tests/integration/$test_dir_and_filename.py::$test_class_name
# run a specific snowflake integration test
python -m pytest -m profile_snowflake tests/integration/$test_dir_and_filename.py::$test_class_name::$test__method_name

# run all unit tests in a module
python -m pytest tests/unit/$test_file_name.py
# run a specific unit test
python -m pytest tests/unit/$test_file_name.py::$test_class_name::$test_method_name
```

## Updating documentation

Many changes will require an update to `dbt-snowflake` documentation. Here are some relevant links.

- Docs are [here](https://docs.getdbt.com/).
- The docs repo for making changes is located [here](https://github.com/dbt-labs/docs.getdbt.com).
- The changes made are likely to impact one or both of [Snowflake Profile](https://docs.getdbt.com/reference/warehouse-profiles/snowflake-profile), or [Snowflake Configs](https://docs.getdbt.com/reference/resource-configs/snowflake-configs).
- We ask every community member who makes a user-facing change to open an issue or PR regarding doc changes.

## Submitting a Pull Request

A `dbt-snowflake` maintainer will review your PR and will determine if it has passed regression tests. They may suggest code revisions for style and clarity, or they may request that you add unit or integration tests. These are good things! We believe that, with a little bit of help, anyone can contribute high-quality code.

Once all tests are passing and your PR has been approved, a `dbt-snowflake` maintainer will merge your changes into the active development branch. And that's it! Happy developing :tada:
12 changes: 9 additions & 3 deletions dbt/adapters/snowflake/connections.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,11 @@
_TOKEN_REQUEST_URL = 'https://{}.snowflakecomputing.com/oauth/token-request'


@dataclass
class SnowflakeAdapterResponse(AdapterResponse):
query_id: str = ""


@dataclass
class SnowflakeCredentials(Credentials):
account: str
Expand Down Expand Up @@ -346,16 +351,17 @@ def cancel(self, connection):
logger.debug("Cancel query '{}': {}".format(connection_name, res))

@classmethod
def get_response(cls, cursor) -> AdapterResponse:
def get_response(cls, cursor) -> SnowflakeAdapterResponse:
code = cursor.sqlstate

if code is None:
code = 'SUCCESS'

return AdapterResponse(
return SnowflakeAdapterResponse(
_message="{} {}".format(code, cursor.rowcount),
rows_affected=cursor.rowcount,
code=code
code=code,
query_id=cursor.sfqid
)

# disable transactional logic by default on Snowflake
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,12 @@ def project_config(self):

@use_profile('snowflake')
def test_snowflake_adapter_methods(self):
self.run_dbt(['compile']) # trigger any compile-time issues
self.run_dbt(['compile'])
self.run_dbt()
self.assertTablesEqual('MODEL', 'EXPECTED')

@use_profile('snowflake')
def test_snowflake_adapter_query_id(self):
results = self.run_dbt()
# strip to ensure nonempty string doesn't get passed
assert results[0].adapter_response['query_id'].strip() != ""

0 comments on commit d934788

Please sign in to comment.