Skip to content

Conversation

@matdmiller
Copy link
Contributor

Retain indexes when calling transform.

Same as PR to sqlite-utils: simonw/sqlite-utils#634

From sqlite-utils PR:
Recreate indexes when calling transform when possible and raise an error when they cannot be retained automatically.

This resolves my initial problem: 'After creating a table with an index, if you add a foreign key constraint to the table it drops existing non-related indexes.'

GH Issue: simonw/sqlite-utils#633

My explanation, thoughts and reasoning:

I think it is best to try and preserve existing indexes and fail loudly if they are unable to be recreated automatically. At the very least I believe you should be warned if they are not going to be recreated.

Below is my proposed solution with code:

If a table is kept it will automatically drop the original index. To be able to create the index on the new table using the original CREATE INDEX statement the original index must be dropped. I think it's a reasonable default for the 'kept' table to lose its indexes. I suspect the most common reason to keep the original table is for 'backup' purposes. It would also be difficult to automatically create updated CREATE INDEX statements for the kept renamed table where the statements are more complicated.

If a column exists in the original index but not in the new table, you should have to manually recreate the index as desired. In complicated CREATE INDEX statements it is difficult to automatically replace or remove columns correctly. Failing before any operations have been completed is desired because it allows you to know there will be a problem prior to the SQL statements being executed allowing you to collect the required information required to manually update the indexes prior to any actions taking place as well as not leaving your database in a potentially invalid state.

@matdmiller
Copy link
Contributor Author

@jph00 PR Submitted as requested.

@jph00 jph00 added the enhancement New feature or request label Jul 22, 2024
@jph00 jph00 merged commit 5855737 into AnswerDotAI:main Jul 22, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants