mirror of
https://github.com/simonw/sqlite-utils.git
synced 2026-07-23 01:14:31 +02:00
Recreate indexes when calling transform when possible (#634)
* Recreate indexes when calling transform when possible and raise an error when they cannot be retained automatically * Docs for sqlite_utils.db.TransformError Co-authored-by: Simon Willison <swillison@gmail.com>
This commit is contained in:
parent
cbddfb28f9
commit
42230709f7
3 changed files with 153 additions and 1 deletions
|
|
@ -156,6 +156,10 @@ XIndexColumn = namedtuple(
|
|||
Trigger = namedtuple("Trigger", ("name", "table", "sql"))
|
||||
|
||||
|
||||
class TransformError(Exception):
|
||||
pass
|
||||
|
||||
|
||||
ForeignKeyIndicator = Union[
|
||||
str,
|
||||
ForeignKey,
|
||||
|
|
@ -1972,6 +1976,30 @@ class Table(Queryable):
|
|||
sqls.append(
|
||||
"ALTER TABLE [{}] RENAME TO [{}];".format(new_table_name, self.name)
|
||||
)
|
||||
# Re-add existing indexes
|
||||
for index in self.indexes:
|
||||
if index.origin != "pk":
|
||||
index_sql = self.db.execute(
|
||||
"""SELECT sql FROM sqlite_master WHERE type = 'index' AND name = :index_name;""",
|
||||
{"index_name": index.name},
|
||||
).fetchall()[0][0]
|
||||
if index_sql is None:
|
||||
raise TransformError(
|
||||
f"Index '{index.name}' on table '{self.name}' does not have a "
|
||||
"CREATE INDEX statement. You must manually drop this index prior to running this "
|
||||
"transformation and manually recreate the new index after running this transformation."
|
||||
)
|
||||
if keep_table:
|
||||
sqls.append(f"DROP INDEX IF EXISTS [{index.name}];")
|
||||
for col in index.columns:
|
||||
if col in rename.keys() or col in drop:
|
||||
raise TransformError(
|
||||
f"Index '{index.name}' column '{col}' is not in updated table '{self.name}'. "
|
||||
f"You must manually drop this index prior to running this transformation "
|
||||
f"and manually recreate the new index after running this transformation. "
|
||||
f"The original index sql statement is: `{index_sql}`. No changes have been applied to this table."
|
||||
)
|
||||
sqls.append(index_sql)
|
||||
return sqls
|
||||
|
||||
def extract(
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue