From 5659c262deee23125df43834121cbb9df58077c8 Mon Sep 17 00:00:00 2001 From: Mat Miller Date: Sun, 21 Jul 2024 22:26:48 -0500 Subject: [PATCH] retain indexes in transform --- sqlite_minutils/db.py | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/sqlite_minutils/db.py b/sqlite_minutils/db.py index 44f17e9..caa9b04 100644 --- a/sqlite_minutils/db.py +++ b/sqlite_minutils/db.py @@ -1871,6 +1871,28 @@ def transform_sql( sqls.append( "ALTER TABLE [{}] RENAME TO [{}];".format(new_table_name, self.name) ) + # Re-add existing indexes + for index in self.indexes: + if index.origin not in ("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] + assert index_sql is not None, ( + f"Index '{index}' 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: + assert col not in rename.keys() and col not in drop, ( + 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(