Skip to content

Commit

Permalink
Overwrite existing models in schema.yml file (#95)
Browse files Browse the repository at this point in the history
* overwrite existing models in schema vs append-only

* bump version

* apply pr feedback
  • Loading branch information
griffatrasgo authored Jul 5, 2022
1 parent 4619917 commit 8ab6d68
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 11 deletions.
11 changes: 11 additions & 0 deletions rasgoql/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,15 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
### Fixed
- Fixed a postgres bug

## [1.6.3] - 2022-06-27
### Added
- Added a jinja date macro

## [1.6.4] - 2022-07-05
### Changed
- Changed default behavior of to_dbt function. Instead of always appending model details to the schema.yml file (which creates duplicate entries for existing models), rql will now check if a model entry already exists in the file and overwrite it. If the model does not exist, it will be appended.


[1.0.0]: https://pypi.org/project/rasgoql/1.0.0/
[1.0.1]: https://pypi.org/project/rasgoql/1.0.1/
[1.0.2]: https://pypi.org/project/rasgoql/1.0.2/
Expand All @@ -137,3 +146,5 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
[1.6.0]: https://pypi.org/project/rasgoql/1.6.0/
[1.6.1]: https://pypi.org/project/rasgoql/1.6.1/
[1.6.2]: https://pypi.org/project/rasgoql/1.6.2/
[1.6.3]: https://pypi.org/project/rasgoql/1.6.3/
[1.6.4]: https://pypi.org/project/rasgoql/1.6.4/
29 changes: 19 additions & 10 deletions rasgoql/rasgoql/utils/dbt.py
Original file line number Diff line number Diff line change
Expand Up @@ -134,16 +134,25 @@ def save_schema_file(
Writes a table def to a dbt schema file
"""
filepath = os.path.join(output_directory, 'schema.yml')
columns_list = []
for row in schema:
columns_list.append({"name:": row[0]})
model_dict = {"name": model_name, "columns": columns_list}
if config_args:
model_dict.update({"config": config_args})
if not os.path.exists(filepath):
schema_definition = {"version": 2, "models": [model_dict]}
existing_schema = None
if os.path.exists(filepath):
with open(filepath, "r") as _file:
existing_schema = yaml.safe_load(_file)
schema_definition = existing_schema or {"version": 2, "models": []}

columns_list = [{"name:": row[0]} for row in schema]
for mod in schema_definition["models"]:
if mod.get("name") == model_name:
mod["columns"] = columns_list
if config_args:
mod["config"] = config_args
break
else:
schema_definition = [model_dict]
with open(filepath, "a") as _file:
model_dict = {"name": model_name, "columns": columns_list}
if config_args:
model_dict.update({"config": config_args})
schema_definition["models"].append(model_dict)

with open(filepath, "w") as _file:
yaml.dump(data=schema_definition, Dumper=yaml.SafeDumper, stream=_file, sort_keys=False)
return filepath
2 changes: 1 addition & 1 deletion rasgoql/rasgoql/version.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
"""
Package version for pypi
"""
__version__ = '1.6.3'
__version__ = '1.6.4'

0 comments on commit 8ab6d68

Please sign in to comment.