Skip to content

Commit

Permalink
Merge pull request z3z1ma#182 from nijigen-plot/feature/add-catalog-d…
Browse files Browse the repository at this point in the history
…escription-on-yml-create

Overwrite description when --catalog-file is specified
  • Loading branch information
z3z1ma authored Nov 15, 2024
2 parents 53e5a8f + 0c08a83 commit 93ef203
Showing 1 changed file with 42 additions and 17 deletions.
59 changes: 42 additions & 17 deletions src/dbt_osmosis/core/osmosis.py
Original file line number Diff line number Diff line change
Expand Up @@ -898,6 +898,7 @@ def _run(
n_cols_doc_inherited = 0
n_cols_removed = 0
n_cols_data_type_changed = 0
n_cols_description_changed = 0

with self.mutex:
schema_file = self.yaml_handler.load(schema_path.current)
Expand All @@ -907,19 +908,21 @@ def _run(
return

should_dump = False
n_cols_added, n_cols_doc_inherited, n_cols_removed, n_cols_data_type_changed = (
0,
0,
0,
0,
)
(
n_cols_added,
n_cols_doc_inherited,
n_cols_removed,
n_cols_data_type_changed,
n_cols_description_changed,
) = (0, 0, 0, 0, 0)
if len(missing_columns) > 0 or len(undocumented_columns) or len(extra_columns) > 0:
# Update schema file
(
n_cols_added,
n_cols_doc_inherited,
n_cols_removed,
n_cols_data_type_changed,
n_cols_description_changed,
) = self.update_schema_file_and_node(
missing_columns,
undocumented_columns,
Expand All @@ -929,7 +932,11 @@ def _run(
columns_db_meta,
)
if (
n_cols_added + n_cols_doc_inherited + n_cols_removed + n_cols_data_type_changed
n_cols_added
+ n_cols_doc_inherited
+ n_cols_removed
+ n_cols_data_type_changed
+ n_cols_description_changed
> 0
):
should_dump = True
Expand Down Expand Up @@ -1026,26 +1033,33 @@ def remove_columns_not_in_database(
)
return changes_committed

def update_columns_data_type(
def update_columns_attribute(
self,
node: ManifestNode,
yaml_file_model_section: Dict[str, Any],
columns_db_meta: Dict[str, ColumnMetadata],
attribute_name: str,
meta_key: str,
skip_attribute_update: Any,
) -> int:
changes_committed = 0
if self.skip_add_data_types:
if (skip_attribute_update is True) or (skip_attribute_update is None):
return changes_committed
for column in columns_db_meta:
cased_column_name = self.column_casing(column)
if cased_column_name in node.columns:
if columns_db_meta.get(cased_column_name):
data_type = columns_db_meta.get(cased_column_name).type
if node.columns[cased_column_name].data_type == data_type:
column_meta_obj = columns_db_meta.get(cased_column_name)
if column_meta_obj:
column_meta = getattr(column_meta_obj, meta_key, "")
if column_meta is None:
column_meta = ""
current_value = getattr(node.columns[cased_column_name], attribute_name, "")
if current_value == column_meta:
continue
node.columns[cased_column_name].data_type = data_type
setattr(node.columns[cased_column_name], attribute_name, column_meta)
for model_column in yaml_file_model_section["columns"]:
if self.column_casing(model_column["name"]) == cased_column_name:
model_column.update({"data_type": data_type})
model_column.update({attribute_name: column_meta})
changes_committed += 1
return changes_committed

Expand Down Expand Up @@ -1088,7 +1102,7 @@ def update_schema_file_and_node(
node: ManifestNode,
section: Dict[str, Any],
columns_db_meta: Dict[str, ColumnMetadata],
) -> Tuple[int, int, int, int]:
) -> Tuple[int, int, int, int, int]:
"""Take action on a schema file mirroring changes in the node."""
logger().info(":microscope: Looking for actions for %s", node.unique_id)
n_cols_added = 0
Expand Down Expand Up @@ -1116,9 +1130,20 @@ def update_schema_file_and_node(
self.add_inheritance_for_specified_keys,
)
)
n_cols_data_type_updated = self.update_columns_data_type(node, section, columns_db_meta)
n_cols_data_type_updated = self.update_columns_attribute(
node, section, columns_db_meta, "data_type", "type", self.skip_add_data_types
)
n_cols_description_updated = self.update_columns_attribute(
node, section, columns_db_meta, "description", "comment", self.catalog_file
)
n_cols_removed = self.remove_columns_not_in_database(extra_columns, node, section)
return n_cols_added, n_cols_doc_inherited, n_cols_removed, n_cols_data_type_updated
return (
n_cols_added,
n_cols_doc_inherited,
n_cols_removed,
n_cols_data_type_updated,
n_cols_description_updated,
)

@staticmethod
def maybe_get_section_from_schema_file(
Expand Down

0 comments on commit 93ef203

Please sign in to comment.