Skip to content

Commit

Permalink
[#3627] Improve findability of macro_patches, schedule right macro file
Browse files Browse the repository at this point in the history
for processing
  • Loading branch information
gshank committed Jul 28, 2021
1 parent c448702 commit a83f00c
Show file tree
Hide file tree
Showing 6 changed files with 27 additions and 13 deletions.
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,9 @@
- Fix table and view materialization issue when switching from one to the other ([#2161](https://github.com/dbt-labs/dbt/issues/2161)), [#3547](https://github.com/dbt-labs/dbt/pull/3547))
- Partial parsing: don't reprocess SQL file already scheduled ([#3589](https://github.com/dbt-labs/dbt/issues/3589), [#3620](https://github.com/dbt-labs/dbt/pull/3620))
- Handle interator functions in model config ([#3573](https://github.com/dbt-labs/dbt/issues/3573))
- Partial parsing: fix error after changing empty yaml file ([#3567](https://gith7ub.com/dbt-labs/dbt/issues/3567), [#3618](https://github.com/dbt-labs/dbt/pull/3618))
- Partial parsing: fix error after changing empty yaml file ([#3567](https://github.com/dbt-labs/dbt/issues/3567), [#3618](https://github.com/dbt-labs/dbt/pull/3618))
- Partial parsing: handle source tests when changing test macro ([#3584](https://github.com/dbt-labs/dbt/issues/3584), [#3620](https://github.com/dbt-labs/dbt/pull/3620))
- Partial parsing: schedule new macro file for parsing when macro patching ([#3627](https://github.com/dbt-labs/dbt/issues/3627), [#3627](https://github.com/dbt-labs/dbt/pull/3627))

### Under the hood
- Improve default view and table materialization performance by checking relational cache before attempting to drop temp relations ([#3112](https://github.com/fishtown-analytics/dbt/issues/3112), [#3468](https://github.com/fishtown-analytics/dbt/pull/3468))
Expand Down
2 changes: 1 addition & 1 deletion core/dbt/contracts/files.py
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,7 @@ class SchemaSourceFile(BaseSourceFile):
# node patches contain models, seeds, snapshots, analyses
ndp: List[str] = field(default_factory=list)
# any macro patches in this file by macro unique_id.
mcp: List[str] = field(default_factory=list)
mcp: Dict[str, str] = field(default_factory=dict)
# any source patches in this file. The entries are package, name pairs
# Patches are only against external sources. Sources can be
# created too, but those are in 'sources'
Expand Down
2 changes: 1 addition & 1 deletion core/dbt/contracts/graph/manifest.py
Original file line number Diff line number Diff line change
Expand Up @@ -759,7 +759,7 @@ def add_macro_patch(
if macro.patch_path:
package_name, existing_file_path = macro.patch_path.split('://')
raise_duplicate_macro_patch_name(patch, existing_file_path)
source_file.macro_patches.append(unique_id)
source_file.macro_patches[patch.name] = unique_id
macro.patch(patch)

def add_source_patch(
Expand Down
16 changes: 7 additions & 9 deletions core/dbt/parser/partial.py
Original file line number Diff line number Diff line change
Expand Up @@ -668,19 +668,17 @@ def delete_schema_source(self, schema_file, source_dict):

def delete_schema_macro_patch(self, schema_file, macro):
# This is just macro patches that need to be reapplied
for unique_id in schema_file.macro_patches:
parts = unique_id.split('.')
macro_name = parts[-1]
if macro_name == macro['name']:
macro_unique_id = unique_id
break
macro_unique_id = None
if macro['name'] in schema_file.macro_patches:
macro_unique_id = schema_file.macro_patches[macro['name']]
del schema_file.macro_patches[macro['name']]
if macro_unique_id and macro_unique_id in self.saved_manifest.macros:
macro = self.saved_manifest.macros.pop(macro_unique_id)
self.deleted_manifest.macros[macro_unique_id] = macro
macro_file_id = macro.file_id
self.add_to_pp_files(self.saved_files[macro_file_id])
if macro_unique_id in schema_file.macro_patches:
schema_file.macro_patches.remove(macro_unique_id)
if macro_file_id in self.new_files:
self.saved_files[macro_file_id] = self.new_files[macro_file_id]
self.add_to_pp_files(self.saved_files[macro_file_id])

# exposures are created only from schema files, so just delete
# the exposure.
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
version: 2
macros:
- name: do_something
description: "This is a test macro"
Original file line number Diff line number Diff line change
Expand Up @@ -163,8 +163,17 @@ def test_postgres_pp_models(self):
with self.assertRaises(CompilationException):
results = self.run_dbt(["--partial-parse", "run"])

# Remove the macro patch
# put back macro file, got back to schema file with no macro
# add separate macro patch schema file
shutil.copyfile('extra-files/models-schema2.yml', 'models-a/schema.yml')
shutil.copyfile('extra-files/my_macro.sql', 'macros/my_macro.sql')
shutil.copyfile('extra-files/macros.yml', 'macros/macros.yml')
results = self.run_dbt(["--partial-parse", "run"])

# delete macro and schema file
print(f"\n\n*** remove macro and macro_patch\n\n")
os.remove(normalize('macros/my_macro.sql'))
os.remove(normalize('macros/macros.yml'))
results = self.run_dbt(["--partial-parse", "run"])
self.assertEqual(len(results), 3)

Expand Down Expand Up @@ -193,6 +202,8 @@ def tearDown(self):
os.remove(normalize('macros/my_macro.sql'))
if os.path.exists(normalize('models-a/eschema.yml')):
os.remove(normalize('models-a/eschema.yml'))
if os.path.exists(normalize('macros/macros.yml')):
os.remove(normalize('macros/macros.yml'))


class TestSources(DBTIntegrationTest):
Expand Down

0 comments on commit a83f00c

Please sign in to comment.