mirror of
https://github.com/simonw/sqlite-utils.git
synced 2026-07-23 09:24:31 +02:00
parent
0fe3b38290
commit
e8f887ef4a
6 changed files with 64 additions and 0 deletions
|
|
@ -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",
|
||||
|
|
|
|||
|
|
@ -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;")
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue