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

fix: preserve is_dttm on column update #156

Merged
merged 1 commit into from
Dec 1, 2022
Merged
Show file tree
Hide file tree
Changes from all 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
25 changes: 17 additions & 8 deletions src/preset_cli/cli/superset/sync/dbt/datasets.py
Original file line number Diff line number Diff line change
Expand Up @@ -149,14 +149,23 @@ def sync_datasets( # pylint: disable=too-many-locals, too-many-branches, too-ma
client.update_dataset(dataset["id"], override_columns=False, **update)

# update column descriptions
update = {
"columns": [
{"column_name": name, "description": column.get("description", "")}
for name, column in model.get("columns", {}).items()
],
}
if update["columns"]:
client.update_dataset(dataset["id"], override_columns=True, **update)
if columns := model.get("columns"):
current_columns = client.get_dataset(dataset["id"])["columns"]
for column in current_columns:
name = column["column_name"]
if name in columns:
column["description"] = columns[name].get("description", "")

# remove columns that are not part of the update payload
for key in ("changed_on", "created_on", "type_generic"):
if key in column:
del column[key]

client.update_dataset(
dataset["id"],
override_columns=True,
columns=current_columns,
)

datasets.append(dataset)

Expand Down
23 changes: 23 additions & 0 deletions tests/cli/superset/sync/dbt/datasets_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,12 @@ def test_sync_datasets_new(mocker: MockerFixture) -> None:
client = mocker.MagicMock()
client.get_datasets.return_value = []
client.create_dataset.side_effect = [{"id": 1}, {"id": 2}, {"id": 3}]
client.get_dataset.return_value = {
"columns": [
{"column_name": "id", "is_dttm": False, "type_generic": "INTEGER"},
{"column_name": "ts", "is_dttm": True, "type_generic": "TIMESTAMP"},
],
}

sync_datasets(
client=client,
Expand Down Expand Up @@ -109,6 +115,11 @@ def test_sync_datasets_new(mocker: MockerFixture) -> None:
{
"column_name": "id",
"description": "Primary key",
"is_dttm": False,
},
{
"column_name": "ts",
"is_dttm": True,
},
],
),
Expand All @@ -123,6 +134,9 @@ def test_sync_datasets_no_metrics(mocker: MockerFixture) -> None:
client = mocker.MagicMock()
client.get_datasets.return_value = []
client.create_dataset.side_effect = [{"id": 1}, {"id": 2}, {"id": 3}]
client.get_dataset.return_value = {
"columns": [{"column_name": "id", "is_dttm": False}],
}

sync_datasets(
client=client,
Expand Down Expand Up @@ -160,6 +174,7 @@ def test_sync_datasets_no_metrics(mocker: MockerFixture) -> None:
{
"column_name": "id",
"description": "Primary key",
"is_dttm": False,
},
],
),
Expand Down Expand Up @@ -201,6 +216,9 @@ def test_sync_datasets_existing(mocker: MockerFixture) -> None:
"""
client = mocker.MagicMock()
client.get_datasets.side_effect = [[{"id": 1}], [{"id": 2}], [{"id": 3}]]
client.get_dataset.return_value = {
"columns": [{"column_name": "id", "is_dttm": False}],
}

sync_datasets(
client=client,
Expand Down Expand Up @@ -248,6 +266,7 @@ def test_sync_datasets_existing(mocker: MockerFixture) -> None:
{
"column_name": "id",
"description": "Primary key",
"is_dttm": False,
},
],
),
Expand Down Expand Up @@ -280,6 +299,9 @@ def test_sync_datasets_external_url(mocker: MockerFixture) -> None:
"""
client = mocker.MagicMock()
client.get_datasets.side_effect = [[{"id": 1}], [{"id": 2}], [{"id": 3}]]
client.get_dataset.return_value = {
"columns": [{"column_name": "id", "is_dttm": False}],
}

sync_datasets(
client=client,
Expand Down Expand Up @@ -331,6 +353,7 @@ def test_sync_datasets_external_url(mocker: MockerFixture) -> None:
{
"column_name": "id",
"description": "Primary key",
"is_dttm": False,
},
],
),
Expand Down