feat(sqlite): no_tx
migration support
#4015
Open
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.
Context, problem statement and description
SQLite includes several SQL statements that are useful during migrations but must be executed outside of a transaction to take effect, such as
PRAGMA foreign_keys = ON|OFF
orVACUUM
. Additionally, advanced migrations may want more precise control over how statements are grouped into transactions or savepoints to achieve the desired atomicity for different parts of the migration.While SQLx already supports marking migrations to run outside explicit transactions through a
-- no-transaction
comment, this feature is currently only implemented forPgConnection
'sMigrate
, leaving SQLite and MySQL without this capability. Although it's possible to work around this limitation by implementing custom migration logic instead of executingMigrator#run
, this comes at the cost of significantly reduced developer ergonomics: code that relies on the default migration logic, such as#[sqlx::test]
orcargo sqlx database setup
, won't support these migrations.These changes extend
SqliteConnection
'sMigrate
implementation to supportno_tx
migrations in the same way as PostgreSQL, addressing this feature gap. I also considered implementing the same functionality for MySQL, but since I haven't found a practical use case for it yet, and every non-transaction-friendly statement I could think about in MySQL triggers implicit commits anyway, I determined it wasn't necessary at this time and could be considered an overreach.Does your PR solve an issue?
To my knowledge, this PR doesn't directly address any previously reported issue in this repository. I believe I may be the first person that happens to both attempt using SQLx with SQLite migrations containing "advanced" SQL statements and have the motivation to improve things at the source :)
Is this a breaking change?
Technically yes, as SQLite migrations that begin with a
-- no-transaction
comment will now be executed outside of a transaction, which represents a significant behavioral change. However, I doubt this will cause substantial disruption in practice for several reasons:-- no-transaction
comment without intending to disable the automatic migration transaction. Since this functionality didn't work previously, users likely either settled for removing the comment or implementing alternative solutions, as mentioned above. This change could therefore be considered a bug fix, and Hyrum's law is less likely to apply to behaviors not useful to achieve any outcome.