-
-
Notifications
You must be signed in to change notification settings - Fork 112
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Recreate indexes when calling transform when possible and raise an er… #634
Merged
simonw
merged 5 commits into
simonw:main
from
matdmiller:fix_index_being_dropped_but_not_recreated_on_alter
Nov 23, 2024
Merged
Changes from 1 commit
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
e31dbf3
recreate indexes when calling transform when possible and raise an er…
matdmiller c47a2a4
Clarifying comments
simonw 3041c5d
Test for does not have a CREATE INDEX case
simonw 4d2274c
Use new TransformError instead of AssertionError
simonw b525793
Docs for sqlite_utils.db.TransformError
simonw File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -1967,6 +1967,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, ( | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The problem with I don't personally like that feature, but I think we should switch to a different exception just in case. |
||
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( | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I changed this to 'index.origin != "pk"` instead.