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

feat(magic): add dryrun config option to only print SQL output #159

Merged
merged 2 commits into from
Mar 25, 2023
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
5 changes: 4 additions & 1 deletion pyprql/magic/prql.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ class PrqlMagic(SqlMagic):
autoview = Bool(True, config=True, help="Display results")
feedback = Bool(False, config=True, help="Print number of rows affected by DML")
target = Unicode("sql.any", config=True, help="Compile target of prql-compiler")
dryrun = Bool(False, config=True, help="Only print the compiled SQL")
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would it be better to use a different name?


@needs_local_scope
@line_magic("prql")
Expand Down Expand Up @@ -101,6 +102,8 @@ def prql(
cell,
CompileOptions(target=self.target, format=True, signature_comment=True),
)

if self.dryrun:
print(cell)
return None
result = super().execute(line=line, cell=cell, local_ns=local_ns)
return result
8 changes: 8 additions & 0 deletions pyprql/tests/test_magic.py
Original file line number Diff line number Diff line change
Expand Up @@ -285,6 +285,14 @@ def test_without_target(ip, capsys):
)


def test_dryrun(ip, capsys):
ip.run_line_magic("config", "PrqlMagic.dryrun = True")
result = run_prql(ip, 'from a | select b = f"{c}-{d}"')
captured = capsys.readouterr()
assert captured.out.startswith("\nSELECT\n CONCAT")
assert result is None


def test_csv(ip):
ip.run_line_magic("config", "SqlMagic.autopandas = False") # uh-oh
result = run_prql(ip, "from test")
Expand Down