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

override seed types #708

Merged
merged 8 commits into from
Apr 23, 2018
Merged
Show file tree
Hide file tree
Changes from 6 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
13 changes: 8 additions & 5 deletions dbt/adapters/bigquery.py
Original file line number Diff line number Diff line change
Expand Up @@ -484,7 +484,8 @@ def convert_datetime_type(cls, agate_table, col_idx):
return "datetime"

@classmethod
def create_csv_table(cls, profile, schema, table_name, agate_table):
def create_csv_table(cls, profile, schema, table_name, agate_table,
column_override):
pass

@classmethod
Expand All @@ -493,17 +494,19 @@ def reset_csv_table(cls, profile, schema, table_name, agate_table,
cls.drop(profile, schema, table_name, "table")

@classmethod
def _agate_to_schema(cls, agate_table):
def _agate_to_schema(cls, agate_table, column_override):
bq_schema = []
for idx, col_name in enumerate(agate_table.column_names):
type_ = cls.convert_agate_type(agate_table, idx)
inferred_type = cls.convert_agate_type(agate_table, idx)
type_ = column_override.get(col_name, inferred_type)
bq_schema.append(
google.cloud.bigquery.SchemaField(col_name, type_))
return bq_schema

@classmethod
def load_csv_rows(cls, profile, schema, table_name, agate_table):
bq_schema = cls._agate_to_schema(agate_table)
def load_csv_rows(cls, profile, schema, table_name, agate_table,
column_override):
bq_schema = cls._agate_to_schema(agate_table, column_override)
dataset = cls.get_dataset(profile, schema, None)
table = dataset.table(table_name, schema=bq_schema)
conn = cls.get_connection(profile, None)
Expand Down
16 changes: 10 additions & 6 deletions dbt/adapters/default.py
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,8 @@ def cancel_connection(cls, project, connection):
'`cancel_connection` is not implemented for this adapter!')

@classmethod
def create_csv_table(cls, profile, schema, table_name, agate_table):
def create_csv_table(cls, profile, schema, table_name, agate_table,
column_override):
raise dbt.exceptions.NotImplementedException(
'`create_csv_table` is not implemented for this adapter!')

Expand All @@ -110,7 +111,8 @@ def reset_csv_table(cls, profile, schema, table_name, agate_table,
'`reset_csv_table` is not implemented for this adapter!')

@classmethod
def load_csv_rows(cls, profile, schema, table_name, agate_table):
def load_csv_rows(cls, profile, schema, table_name, agate_table,
column_override):
raise dbt.exceptions.NotImplementedException(
'`load_csv_rows` is not implemented for this adapter!')

Expand Down Expand Up @@ -651,18 +653,20 @@ def quote_schema_and_table(cls, profile, schema, table, model_name=None):

@classmethod
def handle_csv_table(cls, profile, schema, table_name, agate_table,
full_refresh=False):
column_override, full_refresh=False):
existing = cls.query_for_existing(profile, schema)
existing_type = existing.get(table_name)
if existing_type and existing_type != "table":
raise dbt.exceptions.RuntimeException(
"Cannot seed to '{}', it is a view".format(table_name))
if existing_type:
cls.reset_csv_table(profile, schema, table_name, agate_table,
full_refresh=full_refresh)
column_override, full_refresh=full_refresh)
else:
cls.create_csv_table(profile, schema, table_name, agate_table)
cls.load_csv_rows(profile, schema, table_name, agate_table)
cls.create_csv_table(profile, schema, table_name, agate_table,
column_override)
cls.load_csv_rows(profile, schema, table_name, agate_table,
column_override)
cls.commit_if_has_connection(profile, None)

@classmethod
Expand Down
16 changes: 11 additions & 5 deletions dbt/adapters/postgres.py
Original file line number Diff line number Diff line change
Expand Up @@ -194,26 +194,32 @@ def convert_time_type(cls, agate_table, col_idx):
return "time"

@classmethod
def create_csv_table(cls, profile, schema, table_name, agate_table):
def create_csv_table(cls, profile, schema, table_name, agate_table,
column_override):
col_sqls = []
for idx, col_name in enumerate(agate_table.column_names):
type_ = cls.convert_agate_type(agate_table, idx)
inferred_type = cls.convert_agate_type(agate_table, idx)
type_ = column_override.get(col_name, inferred_type)
col_sqls.append('{} {}'.format(col_name, type_))
sql = 'create table "{}"."{}" ({})'.format(schema, table_name,
", ".join(col_sqls))
return cls.add_query(profile, sql)

@classmethod
def reset_csv_table(cls, profile, schema, table_name, agate_table,
full_refresh=False):
column_override, full_refresh=False):
if full_refresh:
cls.drop_table(profile, schema, table_name, None)
cls.create_csv_table(profile, schema, table_name, agate_table)
cls.create_csv_table(profile, schema, table_name, agate_table,
column_override)
else:
cls.truncate(profile, schema, table_name)

@classmethod
def load_csv_rows(cls, profile, schema, table_name, agate_table):
def load_csv_rows(cls, profile, schema, table_name, agate_table,
column_override):
bindings = []
placeholders = []
cols_sql = ", ".join(c for c in agate_table.column_names)

for chunk in chunks(agate_table.rows, 10000):
Expand Down
1 change: 1 addition & 0 deletions dbt/contracts/graph/parsed.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
Required('post-hook'): [hook_contract],
Required('pre-hook'): [hook_contract],
Required('vars'): dict,
Required('column_types'): dict,
}, extra=ALLOW_EXTRA)

parsed_node_contract = unparsed_node_contract.extend({
Expand Down
2 changes: 1 addition & 1 deletion dbt/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ class SourceConfig(object):
ConfigKeys = DBTConfigKeys

AppendListFields = ['pre-hook', 'post-hook']
ExtendDictFields = ['vars']
ExtendDictFields = ['vars', 'column_types']
ClobberFields = [
'schema',
'enabled',
Expand Down
3 changes: 3 additions & 0 deletions dbt/node_runners.py
Original file line number Diff line number Diff line change
Expand Up @@ -509,7 +509,10 @@ def execute(self, compiled_node, existing_, flat_graph):
schema = compiled_node["schema"]
table_name = compiled_node["name"]
table = compiled_node["agate_table"]

column_override = compiled_node['config'].get('column_types', {})
self.adapter.handle_csv_table(self.profile, schema, table_name, table,
column_override,
full_refresh=dbt.flags.FULL_REFRESH)

if dbt.flags.FULL_REFRESH:
Expand Down
1 change: 1 addition & 0 deletions dbt/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
'pre-hook',
'post-hook',
'vars',
'column_types',
'bind',
]

Expand Down
15 changes: 15 additions & 0 deletions test/integration/005_simple_seed_test/macros/schema_test.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@

{% macro test_column_type(model, field, type) %}

{% set cols = adapter.get_columns_in_table(model.schema, model.name) %}

{% set col_types = {} %}
{% for col in cols %}
{% set _ = col_types.update({col.name: col.data_type}) %}
{% endfor %}

{% set val = 0 if col_types[field] == type else 1 %}

select {{ val }} as pass_fail

{% endmacro %}
7 changes: 7 additions & 0 deletions test/integration/005_simple_seed_test/models/schema.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@


seed_enabled:
constraints:
column_type:
- {field: id, type: 'character varying(255)' }
- {field: birthday, type: 'date' }
34 changes: 34 additions & 0 deletions test/integration/005_simple_seed_test/test_simple_seed.py
Original file line number Diff line number Diff line change
Expand Up @@ -121,3 +121,37 @@ def test_simple_seed_with_disabled(self):
self.assertTableDoesExist('seed_enabled')
self.assertTableDoesNotExist('seed_disabled')


class TestSimpleSeedColumnOverride(DBTIntegrationTest):

@property
def schema(self):
return "simple_seed_005"

@property
def models(self):
return "test/integration/005_simple_seed_test/models"

@property
def project_config(self):
return {
"data-paths": ['test/integration/005_simple_seed_test/data-config'],
"macro-paths": ['test/integration/005_simple_seed_test/macros'],
"seeds": {
"test": {
"enabled": False,
"seed_enabled": {
"enabled": True,
"column_types": {
"id": "text",
"birthday": "date",
}
},
}
}
}

@attr(type='postgres')
def test_simple_seed_with_column_override(self):
self.run_dbt(["seed"])
self.run_dbt(["test"])
1 change: 1 addition & 0 deletions test/unit/test_compiler.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ def setUp(self):
'post-hook': [],
'pre-hook': [],
'vars': {},
'column_types': {},
}

def test__prepend_ctes__already_has_cte(self):
Expand Down
2 changes: 2 additions & 0 deletions test/unit/test_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ def setUp(self):
'post-hook': [],
'pre-hook': [],
'vars': {},
'column_types': {},
}

self.disabled_config = {
Expand All @@ -65,6 +66,7 @@ def setUp(self):
'post-hook': [],
'pre-hook': [],
'vars': {},
'column_types': {},
}

def test__single_model(self):
Expand Down