Transactional Migration Rollback Safety Configuration in CodeIgniter 4
0 reputation · 28 Jun 2025, 23:08 UTC
0 reputation · 28 Jun 2025, 23:08 UTC
The goal is to guarantee that a failed migration leaves the database unchanged, even when the migration contains schema‑altering statements such as CREATE TABLE or ALTER TABLE.
In MySQL, DDL commands cause an implicit commit that breaks the transaction wrapper CodeIgniter applies by default, so a rollback may not undo those changes.
It is unclear whether the framework provides a configuration option to disable automatic commits for DDL within migrations or to wrap such statements in savepoints on DBMS that support them.
Is there a setting to turn off implicit commits for DDL inside migration transactions?
Can developers enable savepoint‑based wrapping for transactional DDL on MySQL or other engines?
Does CodeIgniter expose a driver‑specific flag to make rollback safe regardless of the underlying database’s DDL behavior?
31775 reputation · 29 Jun 2025, 10:14 UTC
The CodeIgniter 4 Migration class wraps up() and down() in startTrans()/commit()/rollback(), but does not intercept DBMS‑level implicit commits from DDL statements such as CREATE TABLE or ALTER TABLE.
On MySQL, any open transaction is implicitly committed before a DDL statement runs. Consequently, a framework rollback cannot undo schema changes made by that DDL; the modification persists after rollback.
On databases with transactional DDL—such as PostgreSQL or SQLite—ROLLBACK within an active transaction does undo schema changes, because the DDL is part of the transaction.
CodeIgniter 4 does not expose a configuration option, driver‑specific flag, or migration‑level setting to disable implicit DDL commits or to wrap DDL in savepoints globally.
Developers needing transactional DDL safety on MySQL must either use a database engine with transactional DDL for schema‑changing migrations, or employ raw SAVEPOINT/ROLLBACK TO SAVEPOINT via $db->query(), which bypasses CodeIgniter’s transaction management and may introduce portability and connection‑state risks.
If you are working with MySQL and need to verify whether a migration’s rollback will preserve or discard schema changes, please specify the database engine version and connection settings so a targeted diagnostic can be provided.
Use comments to ask for clarification. Post a solution as an answer.
No question comments on this page.