Skip to content

Commit

Permalink
Merge pull request #2464 from fishtown-analytics/fix/duplicate-source…
Browse files Browse the repository at this point in the history
…s-crash

Fix duplicate sources crash
  • Loading branch information
beckjake committed May 18, 2020
2 parents 5734fb8 + df8aa64 commit 9070038
Show file tree
Hide file tree
Showing 5 changed files with 64 additions and 2 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
- Fixed an argument issue with the `create_schema` macro on bigquery ([#2445](https://github.com/fishtown-analytics/dbt/issues/2445), [#2448](https://github.com/fishtown-analytics/dbt/pull/2448))
- dbt now logs using the adapter plugin's ideas about how relations should be displayed ([dbt-spark/#74](https://github.com/fishtown-analytics/dbt-spark/issues/74), [#2450](https://github.com/fishtown-analytics/dbt/pull/2450))
- The create_adapter_plugin.py script creates a version 2 dbt_project.yml file ([#2451](https://github.com/fishtown-analytics/dbt/issues/2451), [#2455](https://github.com/fishtown-analytics/dbt/pull/2455))
- Fixed dbt crashing with an AttributeError on duplicate sources ([#2463](https://github.com/fishtown-analytics/dbt/issues/2463), [#2464](https://github.com/fishtown-analytics/dbt/pull/2464))


## dbt 0.17.0rc1 (May 12, 2020)
Expand Down
14 changes: 13 additions & 1 deletion core/dbt/contracts/graph/parsed.py
Original file line number Diff line number Diff line change
Expand Up @@ -327,9 +327,15 @@ class UnpatchedSourceDefinition(UnparsedBaseNode, HasUniqueID, HasFqn):
resource_type: NodeType = field(metadata={'restrict': [NodeType.Source]})
patch_path: Optional[Path] = None

def get_full_source_name(self):
return f'{self.source.name}_{self.table.name}'

def get_source_representation(self):
return f'source("{self.source.name}", "{self.table.name}")'

@property
def name(self) -> str:
return '{0.name}_{1.name}'.format(self.source, self.table)
return self.get_full_source_name()

@property
def quote_columns(self) -> Optional[bool]:
Expand Down Expand Up @@ -391,6 +397,12 @@ class ParsedSourceDefinition(
config: SourceConfig = field(default_factory=SourceConfig)
patch_path: Optional[Path] = None

def get_full_source_name(self):
return f'{self.source_name}_{self.name}'

def get_source_representation(self):
return f'source("{self.source.name}", "{self.table.name}")'

@property
def is_refable(self):
return False
Expand Down
3 changes: 2 additions & 1 deletion core/dbt/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -724,7 +724,8 @@ def raise_duplicate_resource_name(node_1, node_2):
if node_1.resource_type in NodeType.refable():
get_func = 'ref("{}")'.format(duped_name)
elif node_1.resource_type == NodeType.Source:
get_func = 'source("{}", "{}")'.format(node_1.source_name, duped_name)
duped_name = node_1.get_full_source_name()
get_func = node_1.get_source_representation()
elif node_1.resource_type == NodeType.Documentation:
get_func = 'doc("{}")'.format(duped_name)
elif node_1.resource_type == NodeType.Test and 'schema' in node_1.tags:
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
version: 2
sources:
- name: something
tables:
- name: dupe
- name: dupe
42 changes: 42 additions & 0 deletions test/integration/025_duplicate_model_test/test_duplicate_source.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
from dbt.exceptions import CompilationException
from test.integration.base import DBTIntegrationTest, use_profile


class TestDuplicateSourceEnabled(DBTIntegrationTest):

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

@property
def models(self):
return "models-source-dupes"

@property
def profile_config(self):
return {
"test": {
"outputs": {
"dev": {
"type": "postgres",
"threads": 1,
"host": self.database_host,
"port": 5432,
"user": "root",
"pass": "password",
"dbname": "dbt",
"schema": self.unique_schema()
},
},
"target": "dev"
}
}

@use_profile("postgres")
def test_postgres_duplicate_model_enabled(self):
message = "dbt found two resources with the name"
try:
self.run_dbt(["compile"])
self.assertTrue(False, "dbt did not throw for duplicate sources")
except CompilationException as e:
self.assertTrue(message in str(e), "dbt did not throw the correct error message")

0 comments on commit 9070038

Please sign in to comment.