sqlite-utils index-foreign-keys / db.index_foreign_keys()

Closes #33
This commit is contained in:
Simon Willison 2019-06-30 16:50:54 -07:00 committed by GitHub
commit e8f887ef4a
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 64 additions and 0 deletions

View file

@ -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.
.. _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:
Setting defaults and not null constraints

View file

@ -419,6 +419,17 @@ Here's an example adding two foreign keys at once:
("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:
Setting an ID based on the hash of the row contents

View file

@ -217,6 +217,20 @@ def add_foreign_key(path, table, column, other_table, other_column):
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")
@click.argument(
"path",

View file

@ -315,6 +315,16 @@ class Database:
# can see the newly created foreign key.
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):
self.conn.execute("VACUUM;")

View file

@ -310,6 +310,16 @@ def test_add_column_foreign_key(db_path):
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):
assert None == Database(db_path)["Gosh"].detect_fts()
result = CliRunner().invoke(

View file

@ -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(
"extra_data,expected_new_columns",
[