Skip to content

fix(flex): use sql_upgrade.php CLI mode in upgradeOpenEMR#666

Merged
kojiromike merged 1 commit intoopenemr:masterfrom
kojiromike:fix-drid-upgrade-cli
May 1, 2026
Merged

fix(flex): use sql_upgrade.php CLI mode in upgradeOpenEMR#666
kojiromike merged 1 commit intoopenemr:masterfrom
kojiromike:fix-drid-upgrade-cli

Conversation

@kojiromike
Copy link
Copy Markdown
Member

Summary

After openemr/openemr#11906 added native CLI support to sql_upgrade.php, the sed-based version-injection in upgradeOpenEMR() silently became a no-op, causing dev-reset-install-demodata (a.k.a. openemr-cmd drid) to leave the database at v5.0.0 with only the earliest upgrade scripts partially applied.

Mechanism

  1. demoData() calls upgradeOpenEMR 5.0.0.
  2. The function tries to inject the version with sed, looking for $form_old_version = $_POST['form_old_version'];.
  3. That pattern no longer exists in sql_upgrade.php. After fix(sql): Allow sql_upgrade to work on the cli openemr#11906, the assignment is $form_old_version = $cliFromVersion ?? ($_POST['form_old_version'] ?? '').
  4. The sed becomes a silent no-op. $form_old_version resolves to ''.
  5. The loop guard strcmp(\$version, '') < 0 is false for every upgrade file, so sql_upgrade.php starts processing from 2_6_0-to-2_6_1_upgrade.sql instead of from 5.0.0.
  6. 2_6_0-to-2_6_1_upgrade.sql:40 has an unguarded INSERT INTO categories_seq VALUES (0) — adding a second row alongside the demo dump's (26).
  7. 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. With two rows being set to the same id, the second hits a Duplicate entry PRIMARY KEY violation and the upgrade aborts.
  8. Database is left at v5.0.0 / v_database=206, with all post-5.0.0 schema changes missing.

User-visible symptoms after openemr-cmd drid

  • Login page renders without theme styling.
  • Patient dashboard fails with SQL Statement failed on preparation: ... lists_medication ... reporting_source_record_id ....
  • UUID_Service and Email_Service background services error.

Fix

Use the supported --from=VERSION CLI argument added in openemr/openemr#11906. The loop guard at sql_upgrade.php:379 is !empty(\$_POST['form_submit']) || \$cliFromVersion !== null, so passing --from= also satisfies the entry condition — no need to mutate the script source.

Scope

Only docker/openemr/flex/utilities/devtoolsLibrary.source is touched. The 8.0.0 / 8.1.0 / 8.1.1 image variants ship paired with their corresponding openemr release, where the older sed pattern still matches; they are out of scope here.

Test plan

  • bash -n on the patched file passes
  • Replaced the in-container /root/devtoolsLibrary.source via docker cp and re-ran openemr-cmd drid
  • Verified post-drid state: version = 8.1.1-dev / v_database=538 / v_acl=13, lists_medication.reporting_source_record_id present, categories_seq contains the expected single row
  • Reviewer to spot-check on a non-arm64 host if convenient

Copilot AI review requested due to automatic review settings May 1, 2026 14:59
@kojiromike
Copy link
Copy Markdown
Member Author

@Firehed you broke my spacebar

https://m.xkcd.com/1172/

Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR updates the Flex container devtools database-upgrade helper to use OpenEMR’s supported sql_upgrade.php CLI mode instead of mutating the script via sed, addressing a silent failure that could leave dev-reset-install-demodata databases partially upgraded.

Changes:

  • Replace sed-based non-interactive execution of sql_upgrade.php with a direct CLI invocation using --from=VERSION.
  • Remove temporary file creation/modification/cleanup logic now that native CLI mode is used.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

sed -i "1s@^@<?php \$_GET['site'] = 'default'; ?>@" /var/www/localhost/htdocs/openemr/sql_upgrade_temp.php
php -f /var/www/localhost/htdocs/openemr/sql_upgrade_temp.php
rm -f /var/www/localhost/htdocs/openemr/sql_upgrade_temp.php
php -f /var/www/localhost/htdocs/openemr/sql_upgrade.php -- --from="${1}"
Copy link

Copilot AI May 1, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The previous implementation always forced the upgrade to run against the default site by injecting $_GET['site'] = 'default';. This new CLI invocation no longer sets the site context at all, which can change behavior (or fail) in multisite-enabled containers. Consider passing the site explicitly in CLI mode (or otherwise setting the site to preserve the prior default-site behavior).

Copilot uses AI. Check for mistakes.
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
@kojiromike kojiromike force-pushed the fix-drid-upgrade-cli branch from b936b69 to 151d08a Compare May 1, 2026 15:54
@Firehed
Copy link
Copy Markdown
Contributor

Firehed commented May 1, 2026

@Firehed you broke my spacebar

https://m.xkcd.com/1172/

I regret nothing.

🙃

@kojiromike kojiromike merged commit 481147a into openemr:master May 1, 2026
31 checks passed
@kojiromike kojiromike deleted the fix-drid-upgrade-cli branch May 1, 2026 18:07
kojiromike added a commit that referenced this pull request May 4, 2026
## Summary

Adds a `schedule` trigger to `.github/workflows/build-323.yml` so the
default `openemr/openemr:flex` tag refreshes nightly at 2 AM UTC,
matching the existing pattern in `build-edge.yml`, `build-810.yml`, and
`build-811.yml`.

Without this, fixes that land on `master` (devops or upstream openemr)
can sit in the unpublished `openemr/openemr:flex` image for weeks until
someone manually dispatches the workflow. The motivating example: #666
merged on 2026-05-01, but the published `flex` digest is still the
2026-04-07 build, so `openemr-cmd dev-reset-install-demodata` continues
to fail in fresh containers.

When a future Alpine version takes over `is_default_flex: true`, this
trigger should move with it.

Closes #686

## Test plan

- [ ] Verify actionlint passes
- [ ] After merge, confirm a scheduled run kicks off the next 2 AM UTC
and publishes a fresh `openemr/openemr:flex` digest
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants