Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Upgrade target_redshift #3

Draft
wants to merge 23 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 27 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,11 @@ dist/
.coverage.*
coverage.log
coverage_html
.cache/
htmlcov/
pytest-out.xml
.pytest_cache/
build/

# Singer JSON files
properties.json
Expand All @@ -32,3 +37,25 @@ tmp
# Docs
docs/_build/
docs/_templates/

# Textio build
venv*/
*.tar.gz
.venv
.dev
.assets
.build
.test
.lint
.code

# Custom environment vars
.env
.env.*

# Testing

# Anciliary files
.vscode/
*.json.gz
.ruff_cache/
96 changes: 96 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
.PHONY: help clean clean-all clean-assets dev lint

codedir := . # location of code
testdir := ./*/tests
scriptdir := .

syspython := python3

pip := venv/bin/pip
aws := aws # awscli v2 should be installed via homebrew or pipx

code-files := $(shell find $(codedir) -name '*.py' -not \( -path '*__pycache__*' \))
test-files := $(shell find $(testdir) -name '*.py' -not \( -path '*__pycache__*' \))
python-script-files := $(shell find $(scriptdir) -name '*.py' -not \( -path '*__pycache__*' \))
gitish := $(shell git rev-parse --short HEAD)

clean: ## Clean build artifacts but NOT downloaded assets
# Python build
find $ . -name '__pycache__' -exec rm -Rf {} +
find $ . -name '*.py[co]' -delete
rm -rf dist
rm -rf *.egg-info
rm -rf *.egg
rm -rf *.eggs
rm -rf *.whl
rm -rf *.tar.gz

rm -rf venv
rm -f .venv
rm -f .dev
rm -f .assets
rm -f .lint

# Test
rm -rf .cache/
rm -f .coverage
rm -rf htmlcov/
rm -f pytest-out.xml

clean-all: clean clean-assets ## Clean everything

venv:
$(syspython) -m venv venv

.venv: venv
venv/bin/pip install --progress-bar off --upgrade pip wheel setuptools pip-tools
touch .venv

%.txt: %.in
venv/bin/pip-compile \
--no-emit-index-url \
--no-emit-options \
--resolver=backtracking \
$^ \
-o "$@"

## update all python requirements*.txt files based on the corresponding requirements*.in file
upgrade-dev-deps: .venv
rm -f requirements_dev.txt
$(MAKE) -sB requirements_dev.txt

.dev: .venv requirements_dev.txt
$(pip) install --progress-bar off --upgrade -r requirements_dev.txt
touch .dev

.build: .dev
$(pip) install --upgrade pip
$(pip) install .

.assets: .dev
touch .assets

clean-assets: ## Clean only assets so they will be re-downloaded
rm -f .assets

.lint: .dev $(code-files) $(test-files)
ifeq ($(ci), true)
venv/bin/black --line-length=101 --safe -v --check $(code-files) $(test-files) $(python-script-files)
else
venv/bin/ruff -v check --fix $(code-files) $(test-files) $(python-script-files)
endif
venv/bin/flake8 --max-line-length=101 $(code-files) $(test-files) $(python-script-files)
touch .lint


dev: .dev ## Setup the local dev environment

build: .build ## Build package

lint: .lint ## Run flake8 and black linting

unit-tests: dev
coverage run -m pytest -vv --disable-pytest-warnings tests/unit && coverage report

tests: dev
textioaws assumerole --config predev/predev --duration 14400 coverage run -m pytest -vv --disable-pytest-warnings tests/integration && coverage report
41 changes: 22 additions & 19 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,11 @@ or

### To run

Like any other target that's following the singer specificiation:
Like any other target that's following the singer specification:

`some-singer-tap | target-redshift --config [config.json]`

It's reading incoming messages from STDIN and using the properites in `config.json` to upload data into Amazon Redshift.
It's reading incoming messages from STDIN and using the properties in `config.json` to upload data into Amazon Redshift.

**Note**: To avoid version conflicts run `tap` and `targets` in separate virtual environments.

Expand Down Expand Up @@ -77,8 +77,8 @@ Full list of options in `config.json`:
| password | String | Yes | Redshift Password |
| dbname | String | Yes | Redshift Database name |
| aws_profile | String | No | AWS profile name for profile based authentication. If not provided, `AWS_PROFILE` environment variable will be used. |
| aws_access_key_id | String | No | S3 Access Key Id. Used for S3 and Redshfit copy operations. If not provided, `AWS_ACCESS_KEY_ID` environment variable will be used. |
| aws_secret_access_key | String | No | S3 Secret Access Key. Used for S3 and Redshfit copy operations. If not provided, `AWS_SECRET_ACCESS_KEY` environment variable will be used. |
| aws_access_key_id | String | No | S3 Access Key Id. Used for S3 and Redshift copy operations. If not provided, `AWS_ACCESS_KEY_ID` environment variable will be used. |
| aws_secret_access_key | String | No | S3 Secret Access Key. Used for S3 and Redshift copy operations. If not provided, `AWS_SECRET_ACCESS_KEY` environment variable will be used. |
| aws_session_token | String | No | S3 AWS STS token for temporary credentials. If not provided, `AWS_SESSION_TOKEN` environment variable will be used. |
| aws_redshift_copy_role_arn | String | No | AWS Role ARN to be used for the Redshift COPY operation. Used instead of the given AWS keys for the COPY operation if provided - the keys are still used for other S3 operations |
| s3_acl | String | No | S3 Object ACL |
Expand All @@ -93,7 +93,7 @@ Full list of options in `config.json`:
| default_target_schema_select_permissions | String | | Grant USAGE privilege on newly created schemas and grant SELECT privilege on newly created tables to a specific list of users or groups. Example: `{"users": ["user_1","user_2"], "groups": ["group_1", "group_2"]}` If `schema_mapping` is not defined then every stream sent by the tap is granted accordingly. |
| schema_mapping | Object | | Useful if you want to load multiple streams from one tap to multiple Redshift schemas.<br><br>If the tap sends the `stream_id` in `<schema_name>-<table_name>` format then this option overwrites the `default_target_schema` value. Note, that using `schema_mapping` you can overwrite the `default_target_schema_select_permissions` value to grant SELECT permissions to different groups per schemas or optionally you can create indices automatically for the replicated tables.<br><br> **Note**: This is an experimental feature and recommended to use via PipelineWise YAML files that will generate the object mapping in the right JSON format. For further info check a [PipelineWise YAML Example]
| disable_table_cache | Boolean | | (Default: False) By default the connector caches the available table structures in Redshift at startup. In this way it doesn't need to run additional queries when ingesting data to check if altering the target tables is required. With `disable_table_cache` option you can turn off this caching. You will always see the most recent table structures but will cause an extra query runtime. |
| add_metadata_columns | Boolean | | (Default: False) Metadata columns add extra row level information about data ingestions, (i.e. when was the row read in source, when was inserted or deleted in redshift etc.) Metadata columns are creating automatically by adding extra columns to the tables with a column prefix `_SDC_`. The metadata columns are documented at https://transferwise.github.io/pipelinewise/data_structure/sdc-columns.html. Enabling metadata columns will flag the deleted rows by setting the `_SDC_DELETED_AT` metadata column. Without the `add_metadata_columns` option the deleted rows from singer taps will not be recongisable in Redshift. |
| add_metadata_columns | Boolean | | (Default: False) Metadata columns add extra row level information about data ingestion, (i.e. when was the row read in source, when was inserted or deleted in redshift etc.) Metadata columns are creating automatically by adding extra columns to the tables with a column prefix `_SDC_`. The metadata columns are documented at [https://transferwise.github.io/pipelinewise/data_structure/sdc-columns.html](https://transferwise.github.io/pipelinewise/data_structure/sdc-columns.html). Enabling metadata columns will flag the deleted rows by setting the `_SDC_DELETED_AT` metadata column. Without the `add_metadata_columns` option the deleted rows from singer taps will not be recognisable in Redshift. |
| hard_delete | Boolean | | (Default: False) When `hard_delete` option is true then DELETE SQL commands will be performed in Redshift to delete rows in tables. It's achieved by continuously checking the `_SDC_DELETED_AT` metadata column sent by the singer tap. Due to deleting rows requires metadata columns, `hard_delete` option automatically enables the `add_metadata_columns` option as well. |
| data_flattening_max_level | Integer | | (Default: 0) Object type RECORD items from taps can be loaded into VARIANT columns as JSON (default) or we can flatten the schema by creating columns automatically.<br><br>When value is 0 (default) then flattening functionality is turned off. |
| primary_key_required | Boolean | | (Default: True) Log based and Incremental replications on tables with no Primary Key cause duplicates when merging UPDATE events. When set to true, stop loading data if no Primary Key is defined. |
Expand All @@ -103,11 +103,10 @@ Full list of options in `config.json`:
| slices | Integer | No | The number of slices to split files into prior to running COPY on Redshift. This should be set to the number of Redshift slices. The number of slices per node depends on the node size of the cluster - run `SELECT COUNT(DISTINCT slice) slices FROM stv_slices` to calculate this. Defaults to `1`. |
| temp_dir | String | | (Default: platform-dependent) Directory of temporary CSV files with RECORD messages. |

### To run tests:
### To run tests

1. Install python dependencies in a virtual env:


```bash
python3 -m venv venv
. venv/bin/activate
Expand All @@ -117,34 +116,38 @@ Full list of options in `config.json`:

1. To run unit tests:


```bash
coverage run -m pytest -vv --disable-pytest-warnings tests/unit && coverage report
```

1. To run integration tests define environment variables first:

```bash

# get the host, port, user, password by running the following it `analytics-data-warehouse`:
# > make -C infrastructure psql config=predev/predev stack=redshift
export TARGET_REDSHIFT_HOST=<redshift-host>
export TARGET_REDSHIFT_PORT=<redshift-port>
export TARGET_REDSHIFT_USER=<redshift-user>
export TARGET_REDSHIFT_PASSWORD=<redshift-password>
export TARGET_REDSHIFT_DBNAME=<redshift-database-name>
export TARGET_REDSHIFT_SCHEMA=<redshift-target-schema>
export TARGET_REDSHIFT_AWS_ACCESS_KEY=<aws-access-key-id>
export TARGET_REDSHIFT_AWS_SECRET_ACCESS_KEY=<aws-access-secret-access-key>
export TARGET_REDSHIFT_S3_ACL=<s3-target-acl>
export TARGET_REDSHIFT_S3_BUCKET=<s3-bucket>
export TARGET_REDSHIFT_S3_KEY_PREFIX=<s3-bucket-directory>

coverage run -m pytest -vv --disable-pytest-warnings tests/integration && coverage report
export TARGET_REDSHIFT_DBNAME='main'
export TARGET_REDSHIFT_SCHEMA='test'
export TARGET_REDSHIFT_S3_BUCKET='data-warehouse-meltano-temp-bucket-predev-predev'

# The following are not required if using password authentication
# export TARGET_REDSHIFT_AWS_ACCESS_KEY=<aws-access-key-id>
# export TARGET_REDSHIFT_AWS_SECRET_ACCESS_KEY=<aws-access-secret-access-key>
# export TARGET_REDSHIFT_S3_ACL=<s3-target-acl>
# export TARGET_REDSHIFT_S3_KEY_PREFIX=<s3-bucket-directory>

textioaws assumerole --config predev/predev --duration 14400 coverage run -m pytest -vv --disable-pytest-warnings tests/integration && coverage report

```

### To run pylint:
### To run pylint

1. Install python dependencies and run python linter


```bash
python3 -m venv venv
. venv/bin/activate
Expand Down
8 changes: 8 additions & 0 deletions requirements_dev.in
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
black
coverage
mock
pre-commit
pylint
pytest
pytest-cov
ruff
90 changes: 90 additions & 0 deletions requirements_dev.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
#
# This file is autogenerated by pip-compile with Python 3.9
# by the following command:
#
# pip-compile --no-emit-index-url --no-emit-options --output-file=requirements_dev.txt requirements_dev.in
#
astroid==2.15.6
# via pylint
black==23.7.0
# via -r requirements_dev.in
cfgv==3.4.0
# via pre-commit
click==8.1.7
# via black
coverage[toml]==7.3.1
# via
# -r requirements_dev.in
# pytest-cov
dill==0.3.7
# via pylint
distlib==0.3.7
# via virtualenv
exceptiongroup==1.1.3
# via pytest
filelock==3.12.3
# via virtualenv
identify==2.5.27
# via pre-commit
iniconfig==2.0.0
# via pytest
isort==5.12.0
# via pylint
lazy-object-proxy==1.9.0
# via astroid
mccabe==0.7.0
# via pylint
mock==5.1.0
# via -r requirements_dev.in
mypy-extensions==1.0.0
# via black
nodeenv==1.8.0
# via pre-commit
packaging==23.1
# via
# black
# pytest
pathspec==0.11.2
# via black
platformdirs==3.10.0
# via
# black
# pylint
# virtualenv
pluggy==1.3.0
# via pytest
pre-commit==3.4.0
# via -r requirements_dev.in
pylint==2.17.5
# via -r requirements_dev.in
pytest==7.4.2
# via
# -r requirements_dev.in
# pytest-cov
pytest-cov==4.1.0
# via -r requirements_dev.in
pyyaml==6.0.1
# via pre-commit
ruff==0.0.287
# via -r requirements_dev.in
tomli==2.0.1
# via
# black
# coverage
# pylint
# pytest
tomlkit==0.12.1
# via pylint
typing-extensions==4.7.1
# via
# astroid
# black
# filelock
# pylint
virtualenv==20.24.5
# via pre-commit
wrapt==1.15.0
# via astroid

# The following packages are considered to be unsafe in a requirements file:
# setuptools
Loading