[PR #16641] [MERGED] fix: Improve path resolution and Alembic configuration (also a fix) #24193

Closed
opened 2026-04-20 05:16:51 -05:00 by GiteaMirror · 0 comments
Owner

📋 Pull Request Information

Original PR: https://github.com/open-webui/open-webui/pull/16641
Author: @silentoplayz
Created: 8/15/2025
Status: Merged
Merged: 8/15/2025
Merged by: @tjbck

Base: devHead: fix-alembic-migration-fails


📝 Commits (1)

  • b810868 fix: Alembic CLI commands from failing

📊 Changes

2 files changed (+10 additions, -7 deletions)

View changed files

📝 backend/open_webui/alembic.ini (+1 -1)
📝 backend/open_webui/env.py (+9 -6)

📄 Description

Pull Request Checklist

Note to first-time contributors: Please open a discussion post in Discussions and describe your changes before submitting a pull request.

  • Target branch: Please verify that the pull request targets the dev branch.
  • Description: Provide a concise description of the changes made in this pull request.
  • Changelog: Ensure a changelog entry following the format of Keep a Changelog is added at the bottom of the PR description.
  • Documentation: Have you updated relevant documentation Open WebUI Docs, or other documentation sources?
  • Dependencies: Are there any new dependencies? Have you updated the dependency versions in the documentation?
  • Testing: Have you written and run sufficient tests to validate the changes?
  • Code review: Have you performed a self-review of your code, addressing any coding standard issues and ensuring adherence to the project's coding standards?
  • Prefix: To clearly categorize this pull request, prefix the pull request title using one of the following:
    • BREAKING CHANGE: Significant changes that may affect compatibility
    • build: Changes that affect the build system or external dependencies
    • ci: Changes to our continuous integration processes or workflows
    • chore: Refactor, cleanup, or other non-functional code changes
    • docs: Documentation update or addition
    • feat: Introduces a new feature or enhancement to the codebase
    • fix: Bug fix or error correction
    • i18n: Internationalization or localization changes
    • perf: Performance improvement
    • refactor: Code restructuring for better maintainability, readability, or scalability
    • style: Changes that do not affect the meaning of the code (white space, formatting, missing semi-colons, etc.)
    • test: Adding missing tests or correcting existing tests
    • WIP: Work in progress, a temporary label for incomplete or ongoing work

Changelog Entry

Description

This pull request updates the path handling in the env.py file to use Path().resolve() for canonical path resolution, which helps in correctly determining the base directories. It also updates the alembic.ini configuration for prepend_sys_path.

Changed

  • In backend/open_webui/env.py:
    • Changed the definition of OPEN_WEBUI_DIR to _this_file_path_resolved.parent, where _this_file_path_resolved is Path(__file__).resolve(), ensuring canonical path resolution.
    • Updated BACKEND_DIR to be the parent of the newly defined OPEN_WEBUII_DIR.
    • Updated BASE_DIR to be the parent of the newly defined BACKEND_DIR.
    • Added comments explaining the purpose of _this_file_path_resolved, OPEN_WEBUI_DIR, BACKEND_DIR, and BASE_DIR.
  • In backend/open_webui/alembic.ini:
    • Updated script_location in the [alembic] section to migrations.
    • Changed prepend_sys_path from . to .. in the [alembic] section.

Removed

  • In backend/open_webui/env.py:
    • Removed the previous definitions of OPEN_WEBUI_DIR, BACKEND_DIR, and BASE_DIR that relied directly on Path(__file__).parent without resolve().
    • Removed print(OPEN_WEBUI_DIR), print(BACKEND_DIR), and print(BASE_DIR) statements.

Fixed

  • Improved the robustness and correctness of directory path resolution within the application by utilizing Path().resolve() to handle relative path components (.. or .).
  • The following error when attempting to run Alembic related commands from backend/open_webui:
➜  open_webui git:(dev) ✗ alembic history           
Traceback (most recent call last):
  File "/home/g30/.pyenv/versions/3.11.13/bin/alembic", line 8, in <module>
    sys.exit(main())
             ^^^^^^
  File "/home/g30/.pyenv/versions/3.11.13/lib/python3.11/site-packages/alembic/config.py", line 636, in main
    CommandLine(prog=prog).main(argv=argv)
  File "/home/g30/.pyenv/versions/3.11.13/lib/python3.11/site-packages/alembic/config.py", line 626, in main
    self.run_cmd(cfg, options)
  File "/home/g30/.pyenv/versions/3.11.13/lib/python3.11/site-packages/alembic/config.py", line 603, in run_cmd
    fn(
  File "/home/g30/.pyenv/versions/3.11.13/lib/python3.11/site-packages/alembic/command.py", line 548, in history
    _display_history(config, script, base, head)
  File "/home/g30/.pyenv/versions/3.11.13/lib/python3.11/site-packages/alembic/command.py", line 517, in _display_history
    for sc in script.walk_revisions(
  File "/home/g30/.pyenv/versions/3.11.13/lib/python3.11/site-packages/alembic/script/base.py", line 304, in walk_revisions
    for rev in self.revision_map.iterate_revisions(
  File "/home/g30/.pyenv/versions/3.11.13/lib/python3.11/site-packages/alembic/script/revision.py", line 814, in iterate_revisions
    revisions, heads = fn(
                       ^^^
  File "/home/g30/.pyenv/versions/3.11.13/lib/python3.11/site-packages/alembic/script/revision.py", line 1447, in _collect_upgrade_revisions
    for rev in self._parse_upgrade_target(
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/g30/.pyenv/versions/3.11.13/lib/python3.11/site-packages/alembic/script/revision.py", line 1234, in _parse_upgrade_target
    return self.get_revisions(target)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/g30/.pyenv/versions/3.11.13/lib/python3.11/site-packages/alembic/script/revision.py", line 544, in get_revisions
    resolved_id, branch_label = self._resolve_revision_number(id_)
                                ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/g30/.pyenv/versions/3.11.13/lib/python3.11/site-packages/alembic/script/revision.py", line 768, in _resolve_revision_number
    self._revision_map
  File "/home/g30/.pyenv/versions/3.11.13/lib/python3.11/site-packages/sqlalchemy/util/langhelpers.py", line 1145, in __get__
    obj.__dict__[self.__name__] = result = self.fget(obj)
                                           ^^^^^^^^^^^^^^
  File "/home/g30/.pyenv/versions/3.11.13/lib/python3.11/site-packages/alembic/script/revision.py", line 209, in _revision_map
    for revision in self._generator():
  File "/home/g30/.pyenv/versions/3.11.13/lib/python3.11/site-packages/alembic/script/base.py", line 154, in _load_revisions
    script = Script._from_filename(self, dir_name, filename)
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/g30/.pyenv/versions/3.11.13/lib/python3.11/site-packages/alembic/script/base.py", line 1048, in _from_filename
    module = util.load_python_file(dir_, filename)
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/g30/.pyenv/versions/3.11.13/lib/python3.11/site-packages/alembic/util/pyfiles.py", line 95, in load_python_file
    module = load_module_py(module_id, path)
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/g30/.pyenv/versions/3.11.13/lib/python3.11/site-packages/alembic/util/pyfiles.py", line 113, in load_module_py
    spec.loader.exec_module(module)  # type: ignore
    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "<frozen importlib._bootstrap_external>", line 940, in exec_module
  File "<frozen importlib._bootstrap>", line 241, in _call_with_frames_removed
  File "/home/g30/docker-projects/open-webui-dev/backend/open_webui/migrations/versions/7e5b5dc7342b_init.py", line 14, in <module>
    import open_webui.internal.db
ModuleNotFoundError: No module named 'open_webui'

Result AFTER the fix:

➜  open_webui git:(dev) ✗ alembic history
9f0c9cd09105 -> d31026856c01 (head), Update folder table data
3781e22d8b01 -> 9f0c9cd09105, Add note table
7826ab40b532 -> 3781e22d8b01, Update message & channel tables
57c599a3cb57 -> 7826ab40b532, Update file table
922e7a387820 -> 57c599a3cb57, Add channel table
4ace53fd72c8 -> 922e7a387820, Add group table
af906e964978 -> 4ace53fd72c8, Update folder table and change DateTime to BigInteger for timestamp fields
c29facfe716b -> af906e964978, Add feedback table
c69f45358db4 -> c29facfe716b, Update file table path
3ab32c4b8f59 -> c69f45358db4, Add folder table
1af9b942657b -> 3ab32c4b8f59, Update tags
242a2047eae0 -> 1af9b942657b, Migrate tags
6a39f3d8e55c -> 242a2047eae0, Update chat table
c0fbf31ca0db -> 6a39f3d8e55c, Add knowledge table
ca81bd47c050 -> c0fbf31ca0db, Update file table
7e5b5dc7342b -> ca81bd47c050, Add config table
<base> -> 7e5b5dc7342b, init

Breaking Changes

  • BREAKING CHANGE: The underlying mechanism for resolving core application directory paths (OPEN_WEBUI_DIR, BACKEND_DIR, BASE_DIR) has changed from direct .parent access to canonical resolution using Path().resolve(). This might affect any external scripts or configurations that rely on specific non-canonical path interpretations.

Additional Information

  • This change aims to provide more reliable and consistent pathing for the application, especially in environments where __file__ might contain relative components or symlinks.
  • The alembic.ini adjustment for prepend_sys_path ensures that the alembic environment can correctly locate necessary modules by adjusting the Python system path.

Screenshots or Videos

image

Contributor License Agreement

By submitting this pull request, I confirm that I have read and fully agree to the Contributor License Agreement (CLA), and I am providing my contributions under its terms.


🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.

## 📋 Pull Request Information **Original PR:** https://github.com/open-webui/open-webui/pull/16641 **Author:** [@silentoplayz](https://github.com/silentoplayz) **Created:** 8/15/2025 **Status:** ✅ Merged **Merged:** 8/15/2025 **Merged by:** [@tjbck](https://github.com/tjbck) **Base:** `dev` ← **Head:** `fix-alembic-migration-fails` --- ### 📝 Commits (1) - [`b810868`](https://github.com/open-webui/open-webui/commit/b810868239cb506f1c7d244ffcbe4bf176d588f2) fix: Alembic CLI commands from failing ### 📊 Changes **2 files changed** (+10 additions, -7 deletions) <details> <summary>View changed files</summary> 📝 `backend/open_webui/alembic.ini` (+1 -1) 📝 `backend/open_webui/env.py` (+9 -6) </details> ### 📄 Description # Pull Request Checklist ### Note to first-time contributors: Please open a discussion post in [Discussions](https://github.com/open-webui/open-webui/discussions) and describe your changes before submitting a pull request. - [X] **Target branch:** Please verify that the pull request targets the `dev` branch. - [X] **Description:** Provide a concise description of the changes made in this pull request. - [X] **Changelog:** Ensure a changelog entry following the format of [Keep a Changelog](https://keepachangelog.com/) is added at the bottom of the PR description. - [X] **Documentation:** Have you updated relevant documentation [Open WebUI Docs](https://github.com/open-webui/docs), or other documentation sources? - [X] **Dependencies:** Are there any new dependencies? Have you updated the dependency versions in the documentation? - [X] **Testing:** Have you written and run sufficient tests to validate the changes? - [X] **Code review:** Have you performed a self-review of your code, addressing any coding standard issues and ensuring adherence to the project's coding standards? - [X] **Prefix:** To clearly categorize this pull request, prefix the pull request title using one of the following: - **BREAKING CHANGE**: Significant changes that may affect compatibility - **build**: Changes that affect the build system or external dependencies - **ci**: Changes to our continuous integration processes or workflows - **chore**: Refactor, cleanup, or other non-functional code changes - **docs**: Documentation update or addition - **feat**: Introduces a new feature or enhancement to the codebase - **fix**: Bug fix or error correction - **i18n**: Internationalization or localization changes - **perf**: Performance improvement - **refactor**: Code restructuring for better maintainability, readability, or scalability - **style**: Changes that do not affect the meaning of the code (white space, formatting, missing semi-colons, etc.) - **test**: Adding missing tests or correcting existing tests - **WIP**: Work in progress, a temporary label for incomplete or ongoing work # Changelog Entry ### Description This pull request updates the path handling in the `env.py` file to use `Path().resolve()` for canonical path resolution, which helps in correctly determining the base directories. It also updates the `alembic.ini` configuration for `prepend_sys_path`. ### Changed - In `backend/open_webui/env.py`: - Changed the definition of `OPEN_WEBUI_DIR` to `_this_file_path_resolved.parent`, where `_this_file_path_resolved` is `Path(__file__).resolve()`, ensuring canonical path resolution. - Updated `BACKEND_DIR` to be the parent of the newly defined `OPEN_WEBUII_DIR`. - Updated `BASE_DIR` to be the parent of the newly defined `BACKEND_DIR`. - Added comments explaining the purpose of `_this_file_path_resolved`, `OPEN_WEBUI_DIR`, `BACKEND_DIR`, and `BASE_DIR`. - In `backend/open_webui/alembic.ini`: - Updated `script_location` in the `[alembic]` section to `migrations`. - Changed `prepend_sys_path` from `.` to `..` in the `[alembic]` section. ### Removed - In `backend/open_webui/env.py`: - Removed the previous definitions of `OPEN_WEBUI_DIR`, `BACKEND_DIR`, and `BASE_DIR` that relied directly on `Path(__file__).parent` without `resolve()`. - Removed `print(OPEN_WEBUI_DIR)`, `print(BACKEND_DIR)`, and `print(BASE_DIR)` statements. ### Fixed - Improved the robustness and correctness of directory path resolution within the application by utilizing `Path().resolve()` to handle relative path components (`..` or `.`). - The following error when attempting to run Alembic related commands from `backend/open_webui`: ```cmd ➜ open_webui git:(dev) ✗ alembic history Traceback (most recent call last): File "/home/g30/.pyenv/versions/3.11.13/bin/alembic", line 8, in <module> sys.exit(main()) ^^^^^^ File "/home/g30/.pyenv/versions/3.11.13/lib/python3.11/site-packages/alembic/config.py", line 636, in main CommandLine(prog=prog).main(argv=argv) File "/home/g30/.pyenv/versions/3.11.13/lib/python3.11/site-packages/alembic/config.py", line 626, in main self.run_cmd(cfg, options) File "/home/g30/.pyenv/versions/3.11.13/lib/python3.11/site-packages/alembic/config.py", line 603, in run_cmd fn( File "/home/g30/.pyenv/versions/3.11.13/lib/python3.11/site-packages/alembic/command.py", line 548, in history _display_history(config, script, base, head) File "/home/g30/.pyenv/versions/3.11.13/lib/python3.11/site-packages/alembic/command.py", line 517, in _display_history for sc in script.walk_revisions( File "/home/g30/.pyenv/versions/3.11.13/lib/python3.11/site-packages/alembic/script/base.py", line 304, in walk_revisions for rev in self.revision_map.iterate_revisions( File "/home/g30/.pyenv/versions/3.11.13/lib/python3.11/site-packages/alembic/script/revision.py", line 814, in iterate_revisions revisions, heads = fn( ^^^ File "/home/g30/.pyenv/versions/3.11.13/lib/python3.11/site-packages/alembic/script/revision.py", line 1447, in _collect_upgrade_revisions for rev in self._parse_upgrade_target( ^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/home/g30/.pyenv/versions/3.11.13/lib/python3.11/site-packages/alembic/script/revision.py", line 1234, in _parse_upgrade_target return self.get_revisions(target) ^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/home/g30/.pyenv/versions/3.11.13/lib/python3.11/site-packages/alembic/script/revision.py", line 544, in get_revisions resolved_id, branch_label = self._resolve_revision_number(id_) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/home/g30/.pyenv/versions/3.11.13/lib/python3.11/site-packages/alembic/script/revision.py", line 768, in _resolve_revision_number self._revision_map File "/home/g30/.pyenv/versions/3.11.13/lib/python3.11/site-packages/sqlalchemy/util/langhelpers.py", line 1145, in __get__ obj.__dict__[self.__name__] = result = self.fget(obj) ^^^^^^^^^^^^^^ File "/home/g30/.pyenv/versions/3.11.13/lib/python3.11/site-packages/alembic/script/revision.py", line 209, in _revision_map for revision in self._generator(): File "/home/g30/.pyenv/versions/3.11.13/lib/python3.11/site-packages/alembic/script/base.py", line 154, in _load_revisions script = Script._from_filename(self, dir_name, filename) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/home/g30/.pyenv/versions/3.11.13/lib/python3.11/site-packages/alembic/script/base.py", line 1048, in _from_filename module = util.load_python_file(dir_, filename) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/home/g30/.pyenv/versions/3.11.13/lib/python3.11/site-packages/alembic/util/pyfiles.py", line 95, in load_python_file module = load_module_py(module_id, path) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/home/g30/.pyenv/versions/3.11.13/lib/python3.11/site-packages/alembic/util/pyfiles.py", line 113, in load_module_py spec.loader.exec_module(module) # type: ignore ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "<frozen importlib._bootstrap_external>", line 940, in exec_module File "<frozen importlib._bootstrap>", line 241, in _call_with_frames_removed File "/home/g30/docker-projects/open-webui-dev/backend/open_webui/migrations/versions/7e5b5dc7342b_init.py", line 14, in <module> import open_webui.internal.db ModuleNotFoundError: No module named 'open_webui' ``` Result **AFTER** the fix: ```cmd ➜ open_webui git:(dev) ✗ alembic history 9f0c9cd09105 -> d31026856c01 (head), Update folder table data 3781e22d8b01 -> 9f0c9cd09105, Add note table 7826ab40b532 -> 3781e22d8b01, Update message & channel tables 57c599a3cb57 -> 7826ab40b532, Update file table 922e7a387820 -> 57c599a3cb57, Add channel table 4ace53fd72c8 -> 922e7a387820, Add group table af906e964978 -> 4ace53fd72c8, Update folder table and change DateTime to BigInteger for timestamp fields c29facfe716b -> af906e964978, Add feedback table c69f45358db4 -> c29facfe716b, Update file table path 3ab32c4b8f59 -> c69f45358db4, Add folder table 1af9b942657b -> 3ab32c4b8f59, Update tags 242a2047eae0 -> 1af9b942657b, Migrate tags 6a39f3d8e55c -> 242a2047eae0, Update chat table c0fbf31ca0db -> 6a39f3d8e55c, Add knowledge table ca81bd47c050 -> c0fbf31ca0db, Update file table 7e5b5dc7342b -> ca81bd47c050, Add config table <base> -> 7e5b5dc7342b, init ``` ### Breaking Changes - **BREAKING CHANGE**: The underlying mechanism for resolving core application directory paths (`OPEN_WEBUI_DIR`, `BACKEND_DIR`, `BASE_DIR`) has changed from direct `.parent` access to canonical resolution using `Path().resolve()`. This might affect any external scripts or configurations that rely on specific non-canonical path interpretations. --- ### Additional Information - This change aims to provide more reliable and consistent pathing for the application, especially in environments where `__file__` might contain relative components or symlinks. - The `alembic.ini` adjustment for `prepend_sys_path` ensures that the alembic environment can correctly locate necessary modules by adjusting the Python system path. ### Screenshots or Videos <img width="1903" height="1233" alt="image" src="https://github.com/user-attachments/assets/a0ef0f44-933e-4948-9319-59e02317010a" /> ### Contributor License Agreement By submitting this pull request, I confirm that I have read and fully agree to the [Contributor License Agreement (CLA)](/CONTRIBUTOR_LICENSE_AGREEMENT), and I am providing my contributions under its terms. --- <sub>🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.</sub>
GiteaMirror added the pull-request label 2026-04-20 05:16:51 -05:00
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: github-starred/open-webui#24193