Skip to content

Commit

Permalink
Add Redshift parameter to create tables with backup option specified (
Browse files Browse the repository at this point in the history
…#42)

* Update impl and adapters to support backup parameter

* Add test files

* Add test files

* Add PR link to Changelog

* Add EOF newlines

* Debug and split test into two separate cases

* Add contributor info

Co-authored-by: Jeremy Cohen <[email protected]>
  • Loading branch information
dlb8685 and jtcohen6 authored Nov 24, 2021
1 parent c1f02a1 commit c7d7495
Show file tree
Hide file tree
Showing 8 changed files with 127 additions and 0 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
## dbt-redshift 1.0.0 (Release TBD)

### Under the hood
- Add optional Redshift parameter to create tables with BACKUP NO set, to exclude them from snapshots. ([#18](https://github.com/dbt-labs/dbt-redshift/issues/18), [#42](https://github.com/dbt-labs/dbt-redshift/pull/42))

### Contributors
- [@dlb8685](https://github.com/dlb8685) ([#42](https://github.com/dbt-labs/dbt-redshift/pull/42))

## dbt-redshift 1.0.0rc1 (November 10, 2021)

### Under the hood
Expand Down
1 change: 1 addition & 0 deletions dbt/adapters/redshift/impl.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ class RedshiftConfig(AdapterConfig):
dist: Optional[str] = None
sort: Optional[str] = None
bind: Optional[bool] = None
backup: Optional[bool] = True


class RedshiftAdapter(PostgresAdapter, SQLAdapter):
Expand Down
2 changes: 2 additions & 0 deletions dbt/include/redshift/macros/adapters.sql
Original file line number Diff line number Diff line change
Expand Up @@ -39,13 +39,15 @@
'sort',
validator=validation.any[list, basestring]) -%}
{%- set sql_header = config.get('sql_header', none) -%}
{%- set backup = config.get('backup') -%}

{{ sql_header if sql_header is not none }}

create {% if temporary -%}temporary{%- endif %} table
{{ relation.include(database=(not temporary), schema=(not temporary)) }}
{{ dist(_dist) }}
{{ sort(_sort_type, _sort) }}
{% if backup == false -%}backup no{%- endif %}
as (
{{ sql }}
);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{{
config(
materialized='table', backup=False
)
}}

select 1
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{{
config(
materialized='table', backup=True
)
}}

select 2
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{{
config(
materialized='view', backup=True
)
}}

select 3
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{{
config(
materialized='table'
)
}}

select 4
90 changes: 90 additions & 0 deletions tests/integration/backup_table_tests/test_backup_table_option.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
import os

from tests.integration.base import DBTIntegrationTest, use_profile


class TestBackupTableOption(DBTIntegrationTest):
@property
def schema(self):
return 'backup_table_tests'

@staticmethod
def dir(path):
return os.path.normpath(path)

@property
def models(self):
return self.dir("models")

@property
def project_config(self):
return {
'config-version': 2
}

def check_backup_param_template(self, test_table_name, backup_is_expected):
# Use raw DDL statement to confirm backup is set correctly on new table
with open('target/run/test/models/{}.sql'.format(test_table_name), 'r') as ddl_file:
ddl_statement = ddl_file.readlines()
self.assertEqual('backup no' not in ' '.join(ddl_statement).lower(), backup_is_expected)

@use_profile('redshift')
def test__redshift_backup_table_option(self):
self.assertEqual(len(self.run_dbt()), 4)

# model_backup_undefined should not contain a BACKUP NO parameter in the table DDL
self.check_backup_param_template('model_backup_undefined', True)

# model_backup_true should not contain a BACKUP NO parameter in the table DDL
self.check_backup_param_template('model_backup_true', True)

# model_backup_false should contain a BACKUP NO parameter in the table DDL
self.check_backup_param_template('model_backup_false', False)

# Any view should not contain a BACKUP NO parameter, regardless of the specified config (create will fail)
self.check_backup_param_template('model_backup_true_view', True)


class TestBackupTableOptionProjectFalse(DBTIntegrationTest):
@property
def schema(self):
return 'backup_table_tests'

@staticmethod
def dir(path):
return os.path.normpath(path)

@property
def models(self):
return self.dir("models")

@property
def project_config(self):
# Update project config to set backup to False.
# This should make the 'model_backup_undefined' switch to BACKUP NO
return {
'config-version': 2,
'models': {'backup': False}
}

def check_backup_param_template(self, test_table_name, backup_is_expected):
# Use raw DDL statement to confirm backup is set correctly on new table
with open('target/run/test/models/{}.sql'.format(test_table_name), 'r') as ddl_file:
ddl_statement = ddl_file.readlines()
self.assertEqual('backup no' not in ' '.join(ddl_statement).lower(), backup_is_expected)

@use_profile('redshift')
def test__redshift_backup_table_option_project_config_false(self):
self.assertEqual(len(self.run_dbt()), 4)

# model_backup_undefined should contain a BACKUP NO parameter in the table DDL
self.check_backup_param_template('model_backup_undefined', False)

# model_backup_true should not contain a BACKUP NO parameter in the table DDL
self.check_backup_param_template('model_backup_true', True)

# model_backup_false should contain a BACKUP NO parameter in the table DDL
self.check_backup_param_template('model_backup_false', False)

# Any view should not contain a BACKUP NO parameter, regardless of the specified config (create will fail)
self.check_backup_param_template('model_backup_true_view', True)

0 comments on commit c7d7495

Please sign in to comment.