Compare commits

...

2 commits

Author SHA1 Message Date
Simon Willison
9848eaa61b Support 'python -m sqlite_utils', closes #368
Refs #364
2022-01-08 18:33:00 -08:00
Simon Willison
650f97a08f Initial prototype of .analyze(), refs #366 2022-01-08 13:34:01 -08:00
4 changed files with 28 additions and 0 deletions

View file

@ -6,6 +6,8 @@
The ``sqlite-utils`` command-line tool can be used to manipulate SQLite databases in a number of different ways. 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: .. contents:: :local:
.. _cli_query: .. _cli_query:

4
sqlite_utils/__main__.py Normal file
View file

@ -0,0 +1,4 @@
from .cli import cli
if __name__ == "__main__":
cli()

View file

@ -923,6 +923,13 @@ class Database:
"Run a SQLite ``VACUUM`` against the database." "Run a SQLite ``VACUUM`` against the database."
self.execute("VACUUM;") 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: class Queryable:
def exists(self) -> bool: def exists(self) -> bool:
@ -2902,6 +2909,10 @@ class Table(Queryable):
) )
return self return self
def analyze(self):
"Run ANALYZE against this table"
self.db.analyze(self.name)
def analyze_column( def analyze_column(
self, column: str, common_limit: int = 10, value_truncate=None, total_rows=None self, column: str, common_limit: int = 10, value_truncate=None, total_rows=None
) -> "ColumnDetails": ) -> "ColumnDetails":

View file

@ -1,6 +1,8 @@
from sqlite_utils import cli, Database from sqlite_utils import cli, Database
from sqlite_utils.db import Index, ForeignKey from sqlite_utils.db import Index, ForeignKey
from click.testing import CliRunner from click.testing import CliRunner
import subprocess
import sys
from unittest import mock from unittest import mock
import json import json
import os import os
@ -2017,3 +2019,12 @@ def test_integer_overflow_error(tmpdir):
"sql = INSERT INTO [items] ([bignumber]) VALUES (?);\n" "sql = INSERT INTO [items] ([bignumber]) VALUES (?);\n"
"parameters = [34223049823094832094802398430298048240]\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