Skip to content

Commit e951979

Browse files
committed
fix: Update state when running from temporary directory
1 parent e63f89c commit e951979

File tree

4 files changed

+53
-6
lines changed

4 files changed

+53
-6
lines changed

airflow_dbt_python/__version__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,4 @@
22
__author__ = "Tomás Farías Santana"
33
__copyright__ = "Copyright 2021 Tomás Farías Santana"
44
__title__ = "airflow-dbt-python"
5-
__version__ = "0.8.0"
5+
__version__ = "0.8.1"

airflow_dbt_python/operators/dbt.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -219,6 +219,14 @@ def dbt_directory(self) -> Iterator[str]:
219219

220220
with TemporaryDirectory(prefix="airflowtmp") as tmp_dir:
221221
self.prepare_directory(tmp_dir)
222+
223+
if getattr(self, "state", None) is not None:
224+
state = Path(getattr(self, "state", ""))
225+
# Since we are running in a temporary directory, we need to make
226+
# state paths relative to this temporary directory.
227+
if not state.is_absolute():
228+
setattr(self, "state", str(Path(tmp_dir) / state))
229+
222230
yield tmp_dir
223231

224232
self.profiles_dir = store_profiles_dir

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[tool.poetry]
22
name = "airflow-dbt-python"
3-
version = "0.8.0"
3+
version = "0.8.1"
44
description = "A dbt operator for Airflow that uses the dbt Python package"
55
authors = ["Tomás Farías Santana <tomas@tomasfarias.dev>"]
66
license = "MIT"

tests/test_dbt_base.py

Lines changed: 43 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -125,25 +125,59 @@ def test_prepare_args_with_positional():
125125

126126
@no_s3_hook
127127
def test_dbt_base_dbt_directory():
128-
"""Test dbt_directory yields a temporary directory"""
128+
"""Test dbt_directory yields a temporary directory."""
129129
op = DbtBaseOperator(
130130
task_id="dbt_task",
131131
project_dir="/path/to/project/",
132132
profiles_dir="/path/to/profiles/",
133133
)
134+
op.state = "target/"
135+
134136
with op.dbt_directory() as tmp_dir:
135137
assert Path(tmp_dir).exists()
136138
assert op.project_dir == "/path/to/project/"
137139
assert op.profiles_dir == "/path/to/profiles/"
140+
assert op.state == f"{tmp_dir}/target"
141+
142+
143+
@no_s3_hook
144+
def test_dbt_base_dbt_directory_with_absolute_state():
145+
"""Test dbt_directory does not alter state when not needed."""
146+
op = DbtBaseOperator(
147+
task_id="dbt_task",
148+
project_dir="/path/to/project/",
149+
profiles_dir="/path/to/profiles/",
150+
)
151+
op.state = "/absolute/path/to/target/"
152+
153+
with op.dbt_directory() as tmp_dir:
154+
assert Path(tmp_dir).exists()
155+
assert op.state == "/absolute/path/to/target/"
156+
157+
158+
@no_s3_hook
159+
def test_dbt_base_dbt_directory_with_no_state():
160+
"""Test dbt_directory does not alter state when not needed."""
161+
op = DbtBaseOperator(
162+
task_id="dbt_task",
163+
project_dir="/path/to/project/",
164+
profiles_dir="/path/to/profiles/",
165+
)
166+
167+
with op.dbt_directory() as tmp_dir:
168+
assert Path(tmp_dir).exists()
169+
assert getattr(op, "state", None) is None
138170

139171

140172
@no_s3_hook
141173
def test_dbt_base_dbt_directory_changed_to_s3(
142174
dbt_project_file, profiles_file, s3_bucket
143175
):
144-
"""
145-
Test dbt_directory yields a temporary directory and updates profile_dir and
146-
profiles_dir when files have been pulled from s3
176+
"""Test dbt_directory yields a temporary directory and updates attributes.
177+
178+
Certain attributes, like project_dir, profiles_dir, and state, need to be updated to
179+
work once a temporary directory has been created, in particular, when pulling from
180+
S3.
147181
"""
148182
hook = DbtS3Hook()
149183
bucket = hook.get_bucket(s3_bucket)
@@ -161,11 +195,16 @@ def test_dbt_base_dbt_directory_changed_to_s3(
161195
project_dir=f"s3://{s3_bucket}/dbt/project/",
162196
profiles_dir=f"s3://{s3_bucket}/dbt/profiles/",
163197
)
198+
op.state = "target/"
199+
164200
with op.dbt_directory() as tmp_dir:
165201
assert Path(tmp_dir).exists()
166202
assert Path(tmp_dir).is_dir()
203+
167204
assert op.project_dir == f"{tmp_dir}/"
168205
assert op.profiles_dir == f"{tmp_dir}/"
206+
assert op.state == f"{tmp_dir}/target"
207+
169208
assert Path(f"{tmp_dir}/profiles.yml").exists()
170209
assert Path(f"{tmp_dir}/profiles.yml").is_file()
171210
assert Path(f"{tmp_dir}/dbt_project.yml").exists()

0 commit comments

Comments
 (0)