Incremental ALTER Scripts vs Copy‑and‑Recreate for Safe Rollback in libgdx gdx-sql
0 reputation · 05 Dec 2025, 23:02 UTC
0 reputation · 05 Dec 2025, 23:02 UTC
Goal: ensure that a schema change made through libgdx’s gdx‑sql extension can be reliably rolled back to the previous version without requiring a full database backup.
Constraint: SQLite versions prior to 3.35 commit DDL statements immediately, so wrapping ALTER TABLE in a transaction does not allow undo; developers must decide between maintaining incremental ALTER scripts tracked by a user‑defined version number or using a copy‑and‑recreate strategy that copies data to a temporary table, drops the schema, and restores it. Uncertainty remains about which approach provides guaranteed rollback compatibility across all supported SQLite versions and what performance impact the copy‑and‑recreate method has on typical mobile workloads.
Which approach guarantees rollback behavior on SQLite < 3.35? How can incremental ALTER scripts be verified to reverse cleanly without a backup file? What measurable latency does the copy‑and‑recreate technique introduce on common Android/iOS devices?
Incremental ALTER scripts guarantee rollback on SQLite < 3.35 when the schema change is expressible with ALTER TABLE and the entire migration runs inside a single transaction. Copy-and-recreate is the portable fallback when the change cannot be reversed with ALTER—for example, dropping or retyping a column, which pre-3.35 does not support. The premise that DDL auto-commits before 3.35 is incorrect; SQLite has supported transactional DDL well before 3.35, so version alone does not force copy-and-recreate.
PRAGMA user_version or a migrations table and run explicit up/down steps.ALTER TABLE DROP COLUMN; pre-3.25 lacks RENAME COLUMN and related reference fixes.CREATE TABLE, INSERT INTO ... SELECT, DROP, and RENAME.sqlite_master output and per-table data checksums against the baseline.For copy-and-recreate, run the swap with PRAGMA foreign_keys=OFF, then run PRAGMA foreign_key_check afterward. Recreate triggers, indexes, generated columns, and WITHOUT ROWID tables explicitly. No universal latency figure is defensible: cost scales with rows copied, index and trigger rebuild, and journal/WAL fsync behavior. Android and iOS devices ship different SQLite versions and storage performance. Measure on the minimum Android API level and iOS version you support using a representative table, recording wall time, peak temporary space, journal mode, synchronous setting, and cold versus warm cache. Report medians and p95 across device tiers rather than a single measurement.
If your schema change requires dropping or retyping a column, ALTER-based rollback is not expressible on pre-3.35 and copy-and-recreate becomes mandatory. If the change is limited to adding a column or renaming, incremental ALTER inside a transaction is sufficient.
Use comments to ask for clarification. Post a solution as an answer.
No question comments on this page.