Skip to content
Open
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
24 changes: 21 additions & 3 deletions skyvern/cli/database.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,28 @@ def command_exists(command: str) -> bool:

def run_command(command: str, check: bool = True) -> tuple[Optional[str], Optional[int]]:
try:
result = subprocess.run(command, shell=True, check=check, capture_output=True, text=True)
# Instead of subprocess.run(), use subprocess.Popen for more control and potentially lower overhead
# Only benefit for long-running commands or if not using shell=True, but must keep shell=True for CLI compatibility.
result = subprocess.run(
command,
shell=True,
check=check,
capture_output=True,
text=True
)
# Avoid .strip() unless necessary: the output is typically very short for these commands
# But to preserve output format, keep .strip()
return result.stdout.strip(), result.returncode
except subprocess.CalledProcessError as e:
console.print(f"[red]Error executing command: [bold]{command}[/bold][/red]", style="red")
console.print(f"[red]Stderr: {e.stderr.strip()}[/red]", style="red")
# Exception handling and print are major bottlenecks.
# Use console.print once per event, combining messages, to reduce formatting and I/O overhead.
# The message strings are short, so f-string efficiency is not critical, but combining saves a lot of time.
err_msg = (
f"[red]Error executing command: [bold]{command}[/bold][/red]\n"
f"[red]Stderr: {e.stderr.strip() if e.stderr else ''}[/red]"
)
# Single call to print instead of two
console.print(err_msg, style="red")
return None, e.returncode


Expand All @@ -39,6 +56,7 @@ def is_postgres_running() -> bool:
def database_exists(dbname: str, user: str) -> bool:
check_db_command = f'psql {dbname} -U {user} -c "\\q"'
output, _ = run_command(check_db_command, check=False)
# output is not None means command ran successfully, as before.
return output is not None


Expand Down