Skip to content

Commit 151d08a

Browse files
committed
fix(flex): use sql_upgrade.php CLI mode in upgradeOpenEMR
The previous implementation rewrote sql_upgrade.php with sed to inject the source version. After openemr/openemr#11906 added native CLI support, the target sed pattern (`$form_old_version = $_POST['form_old_version'];`) no longer exists in sql_upgrade.php — the assignment is now `$form_old_version = $cliFromVersion ?? ($_POST['form_old_version'] ?? '')`. The sed becomes a silent no-op, leaving `$form_old_version = ''`. The loop guard `strcmp($version, '') < 0` is then false for every upgrade file, so sql_upgrade.php starts processing from `2_6_0-to-2_6_1_upgrade.sql` instead of the requested 5.0.0. That file contains an unguarded `INSERT INTO categories_seq VALUES (0)` which adds a second row alongside the demo dump's `(26)`. Several files later, `5_0_0-to-5_0_1_upgrade.sql:407` runs `UPDATE categories_seq SET id = (SELECT MAX(id) FROM categories)` without a WHERE clause; with two rows being set to the same id, the second hits a PRIMARY KEY violation and the upgrade aborts. The database is left at v5.0.0 with only the earliest upgrade scripts partially applied — visible to users as broken login CSS, missing columns (`lists_medication.reporting_source_record_id`), and erroring background services after `openemr-cmd drid`. Switch to the supported `--from=VERSION` CLI argument, which is honored by the loop guard at `sql_upgrade.php:379` (`!empty($_POST['form_submit']) || $cliFromVersion !== null`) and removes the need to mutate the script source. Validated end-to-end: `openemr-cmd drid` now leaves the database at v8.1.1-dev / v_database=538, with `categories_seq` containing the expected single row and `lists_medication.reporting_source_record_id` present. Assisted-by: Claude Code
1 parent 28e8caf commit 151d08a

1 file changed

Lines changed: 2 additions & 14 deletions

File tree

docker/openemr/flex/utilities/devtoolsLibrary.source

Lines changed: 2 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -207,26 +207,14 @@ demoData() {
207207
# ============================================================================
208208

209209
# Upgrades OpenEMR database from a specified version to the current version.
210-
# This function runs OpenEMR's sql_upgrade.php script programmatically
211-
# by modifying it to run non-interactively.
210+
# Invokes sql_upgrade.php in CLI mode (added in openemr/openemr#11906).
212211
#
213212
# Parameters:
214213
# $1: Original OpenEMR version (e.g., "5.0.0", "6.0.0")
215214
#
216-
# Process:
217-
# 1. Creates a temporary version of sql_upgrade.php
218-
# 2. Modifies it to run non-interactively (auto-submit form)
219-
# 3. Sets the source version
220-
# 4. Executes the upgrade script
221-
# 5. Cleans up temporary file
222-
#
223215
# Must call prepareVariables() before calling this function.
224216
upgradeOpenEMR() {
225-
sed -e "s@!empty(\$_POST\['form_submit'\])@true@" < /var/www/localhost/htdocs/openemr/sql_upgrade.php > /var/www/localhost/htdocs/openemr/sql_upgrade_temp.php
226-
sed -i "s@\$form_old_version = \$_POST\['form_old_version'\];@\$form_old_version = '${1}';@" /var/www/localhost/htdocs/openemr/sql_upgrade_temp.php
227-
sed -i "1s@^@<?php \$_GET['site'] = 'default'; ?>@" /var/www/localhost/htdocs/openemr/sql_upgrade_temp.php
228-
php -f /var/www/localhost/htdocs/openemr/sql_upgrade_temp.php
229-
rm -f /var/www/localhost/htdocs/openemr/sql_upgrade_temp.php
217+
php -f /var/www/localhost/htdocs/openemr/sql_upgrade.php -- --from="${1}"
230218
}
231219

232220
# ============================================================================

0 commit comments

Comments
 (0)