sqlite-utils icon indicating copy to clipboard operation
sqlite-utils copied to clipboard

Allow mytable.create_index() to ignore an already existing index

Open tobych opened this issue 1 year ago • 0 comments

While I'm working on a migration, I like to keep my code idempotent.

I love that most operations offer an ignore argument.

I'd like one for create_index() too.

I can submit a PR but I'd like some discussion first.

Meanwhile I'm using this motif:

from contextlib import suppress

from sqlite_utils.db import OperationalError

with suppress(OperationalError):
    mytable.create_index({'mycolumn'}, unique=True)

I could instead check to see whether the index already exists, but that's not Pythonic.

I should be checking the error message there really. More fine-grained exception classes would help here, so I could do this:

with suppress(IndexAlreadyExists):
    mytable.create_index({'mycolumn'}, unique=True)

Where IndexAlreadyExists is a subclass of OperationalError. Separate issue though.

tobych avatar Apr 27 '24 02:04 tobych