-
Notifications
You must be signed in to change notification settings - Fork 296
Add dbt project's hash to dag docs to support dag versioning in Airflow 3 #1907
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
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,40 @@ | ||
| from __future__ import annotations | ||
|
|
||
| import hashlib | ||
| import os | ||
| from pathlib import Path | ||
|
|
||
| from cosmos.log import get_logger | ||
|
|
||
| logger = get_logger(__name__) | ||
|
|
||
|
|
||
| def _create_folder_version_hash(dir_path: Path) -> str: | ||
| """ | ||
| Given a directory, iterate through its content and create a hash that will change in case the | ||
| contents of the directory change. The value should not change if the values of the directory do not change, even if | ||
| the command is run from different Airflow instances. | ||
|
|
||
| This method output must be concise and it currently changes based on operating system. | ||
| """ | ||
| # This approach is less efficient than using modified time | ||
| # sum([path.stat().st_mtime for path in dir_path.glob("**/*")]) | ||
| # unfortunately, the modified time approach does not work well for dag-only deployments | ||
| # where DAGs are constantly synced to the deployed Airflow | ||
| # for 5k files, this seems to take 0.14 | ||
| hasher = hashlib.md5() | ||
| filepaths = [] | ||
|
|
||
| for root_dir, dirs, files in os.walk(dir_path): | ||
| paths = [os.path.join(root_dir, filepath) for filepath in files] | ||
| filepaths.extend(paths) | ||
|
|
||
| for filepath in sorted(filepaths): | ||
| try: | ||
| with open(str(filepath), "rb") as fp: | ||
| buf = fp.read() | ||
| hasher.update(buf) | ||
| except FileNotFoundError: | ||
| logger.warning(f"The dbt project folder contains a symbolic link to a non-existent file: {filepath}") | ||
|
|
||
| return hasher.hexdigest() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,36 @@ | ||
| import logging | ||
| from pathlib import Path | ||
|
|
||
| from cosmos.versioning import _create_folder_version_hash | ||
|
|
||
|
|
||
| def test__create_folder_version_hash(tmp_path, caplog): | ||
| """ | ||
| Test that Cosmos is still able to create the hash of a dbt project folder even when | ||
| there is a symbolic link referencing a no longer existing file. | ||
|
|
||
| This test addresses the issue: | ||
| https://github.com/astronomer/astronomer-cosmos/issues/1096 | ||
| """ | ||
| caplog.set_level(logging.INFO) | ||
|
|
||
| # Create a source folder with two files | ||
| source_dir = tmp_path / "original_dbt_folder" | ||
| source_dir.mkdir() | ||
| file_1 = Path(source_dir / "file_1.sql") | ||
| file_1.touch() | ||
| file_2 = Path(source_dir / "file_2.sql") | ||
| file_2.touch() | ||
|
|
||
| # Create a target folder with symbolic links to the two files in the source folder | ||
| target_dir = tmp_path / "cosmos_dbt_folder" | ||
| target_dir.mkdir() | ||
| file_1_symlink = Path(target_dir / "file_1.sql") | ||
| file_1_symlink.symlink_to(file_1) | ||
| file_2_symlink = Path(target_dir / "file_2.sql") | ||
| file_2_symlink.symlink_to(file_2) | ||
|
|
||
| # Delete one of the original files from the source folder | ||
| file_1.unlink() | ||
|
|
||
| _create_folder_version_hash(target_dir) |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.