Skip to content

Commit

Permalink
Mcknight/backport 63 (#91)
Browse files Browse the repository at this point in the history
* backport of pr #63

* Empty-Commit

* removing unneeded changelog entry

Co-authored-by: Steven Meltser <[email protected]>
  • Loading branch information
McKnight-42 and SMeltser authored Mar 31, 2022
1 parent 39d29bc commit 3e441e2
Show file tree
Hide file tree
Showing 5 changed files with 64 additions and 6 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
### Under the hood
- install compatible branch of dbt-core in unit/integration tests based on merge target ([#80](https://github.com/dbt-labs/dbt-redshift/pull/80))
- Fix test related to preventing coercion of boolean values (True,False) to numeric values (0,1) in query results ([#58](https://github.com/dbt-labs/dbt-redshift/blob/1.0.latest/CHANGELOG.md))
- Fix table creation statement ordering when including both the BACKUP parameter as well as the dist/sort keys ([#23](https://github.com/dbt-labs/dbt-redshift/issues/60))

## dbt-redshift 1.0.0 (December 3, 2021)

Expand Down
2 changes: 1 addition & 1 deletion dbt/include/redshift/macros/adapters.sql
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,9 @@

create {% if temporary -%}temporary{%- endif %} table
{{ relation.include(database=(not temporary), schema=(not temporary)) }}
{% if backup == false -%}backup no{%- endif %}
{{ 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, dist='distkey'
)
}}

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

select 1 as sortkey
53 changes: 48 additions & 5 deletions tests/integration/backup_table_tests/test_backup_table_option.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,12 @@ 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)
lowercase_statement = ' '.join(ddl_statement).lower()
self.assertEqual('backup no' not in lowercase_statement, backup_is_expected)

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

# model_backup_undefined should not contain a BACKUP NO parameter in the table DDL
self.check_backup_param_template('model_backup_undefined', True)
Expand All @@ -44,7 +45,6 @@ def test__redshift_backup_table_option(self):
# 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):
Expand All @@ -71,11 +71,12 @@ 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)
lowercase_statement = ' '.join(ddl_statement).lower()
self.assertEqual('backup no' not in lowercase_statement, backup_is_expected)

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

# model_backup_undefined should contain a BACKUP NO parameter in the table DDL
self.check_backup_param_template('model_backup_undefined', False)
Expand All @@ -88,3 +89,45 @@ def test__redshift_backup_table_option_project_config_false(self):

# 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 TestBackupTableOptionOrder(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_flag_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()
lowercase_statement = ' '.join(ddl_statement).lower()
self.assertEqual('backup no' not in lowercase_statement, backup_flag_is_expected)
if backup_flag_is_expected:
distkey_index = lowercase_statement.find('distkey')
sortkey_index = lowercase_statement.find('sortkey')
backup_index = lowercase_statement.find('backup no')
self.assertEqual((backup_index < distkey_index) or distkey_index == -1, backup_flag_is_expected)
self.assertEqual((backup_index < sortkey_index) or sortkey_index == -1, backup_flag_is_expected)

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

# model_backup_param_before_distkey should contain a BACKUP NO parameter which precedes a DISTKEY in the table ddl
self.check_backup_param_template('model_backup_param_before_distkey', False)

# model_backup_param_before_sortkey should contain a BACKUP NO parameter which precedes a SORTKEY in the table ddl
self.check_backup_param_template('model_backup_param_before_sortkey', False)

0 comments on commit 3e441e2

Please sign in to comment.