Skip to content

Commit

Permalink
dependent snippet deletion bug fix (#896)
Browse files Browse the repository at this point in the history
* Empty-Commit

* Snippet deletion error

* delete snippet fix

* rebase

* fix

* duckdb pinned

* fix test
  • Loading branch information
neelasha23 authored Sep 27, 2023
1 parent b282122 commit 0d1f1d9
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 1 deletion.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

## 0.10.3dev

* [Fix] Remove force deleted snippets from dependent snippet's `with` (#717)

## 0.10.2 (2023-09-22)

* [Feature] Improved messages when loading configurations from `pyproject.toml` file.
Expand Down
4 changes: 3 additions & 1 deletion src/sql/cmd/snippets.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ def _modify_display_msg(key, remaining_keys, dependent_keys=None):
if dependent_keys:
msg = f"{msg}{', '.join(dependent_keys)} depend on {key}\n"
if remaining_keys:
msg = f"{msg}Stored snippets : {', '.join(remaining_keys)}"
msg = f"{msg}Stored snippets: {', '.join(remaining_keys)}"
else:
msg = f"{msg}There are no stored snippets"
return msg
Expand Down Expand Up @@ -104,6 +104,8 @@ def snippets(others):
key = args.delete_force
deps = store.get_key_dependents(key)
remaining_keys = store.del_saved_key(key)
for dep in deps:
store.store[dep].remove_snippet_dependency(key)
return _modify_display_msg(key, remaining_keys, deps)

elif args.delete_force_all:
Expand Down
4 changes: 4 additions & 0 deletions src/sql/store.py
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,10 @@ def __str__(self) -> str:
rts=_remove_trailing_semicolon,
)

def remove_snippet_dependency(self, snippet):
if snippet in self._with_:
self._with_.remove(snippet)


def _remove_trailing_semicolon(query):
query_ = query.rstrip()
Expand Down
7 changes: 7 additions & 0 deletions src/tests/test_magic_cmd.py
Original file line number Diff line number Diff line change
Expand Up @@ -789,6 +789,13 @@ def test_delete_invalid_snippet(arg, ip_snippets):
assert str(excinfo.value) == "No such saved snippet found : non_existent_snippet"


@pytest.mark.parametrize("arg", ["--delete-force", "-D"])
def test_delete_snippet_when_dependency_force_deleted(ip_snippets, arg):
ip_snippets.run_cell(f"%sqlcmd snippets {arg} high_price")
out = ip_snippets.run_cell("%sqlcmd snippets --delete high_price_a").result
assert "high_price_a has been deleted.\nStored snippets: high_price_b" in out


@pytest.mark.parametrize(
"arguments", ["--table schema1.table1", "--table table1 --schema schema1"]
)
Expand Down

0 comments on commit 0d1f1d9

Please sign in to comment.