mirror of
https://github.com/simonw/sqlite-utils.git
synced 2026-07-23 01:14:31 +02:00
parent
d302835d57
commit
7a52214624
8 changed files with 137 additions and 1 deletions
|
|
@ -692,6 +692,34 @@ def create_index(
|
|||
)
|
||||
|
||||
|
||||
@cli.command(name="drop-index")
|
||||
@click.argument(
|
||||
"path",
|
||||
type=click.Path(exists=True, file_okay=True, dir_okay=False, allow_dash=False),
|
||||
required=True,
|
||||
)
|
||||
@click.argument("table")
|
||||
@click.argument("index")
|
||||
@click.option("--ignore", help="Ignore if index does not exist", is_flag=True)
|
||||
@load_extension_option
|
||||
def drop_index(path, table, index, ignore, load_extension):
|
||||
"""
|
||||
Drop an index by index name from the specified table
|
||||
|
||||
Example:
|
||||
|
||||
\b
|
||||
sqlite-utils drop-index chickens.db chickens idx_chickens_name
|
||||
"""
|
||||
db = sqlite_utils.Database(path)
|
||||
_register_db_for_cleanup(db)
|
||||
_load_extensions(db, load_extension)
|
||||
try:
|
||||
db.table(table).drop_index(index, ignore=ignore)
|
||||
except OperationalError as ex:
|
||||
raise click.ClickException(str(ex))
|
||||
|
||||
|
||||
@cli.command(name="enable-fts")
|
||||
@click.argument(
|
||||
"path",
|
||||
|
|
|
|||
|
|
@ -3080,6 +3080,22 @@ class Table(Queryable):
|
|||
self.db.analyze(created_index_name)
|
||||
return self
|
||||
|
||||
def drop_index(self, index_name: str, ignore: bool = False):
|
||||
"""
|
||||
Drop an index on this table.
|
||||
|
||||
:param index_name: Name of the index to drop
|
||||
:param ignore: Set to ``True`` to ignore the error if the index does not exist
|
||||
"""
|
||||
if index_name not in {index.name for index in self.indexes}:
|
||||
if ignore:
|
||||
return self
|
||||
raise OperationalError(
|
||||
"No index named {} on table {}".format(index_name, self.name)
|
||||
)
|
||||
self.db.execute("DROP INDEX {}".format(quote_identifier(index_name)))
|
||||
return self
|
||||
|
||||
def add_column(
|
||||
self,
|
||||
col_name: str,
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue