From 650f97a08f29a688c530e5f6c9eedc9269ed7bdc Mon Sep 17 00:00:00 2001 From: Simon Willison Date: Sat, 8 Jan 2022 13:34:01 -0800 Subject: [PATCH 1/2] Initial prototype of .analyze(), refs #366 --- sqlite_utils/db.py | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/sqlite_utils/db.py b/sqlite_utils/db.py index dfc4723..1348b4a 100644 --- a/sqlite_utils/db.py +++ b/sqlite_utils/db.py @@ -923,6 +923,13 @@ class Database: "Run a SQLite ``VACUUM`` against the database." self.execute("VACUUM;") + def analyze(self, name=None): + "Run ``ANALYZE`` against the entire database or a named table or index." + sql = "ANALYZE" + if name is not None: + sql += " [{}]".format(name) + self.execute(sql) + class Queryable: def exists(self) -> bool: @@ -2902,6 +2909,10 @@ class Table(Queryable): ) return self + def analyze(self): + "Run ANALYZE against this table" + self.db.analyze(self.name) + def analyze_column( self, column: str, common_limit: int = 10, value_truncate=None, total_rows=None ) -> "ColumnDetails": From 9848eaa61b43de0ddb74ff6d085bcb18f4640f91 Mon Sep 17 00:00:00 2001 From: Simon Willison Date: Sat, 8 Jan 2022 18:33:00 -0800 Subject: [PATCH 2/2] Support 'python -m sqlite_utils', closes #368 Refs #364 --- docs/cli.rst | 2 ++ sqlite_utils/__main__.py | 4 ++++ tests/test_cli.py | 11 +++++++++++ 3 files changed, 17 insertions(+) create mode 100644 sqlite_utils/__main__.py diff --git a/docs/cli.rst b/docs/cli.rst index 0fe6669..e9d2218 100644 --- a/docs/cli.rst +++ b/docs/cli.rst @@ -6,6 +6,8 @@ The ``sqlite-utils`` command-line tool can be used to manipulate SQLite databases in a number of different ways. +Once installed, the tool should be available as ``sqlite-utils``. It can also be run using ``python -m sqlite_utils``. + .. contents:: :local: .. _cli_query: diff --git a/sqlite_utils/__main__.py b/sqlite_utils/__main__.py new file mode 100644 index 0000000..98dcca0 --- /dev/null +++ b/sqlite_utils/__main__.py @@ -0,0 +1,4 @@ +from .cli import cli + +if __name__ == "__main__": + cli() diff --git a/tests/test_cli.py b/tests/test_cli.py index b789b1e..336ce7b 100644 --- a/tests/test_cli.py +++ b/tests/test_cli.py @@ -1,6 +1,8 @@ from sqlite_utils import cli, Database from sqlite_utils.db import Index, ForeignKey from click.testing import CliRunner +import subprocess +import sys from unittest import mock import json import os @@ -2017,3 +2019,12 @@ def test_integer_overflow_error(tmpdir): "sql = INSERT INTO [items] ([bignumber]) VALUES (?);\n" "parameters = [34223049823094832094802398430298048240]\n" ) + + +def test_python_dash_m(): + "Tool can be run using python -m sqlite_utils" + result = subprocess.run( + [sys.executable, "-m", "sqlite_utils", "--help"], capture_output=True + ) + assert result.returncode == 0 + assert b"Commands for interacting with a SQLite database" in result.stdout