Add ability to debug queries

This commit is contained in:
Šarūnas Nejus 2024-05-02 12:14:03 +01:00
parent faf7529aa9
commit b25d0b1cbd
No known key found for this signature in database
GPG key ID: DD28F6704DBE3435
3 changed files with 134 additions and 2 deletions

View file

@ -20,6 +20,7 @@ import contextlib
import os
import re
import sqlite3
import sys
import threading
import time
from abc import ABC
@ -28,6 +29,8 @@ from collections.abc import Generator, Iterable, Iterator, Mapping, Sequence
from sqlite3 import Connection
from typing import TYPE_CHECKING, Any, AnyStr, Callable, Generic, TypeVar, cast
from rich import print
from rich_tables.generic import flexitable
from unidecode import unidecode
import beets
@ -55,10 +58,22 @@ if TYPE_CHECKING:
else:
D = TypeVar("D", bound="Database")
DEBUG = bool(os.getenv("BEETS_DEBUG", False))
FlexAttrs = dict[str, str]
def print_query(sql, subvals=None):
"""If debugging, replace placeholders and print the query."""
if not DEBUG:
return
topr = sql
for val in subvals or []:
topr = topr.replace("?", str(val), 1)
print(flexitable({"sql": topr}), file=sys.stderr)
class DBAccessError(Exception):
"""The SQLite database became inaccessible.
@ -973,6 +988,7 @@ class Transaction:
"""Execute an SQL statement with substitution values and return
a list of rows from the database.
"""
print_query(statement, subvals)
cursor = self.db._connection().execute(statement, subvals)
return cursor.fetchall()
@ -981,6 +997,7 @@ class Transaction:
the row ID of the last affected row.
"""
try:
print_query(statement, subvals)
cursor = self.db._connection().execute(statement, subvals)
except sqlite3.OperationalError as e:
# In two specific cases, SQLite reports an error while accessing
@ -1001,6 +1018,7 @@ class Transaction:
"""Execute a string containing multiple SQL statements."""
# We don't know whether this mutates, but quite likely it does.
self._mutated = True
print_query(statements)
self.db._connection().executescript(statements)

117
poetry.lock generated
View file

@ -728,6 +728,17 @@ files = [
[package.dependencies]
Flask = ">=0.9"
[[package]]
name = "funcy"
version = "2.0"
description = "A fancy and practical functional tools"
optional = false
python-versions = "*"
files = [
{file = "funcy-2.0-py2.py3-none-any.whl", hash = "sha256:53df23c8bb1651b12f095df764bfb057935d49537a56de211b098f4c79614bb0"},
{file = "funcy-2.0.tar.gz", hash = "sha256:3963315d59d41c6f30c04bc910e10ab50a3ac4a225868bfa96feed133df075cb"},
]
[[package]]
name = "h11"
version = "0.14.0"
@ -1364,6 +1375,30 @@ html5 = ["html5lib"]
htmlsoup = ["BeautifulSoup4"]
source = ["Cython (>=3.0.11)"]
[[package]]
name = "markdown-it-py"
version = "3.0.0"
description = "Python port of markdown-it. Markdown parsing, done right!"
optional = false
python-versions = ">=3.8"
files = [
{file = "markdown-it-py-3.0.0.tar.gz", hash = "sha256:e3f60a94fa066dc52ec76661e37c851cb232d92f9886b15cb560aaada2df8feb"},
{file = "markdown_it_py-3.0.0-py3-none-any.whl", hash = "sha256:355216845c60bd96232cd8d8c40e8f9765cc86f46880e43a8fd22dc1a1a8cab1"},
]
[package.dependencies]
mdurl = ">=0.1,<1.0"
[package.extras]
benchmarking = ["psutil", "pytest", "pytest-benchmark"]
code-style = ["pre-commit (>=3.0,<4.0)"]
compare = ["commonmark (>=0.9,<1.0)", "markdown (>=3.4,<4.0)", "mistletoe (>=1.0,<2.0)", "mistune (>=2.0,<3.0)", "panflute (>=2.3,<3.0)"]
linkify = ["linkify-it-py (>=1,<3)"]
plugins = ["mdit-py-plugins"]
profiling = ["gprof2dot"]
rtd = ["jupyter_sphinx", "mdit-py-plugins", "myst-parser", "pyyaml", "sphinx", "sphinx-copybutton", "sphinx-design", "sphinx_book_theme"]
testing = ["coverage", "pytest", "pytest-cov", "pytest-regressions"]
[[package]]
name = "markupsafe"
version = "3.0.2"
@ -1434,6 +1469,17 @@ files = [
{file = "markupsafe-3.0.2.tar.gz", hash = "sha256:ee55d3edf80167e48ea11a923c7386f4669df67d7994554387f84e7d8b0a2bf0"},
]
[[package]]
name = "mdurl"
version = "0.1.2"
description = "Markdown URL utilities"
optional = false
python-versions = ">=3.7"
files = [
{file = "mdurl-0.1.2-py3-none-any.whl", hash = "sha256:84008a41e51615a49fc9966191ff91509e3c40b939176e643fd50a5c2196b8f8"},
{file = "mdurl-0.1.2.tar.gz", hash = "sha256:bb413d29f5eea38f31dd4754dd7377d4465116fb207585f97bf925588687c1ba"},
]
[[package]]
name = "mediafile"
version = "0.13.0"
@ -1541,6 +1587,17 @@ files = [
{file = "msgpack-1.1.0.tar.gz", hash = "sha256:dd432ccc2c72b914e4cb77afce64aab761c1137cc698be3984eee260bcb2896e"},
]
[[package]]
name = "multimethod"
version = "2.0"
description = "Multiple argument dispatching."
optional = false
python-versions = ">=3.9"
files = [
{file = "multimethod-2.0-py3-none-any.whl", hash = "sha256:45aa231dc9dbb7f980c0f2ad8179e2c2b72a8cd5c7d7534337be66dde29d35be"},
{file = "multimethod-2.0.tar.gz", hash = "sha256:c628b6d2e7d61fbe58484dd884d990901e8314faf58af062e72b65e3423cb109"},
]
[[package]]
name = "multivolumefile"
version = "0.2.3"
@ -2133,7 +2190,7 @@ test = ["pytest", "pytest-cov", "pytest-regressions", "sphinx[test]"]
name = "pygments"
version = "2.18.0"
description = "Pygments is a syntax highlighting package written in Python."
optional = true
optional = false
python-versions = ">=3.8"
files = [
{file = "pygments-2.18.0-py3-none-any.whl", hash = "sha256:b8e6aca0523f3ab76fee51799c488e38782ac06eafcf95e7ba832985c8e7b13a"},
@ -2634,6 +2691,47 @@ urllib3 = ">=1.25.10,<3.0"
[package.extras]
tests = ["coverage (>=6.0.0)", "flake8", "mypy", "pytest (>=7.0.0)", "pytest-asyncio", "pytest-cov", "pytest-httpserver", "tomli", "tomli-w", "types-PyYAML", "types-requests"]
[[package]]
name = "rich"
version = "13.9.4"
description = "Render rich text, tables, progress bars, syntax highlighting, markdown and more to the terminal"
optional = false
python-versions = ">=3.8.0"
files = [
{file = "rich-13.9.4-py3-none-any.whl", hash = "sha256:6049d5e6ec054bf2779ab3358186963bac2ea89175919d699e378b99738c2a90"},
{file = "rich-13.9.4.tar.gz", hash = "sha256:439594978a49a09530cff7ebc4b5c7103ef57baf48d5ea3184f21d9a2befa098"},
]
[package.dependencies]
markdown-it-py = ">=2.2.0"
pygments = ">=2.13.0,<3.0.0"
typing-extensions = {version = ">=4.0.0,<5.0", markers = "python_version < \"3.11\""}
[package.extras]
jupyter = ["ipywidgets (>=7.5.1,<9)"]
[[package]]
name = "rich-tables"
version = "0.6.1"
description = "Ready-made rich tables for various purposes"
optional = false
python-versions = "<4,>=3.8"
files = [
{file = "rich_tables-0.6.1-py3-none-any.whl", hash = "sha256:9d7ee5ea2300d1669d066d0ef2d0694e669b90f50c5a8573edba05e086f007d3"},
{file = "rich_tables-0.6.1.tar.gz", hash = "sha256:57a1a5bbd2051b2ab2b3c30170f095846119e52df80ddf276647e656de3ac632"},
]
[package.dependencies]
funcy = ">=2.0"
multimethod = "*"
platformdirs = ">=4.2.0"
rich = ">=12.3.0"
sqlparse = ">=0.4.4"
typing-extensions = ">=4.7.1"
[package.extras]
hue = ["rgbxy (>=0.5)"]
[[package]]
name = "ruff"
version = "0.8.1"
@ -3009,6 +3107,21 @@ lint = ["mypy", "ruff (==0.5.5)", "types-docutils"]
standalone = ["Sphinx (>=5)"]
test = ["pytest"]
[[package]]
name = "sqlparse"
version = "0.5.3"
description = "A non-validating SQL parser."
optional = false
python-versions = ">=3.8"
files = [
{file = "sqlparse-0.5.3-py3-none-any.whl", hash = "sha256:cf2196ed3418f3ba5de6af7e82c694a9fbdbfecccdfc72e281548517081f16ca"},
{file = "sqlparse-0.5.3.tar.gz", hash = "sha256:09f67787f56a0b16ecdbde1bfc7f5d9c3371ca683cfeaa8e6ff60b4807ec9272"},
]
[package.extras]
dev = ["build", "hatch"]
doc = ["sphinx"]
[[package]]
name = "texttable"
version = "1.7.0"
@ -3274,4 +3387,4 @@ web = ["flask", "flask-cors"]
[metadata]
lock-version = "2.0"
python-versions = ">=3.9,<4"
content-hash = "b6b44295999e2b8c3868b03321df60a2501abc9162a7e802de37ab2ae8aa14ff"
content-hash = "4228984605455bd11a96a1921d10800b4c61d5cd900d5d65a6302918629e8e46"

View file

@ -50,6 +50,7 @@ musicbrainzngs = ">=0.4"
numpy = ">=1.24.4"
platformdirs = ">=3.5.0"
pyyaml = "*"
rich-tables = ">=0.5.1"
typing_extensions = { version = "*", python = "<=3.10" }
unidecode = ">=1.3.6"