mirror of
https://github.com/simonw/sqlite-utils.git
synced 2026-07-26 10:54:32 +02:00
Compare commits
5 commits
main
...
index-fore
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
d82b000e3d | ||
|
|
1e917efe31 | ||
|
|
61164d98cf | ||
|
|
093c3e66db | ||
|
|
f35e4e6662 |
6 changed files with 64 additions and 0 deletions
|
|
@ -310,6 +310,15 @@ If you omit the other table and other column references ``sqlite-utils`` will at
|
||||||
|
|
||||||
See :ref:`python_api_add_foreign_key` in the Python API documentation for further details, including how the automatic table guessing mechanism works.
|
See :ref:`python_api_add_foreign_key` in the Python API documentation for further details, including how the automatic table guessing mechanism works.
|
||||||
|
|
||||||
|
.. _cli_index_foreign_keys:
|
||||||
|
|
||||||
|
Adding indexes for all foreign keys
|
||||||
|
-----------------------------------
|
||||||
|
|
||||||
|
If you want to ensure that every foreign key column in your database has a corresponding index, you can do so like this::
|
||||||
|
|
||||||
|
$ sqlite-utils index-foreign-keys books.db
|
||||||
|
|
||||||
.. _cli_defaults_not_null:
|
.. _cli_defaults_not_null:
|
||||||
|
|
||||||
Setting defaults and not null constraints
|
Setting defaults and not null constraints
|
||||||
|
|
|
||||||
|
|
@ -419,6 +419,17 @@ Here's an example adding two foreign keys at once:
|
||||||
("dogs", "home_town_id", "towns", "id")
|
("dogs", "home_town_id", "towns", "id")
|
||||||
])
|
])
|
||||||
|
|
||||||
|
.. _python_api_index_foreign_keys:
|
||||||
|
|
||||||
|
Adding indexes for all foreign keys
|
||||||
|
-----------------------------------
|
||||||
|
|
||||||
|
If you want to ensure that every foreign key column in your database has a corresponding index, you can do so like this:
|
||||||
|
|
||||||
|
.. code-block:: python
|
||||||
|
|
||||||
|
db.index_foreign_keys()
|
||||||
|
|
||||||
.. _python_api_hash:
|
.. _python_api_hash:
|
||||||
|
|
||||||
Setting an ID based on the hash of the row contents
|
Setting an ID based on the hash of the row contents
|
||||||
|
|
|
||||||
|
|
@ -217,6 +217,20 @@ def add_foreign_key(path, table, column, other_table, other_column):
|
||||||
raise click.ClickException(e)
|
raise click.ClickException(e)
|
||||||
|
|
||||||
|
|
||||||
|
@cli.command(name="index-foreign-keys")
|
||||||
|
@click.argument(
|
||||||
|
"path",
|
||||||
|
type=click.Path(exists=True, file_okay=True, dir_okay=False, allow_dash=False),
|
||||||
|
required=True,
|
||||||
|
)
|
||||||
|
def index_foreign_keys(path):
|
||||||
|
"""
|
||||||
|
Ensure every foreign key column has an index on it.
|
||||||
|
"""
|
||||||
|
db = sqlite_utils.Database(path)
|
||||||
|
db.index_foreign_keys()
|
||||||
|
|
||||||
|
|
||||||
@cli.command(name="create-index")
|
@cli.command(name="create-index")
|
||||||
@click.argument(
|
@click.argument(
|
||||||
"path",
|
"path",
|
||||||
|
|
|
||||||
|
|
@ -315,6 +315,16 @@ class Database:
|
||||||
# can see the newly created foreign key.
|
# can see the newly created foreign key.
|
||||||
self.vacuum()
|
self.vacuum()
|
||||||
|
|
||||||
|
def index_foreign_keys(self):
|
||||||
|
for table_name in self.table_names():
|
||||||
|
table = self[table_name]
|
||||||
|
existing_indexes = {
|
||||||
|
i.columns[0] for i in table.indexes if len(i.columns) == 1
|
||||||
|
}
|
||||||
|
for fk in table.foreign_keys:
|
||||||
|
if fk.column not in existing_indexes:
|
||||||
|
table.create_index([fk.column])
|
||||||
|
|
||||||
def vacuum(self):
|
def vacuum(self):
|
||||||
self.conn.execute("VACUUM;")
|
self.conn.execute("VACUUM;")
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -310,6 +310,16 @@ def test_add_column_foreign_key(db_path):
|
||||||
assert "table 'bobcats' does not exist" in str(result.exception)
|
assert "table 'bobcats' does not exist" in str(result.exception)
|
||||||
|
|
||||||
|
|
||||||
|
def test_index_foreign_keys(db_path):
|
||||||
|
test_add_column_foreign_key(db_path)
|
||||||
|
db = Database(db_path)
|
||||||
|
assert [] == db["books"].indexes
|
||||||
|
result = CliRunner().invoke(cli.cli, ["index-foreign-keys", db_path])
|
||||||
|
assert [["author_id"], ["author_name_ref"]] == [
|
||||||
|
i.columns for i in db["books"].indexes
|
||||||
|
]
|
||||||
|
|
||||||
|
|
||||||
def test_enable_fts(db_path):
|
def test_enable_fts(db_path):
|
||||||
assert None == Database(db_path)["Gosh"].detect_fts()
|
assert None == Database(db_path)["Gosh"].detect_fts()
|
||||||
result = CliRunner().invoke(
|
result = CliRunner().invoke(
|
||||||
|
|
|
||||||
|
|
@ -361,6 +361,16 @@ def test_add_foreign_key_guess_table(fresh_db):
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
|
def test_index_foreign_keys(fresh_db):
|
||||||
|
test_add_foreign_key_guess_table(fresh_db)
|
||||||
|
assert [] == fresh_db["dogs"].indexes
|
||||||
|
fresh_db.index_foreign_keys()
|
||||||
|
assert [["breed_id"]] == [i.columns for i in fresh_db["dogs"].indexes]
|
||||||
|
# Calling it a second time should do nothing
|
||||||
|
fresh_db.index_foreign_keys()
|
||||||
|
assert [["breed_id"]] == [i.columns for i in fresh_db["dogs"].indexes]
|
||||||
|
|
||||||
|
|
||||||
@pytest.mark.parametrize(
|
@pytest.mark.parametrize(
|
||||||
"extra_data,expected_new_columns",
|
"extra_data,expected_new_columns",
|
||||||
[
|
[
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue