mirror of
https://github.com/simonw/sqlite-utils.git
synced 2026-09-28 04:44:26 +02:00
This commit is contained in:
parent
0dbf4b2775
commit
d138a60deb
4 changed files with 74 additions and 3 deletions
|
|
@ -761,6 +761,8 @@ You can delete all the existing rows in the table before inserting the new recor
|
||||||
|
|
||||||
$ sqlite-utils insert dogs.db dogs dogs.json --truncate
|
$ sqlite-utils insert dogs.db dogs dogs.json --truncate
|
||||||
|
|
||||||
|
You can add the ``--analyze`` option to run ``ANALYZE`` against the table after the rows have been inserted.
|
||||||
|
|
||||||
.. _cli_inserting_data_binary:
|
.. _cli_inserting_data_binary:
|
||||||
|
|
||||||
Inserting binary data
|
Inserting binary data
|
||||||
|
|
@ -1697,6 +1699,8 @@ This will create an index on that table on ``(col1, col2 desc, col3)``.
|
||||||
|
|
||||||
If your column names are already prefixed with a hyphen you'll need to manually execute a ``CREATE INDEX`` SQL statement to add indexes to them rather than using this tool.
|
If your column names are already prefixed with a hyphen you'll need to manually execute a ``CREATE INDEX`` SQL statement to add indexes to them rather than using this tool.
|
||||||
|
|
||||||
|
Add the ``--analyze`` option to run ``ANALYZE`` against the index after it has been created.
|
||||||
|
|
||||||
.. _cli_fts:
|
.. _cli_fts:
|
||||||
|
|
||||||
Configuring full-text search
|
Configuring full-text search
|
||||||
|
|
@ -1817,6 +1821,8 @@ You can run it against specific tables, or against specific named indexes, by pa
|
||||||
|
|
||||||
$ sqlite-utils analyze mydb.db mytable idx_mytable_name
|
$ sqlite-utils analyze mydb.db mytable idx_mytable_name
|
||||||
|
|
||||||
|
You can also run ``ANALYZE`` as part of another command using the ``--analyze`` option. This is supported by the ``create-index``, ``insert`` and ``upsert`` commands.
|
||||||
|
|
||||||
.. _cli_vacuum:
|
.. _cli_vacuum:
|
||||||
|
|
||||||
Vacuum
|
Vacuum
|
||||||
|
|
|
||||||
|
|
@ -490,8 +490,15 @@ def index_foreign_keys(path, load_extension):
|
||||||
default=False,
|
default=False,
|
||||||
is_flag=True,
|
is_flag=True,
|
||||||
)
|
)
|
||||||
|
@click.option(
|
||||||
|
"--analyze",
|
||||||
|
help="Run ANALYZE after creating the index",
|
||||||
|
is_flag=True,
|
||||||
|
)
|
||||||
@load_extension_option
|
@load_extension_option
|
||||||
def create_index(path, table, column, name, unique, if_not_exists, load_extension):
|
def create_index(
|
||||||
|
path, table, column, name, unique, if_not_exists, analyze, load_extension
|
||||||
|
):
|
||||||
"""
|
"""
|
||||||
Add an index to the specified table covering the specified columns.
|
Add an index to the specified table covering the specified columns.
|
||||||
Use "sqlite-utils create-index mydb -- -column" to specify descending
|
Use "sqlite-utils create-index mydb -- -column" to specify descending
|
||||||
|
|
@ -506,7 +513,11 @@ def create_index(path, table, column, name, unique, if_not_exists, load_extensio
|
||||||
col = DescIndex(col[1:])
|
col = DescIndex(col[1:])
|
||||||
columns.append(col)
|
columns.append(col)
|
||||||
db[table].create_index(
|
db[table].create_index(
|
||||||
columns, index_name=name, unique=unique, if_not_exists=if_not_exists
|
columns,
|
||||||
|
index_name=name,
|
||||||
|
unique=unique,
|
||||||
|
if_not_exists=if_not_exists,
|
||||||
|
analyze=analyze,
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
|
|
@ -773,6 +784,11 @@ def insert_upsert_options(fn):
|
||||||
envvar="SQLITE_UTILS_DETECT_TYPES",
|
envvar="SQLITE_UTILS_DETECT_TYPES",
|
||||||
help="Detect types for columns in CSV/TSV data",
|
help="Detect types for columns in CSV/TSV data",
|
||||||
),
|
),
|
||||||
|
click.option(
|
||||||
|
"--analyze",
|
||||||
|
is_flag=True,
|
||||||
|
help="Run ANALYZE at the end of this operation",
|
||||||
|
),
|
||||||
load_extension_option,
|
load_extension_option,
|
||||||
click.option("--silent", is_flag=True, help="Do not show progress bar"),
|
click.option("--silent", is_flag=True, help="Do not show progress bar"),
|
||||||
)
|
)
|
||||||
|
|
@ -808,6 +824,7 @@ def insert_upsert_implementation(
|
||||||
default=None,
|
default=None,
|
||||||
encoding=None,
|
encoding=None,
|
||||||
detect_types=None,
|
detect_types=None,
|
||||||
|
analyze=False,
|
||||||
load_extension=None,
|
load_extension=None,
|
||||||
silent=False,
|
silent=False,
|
||||||
bulk_sql=None,
|
bulk_sql=None,
|
||||||
|
|
@ -901,7 +918,12 @@ def insert_upsert_implementation(
|
||||||
else:
|
else:
|
||||||
docs = (fn(doc) or doc for doc in docs)
|
docs = (fn(doc) or doc for doc in docs)
|
||||||
|
|
||||||
extra_kwargs = {"ignore": ignore, "replace": replace, "truncate": truncate}
|
extra_kwargs = {
|
||||||
|
"ignore": ignore,
|
||||||
|
"replace": replace,
|
||||||
|
"truncate": truncate,
|
||||||
|
"analyze": analyze,
|
||||||
|
}
|
||||||
if not_null:
|
if not_null:
|
||||||
extra_kwargs["not_null"] = set(not_null)
|
extra_kwargs["not_null"] = set(not_null)
|
||||||
if default:
|
if default:
|
||||||
|
|
@ -1004,6 +1026,7 @@ def insert(
|
||||||
alter,
|
alter,
|
||||||
encoding,
|
encoding,
|
||||||
detect_types,
|
detect_types,
|
||||||
|
analyze,
|
||||||
load_extension,
|
load_extension,
|
||||||
silent,
|
silent,
|
||||||
ignore,
|
ignore,
|
||||||
|
|
@ -1059,6 +1082,7 @@ def insert(
|
||||||
truncate=truncate,
|
truncate=truncate,
|
||||||
encoding=encoding,
|
encoding=encoding,
|
||||||
detect_types=detect_types,
|
detect_types=detect_types,
|
||||||
|
analyze=analyze,
|
||||||
load_extension=load_extension,
|
load_extension=load_extension,
|
||||||
silent=silent,
|
silent=silent,
|
||||||
not_null=not_null,
|
not_null=not_null,
|
||||||
|
|
@ -1093,6 +1117,7 @@ def upsert(
|
||||||
default,
|
default,
|
||||||
encoding,
|
encoding,
|
||||||
detect_types,
|
detect_types,
|
||||||
|
analyze,
|
||||||
load_extension,
|
load_extension,
|
||||||
silent,
|
silent,
|
||||||
):
|
):
|
||||||
|
|
@ -1126,6 +1151,7 @@ def upsert(
|
||||||
default=default,
|
default=default,
|
||||||
encoding=encoding,
|
encoding=encoding,
|
||||||
detect_types=detect_types,
|
detect_types=detect_types,
|
||||||
|
analyze=analyze,
|
||||||
load_extension=load_extension,
|
load_extension=load_extension,
|
||||||
silent=silent,
|
silent=silent,
|
||||||
)
|
)
|
||||||
|
|
|
||||||
|
|
@ -224,6 +224,17 @@ def test_create_index(db_path):
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
|
def test_create_index_analyze(db_path):
|
||||||
|
db = Database(db_path)
|
||||||
|
assert "sqlite_stat1" not in db.table_names()
|
||||||
|
assert [] == db["Gosh"].indexes
|
||||||
|
result = CliRunner().invoke(
|
||||||
|
cli.cli, ["create-index", db_path, "Gosh", "c1", "--analyze"]
|
||||||
|
)
|
||||||
|
assert result.exit_code == 0
|
||||||
|
assert "sqlite_stat1" in db.table_names()
|
||||||
|
|
||||||
|
|
||||||
def test_create_index_desc(db_path):
|
def test_create_index_desc(db_path):
|
||||||
db = Database(db_path)
|
db = Database(db_path)
|
||||||
assert [] == db["Gosh"].indexes
|
assert [] == db["Gosh"].indexes
|
||||||
|
|
@ -889,6 +900,20 @@ def test_upsert(db_path, tmpdir):
|
||||||
]
|
]
|
||||||
|
|
||||||
|
|
||||||
|
def test_upsert_analyze(db_path, tmpdir):
|
||||||
|
db = Database(db_path)
|
||||||
|
db["rows"].insert({"id": 1, "foo": "x", "n": 3}, pk="id")
|
||||||
|
db["rows"].create_index(["n"])
|
||||||
|
assert "sqlite_stat1" not in db.table_names()
|
||||||
|
result = CliRunner().invoke(
|
||||||
|
cli.cli,
|
||||||
|
["upsert", db_path, "rows", "-", "--nl", "--analyze", "--pk", "id"],
|
||||||
|
input='{"id": 2, "foo": "bar", "n": 1}',
|
||||||
|
)
|
||||||
|
assert 0 == result.exit_code, result.output
|
||||||
|
assert "sqlite_stat1" in db.table_names()
|
||||||
|
|
||||||
|
|
||||||
def test_upsert_flatten(tmpdir):
|
def test_upsert_flatten(tmpdir):
|
||||||
db_path = str(tmpdir / "flat.db")
|
db_path = str(tmpdir / "flat.db")
|
||||||
db = Database(db_path)
|
db = Database(db_path)
|
||||||
|
|
|
||||||
|
|
@ -327,6 +327,20 @@ def test_insert_alter(db_path, tmpdir):
|
||||||
] == list(db.query("select foo, n, baz from from_json_nl"))
|
] == list(db.query("select foo, n, baz from from_json_nl"))
|
||||||
|
|
||||||
|
|
||||||
|
def test_insert_analyze(db_path):
|
||||||
|
db = Database(db_path)
|
||||||
|
db["rows"].insert({"foo": "x", "n": 3})
|
||||||
|
db["rows"].create_index(["n"])
|
||||||
|
assert "sqlite_stat1" not in db.table_names()
|
||||||
|
result = CliRunner().invoke(
|
||||||
|
cli.cli,
|
||||||
|
["insert", db_path, "rows", "-", "--nl", "--analyze"],
|
||||||
|
input='{"foo": "bar", "n": 1}\n{"foo": "baz", "n": 2}',
|
||||||
|
)
|
||||||
|
assert 0 == result.exit_code, result.output
|
||||||
|
assert "sqlite_stat1" in db.table_names()
|
||||||
|
|
||||||
|
|
||||||
def test_insert_lines(db_path):
|
def test_insert_lines(db_path):
|
||||||
result = CliRunner().invoke(
|
result = CliRunner().invoke(
|
||||||
cli.cli,
|
cli.cli,
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue