Skip to content

fix: preflight schema check prevents duplicate column errors on upgrade#1183

Merged
bcordis merged 2 commits intodevelopmentfrom
fix/idempotent-migrations
Mar 19, 2026
Merged

fix: preflight schema check prevents duplicate column errors on upgrade#1183
bcordis merged 2 commits intodevelopmentfrom
fix/idempotent-migrations

Conversation

@bcordis
Copy link
Member

@bcordis bcordis commented Mar 19, 2026

Summary

Fixes installation failure when upgrading between versions caused by MySQL ALTER TABLE ADD COLUMN failing on columns that already exist from a previous partial upgrade attempt.

Problem

MySQL's ALTER TABLE ADD COLUMN is not idempotent — it throws "Duplicate column" if the column already exists. Joomla's schema updater aborts on the first SQL error and marks the install as failed, but the component files have already been copied. This leaves the site in a broken state: new PHP code but old database schema.

This happens when:

  • A previous upgrade attempt failed partway through (columns partially added)
  • A database was imported/restored from a different version
  • Joomla retries the schema update after a partial failure

Fix

New ensureSchemaReady() method runs in preflight() before Joomla copies files or runs the schema updater:

  1. Reads the currently installed install.mysql.utf8.sql (still on disk from the old version)
  2. Parses CREATE TABLE statements to extract expected columns per table
  3. Compares against the live database
  4. Drops any columns that exist in the DB but are NOT in the install SQL — these are leftovers from a failed partial upgrade
  5. DB is now clean for the installed version → Joomla's migration SQL runs without errors

Why this approach

  • No hardcoded column lists — the install SQL file IS the schema reference for the installed version
  • Self-maintaining — each version's install SQL automatically defines what's expected
  • Works for any version combination — 10.0.1→10.2.0, 10.1.0→10.3.0, retry after failure, etc.
  • Also fixes DB import/restore scenarios where the schema doesn't match expectations
  • Preserves dbfix tool<schemas> stays in XML, migration SQL files unchanged

Flow

preflight() → parse OLD install.sql → diff vs live DB → drop extras
    ↓
Joomla copies new files (including new install.sql)
    ↓
Joomla schema updater runs migration SQL → ADD COLUMN succeeds
    ↓
postflight() → scripture migration, guided tours, etc.

Test plan

  • Fresh install on empty database — no errors
  • Upgrade 10.0.1 → 10.2.x — no duplicate column errors
  • Simulate partial upgrade failure (add some columns manually) → retry upgrade → completes cleanly
  • Import 10.2.x database into 10.0.x install → upgrade → no errors
  • Joomla dbfix tool still shows correct schema status
  • All 538 PHPUnit tests pass
  • PHP syntax clean

🤖 Generated with Claude Code

… upgrade

When upgrading from 10.0.x to 10.2.x, MySQL ALTER TABLE ADD COLUMN
statements fail if columns already exist (partial upgrade, DB restore,
or retry after failed install). The new ensureSchemaReady() method runs
in preflight() before Joomla's schema updater and pre-adds any missing
columns, making the migration SQL files safe to re-run.

Covers all columns added in 10.1.0 and 10.3.0 migrations across 14
tables: audit columns, checked_out, location_id, bible_version,
image, platform stats, podcast iTunes fields, and org_name.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
@bcordis bcordis force-pushed the fix/idempotent-migrations branch from ce4ccac to 430d873 Compare March 19, 2026 13:53
Parse the currently installed install.mysql.utf8.sql to determine what
columns each table should have at the installed version. Compare against
the live DB and drop any extra columns that are leftovers from a failed
partial upgrade. This restores the DB to a clean state so Joomla's
migration SQL (ALTER TABLE ADD COLUMN) won't fail on duplicates.

No hardcoded column lists — the install SQL file IS the schema reference
for the installed version. Works for any version-to-version upgrade path.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
@bcordis bcordis force-pushed the fix/idempotent-migrations branch from 430d873 to b4b0f98 Compare March 19, 2026 14:00
@bcordis bcordis merged commit 929fb6e into development Mar 19, 2026
6 checks passed
@bcordis bcordis deleted the fix/idempotent-migrations branch March 19, 2026 14:02
bcordis added a commit that referenced this pull request Mar 19, 2026
…de (#1183)

* fix: add preflight schema check to prevent duplicate column errors on upgrade

When upgrading from 10.0.x to 10.2.x, MySQL ALTER TABLE ADD COLUMN
statements fail if columns already exist (partial upgrade, DB restore,
or retry after failed install). The new ensureSchemaReady() method runs
in preflight() before Joomla's schema updater and pre-adds any missing
columns, making the migration SQL files safe to re-run.

Covers all columns added in 10.1.0 and 10.3.0 migrations across 14
tables: audit columns, checked_out, location_id, bible_version,
image, platform stats, podcast iTunes fields, and org_name.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>

* fix: preflight schema check using install SQL as version reference

Parse the currently installed install.mysql.utf8.sql to determine what
columns each table should have at the installed version. Compare against
the live DB and drop any extra columns that are leftovers from a failed
partial upgrade. This restores the DB to a clean state so Joomla's
migration SQL (ALTER TABLE ADD COLUMN) won't fail on duplicates.

No hardcoded column lists — the install SQL file IS the schema reference
for the installed version. Works for any version-to-version upgrade path.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>

---------

Co-authored-by: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant