[PR #24692] [CLOSED] fix: don't break fresh-DB installs in oauth_session / user-update migrations #98823

Closed
opened 2026-05-16 01:43:59 -05:00 by GiteaMirror · 0 comments
Owner

📋 Pull Request Information

Original PR: https://github.com/open-webui/open-webui/pull/24692
Author: @Classic298
Created: 5/13/2026
Status: Closed

Base: devHead: fix/fresh-db-migrations


📝 Commits (2)

  • 614c1cf fix: don't break fresh-DB installs in user-related migrations
  • c8a6f05 fix: make alembic downgrade chain reversible on fresh DBs

📊 Changes

5 files changed (+60 additions, -48 deletions)

View changed files

📝 backend/open_webui/migrations/versions/38d63c18f30f_add_oauth_session_table.py (+8 -12)
📝 backend/open_webui/migrations/versions/4ace53fd72c8_update_folder_table_datetime.py (+8 -4)
📝 backend/open_webui/migrations/versions/56359461a091_add_calendar_tables.py (+23 -13)
📝 backend/open_webui/migrations/versions/b10670c03dd5_update_user_table.py (+9 -7)
📝 backend/open_webui/migrations/versions/d4e5f6a7b8c9_add_automation_tables.py (+12 -12)

📄 Description

After 2c2d06c31 ("refac: deprecate peewee migration layer") removed the peewee bootstrap, fresh databases now enter alembic with the schema as declared by the init migration (7e5b5dc7342b). Two later migrations were written under the old assumption that the schema came from peewee and silently break the fresh-install path:

  1. 38d63c18f30f (Add oauth_session table) — gated its user-PK fixup on not id_column.get('unique', False). Postgres reports PK columns as not-separately-unique, so the gate is True on fresh DBs and the block unconditionally calls create_primary_key('pk_user_id', ['id']) on a table that already has a PK on id. Postgres rejects with:

    InvalidTableDefinition: multiple primary keys for table "user"
    

    That rolls the entire alembic transaction back (DDL is transactional on Postgres), so on startup the config table doesn't exist either, and STATE.load() crashes with:

    psycopg2.errors.UndefinedTable: relation "config" does not exist
    

    SQLite tolerated this because batch_alter_table rebuilds the table from scratch — the duplicate PK was lost in the copy. Reported by urbenlegend on open-webui#24560 (last reply on dev).

    Fix: gate on "PK isn't already on id", so the block only runs on legacy peewee-shaped schemas.

  2. b10670c03dd5 (Update user table) — _drop_sqlite_indexes_for_column issued DROP INDEX on every index referencing the target column, including the auto-created indexes that back UNIQUE constraints (e.g. sqlite_autoindex_user_2 for oauth_sub). SQLite refuses:

    index associated with UNIQUE or PRIMARY KEY constraint cannot be
    dropped
    

    This is invisible on Postgres (the dialect branch is gated to if conn.dialect.name == 'sqlite'), but stops the migration chain partway through on every fresh SQLite install.

    Fix: skip sqlite_autoindex_* indexes — batch_alter_table.drop_column later handles them through its table-rebuild path.

Verified both fixes with embedded postgres (via pgserver) and SQLite — all 44 migrations run cleanly to head on both, and STATE.load() no longer raises.

Contributor License Agreement

Note

Deleting the CLA section will lead to immediate closure of your PR and it will not be merged in.


🔄 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/24692 **Author:** [@Classic298](https://github.com/Classic298) **Created:** 5/13/2026 **Status:** ❌ Closed **Base:** `dev` ← **Head:** `fix/fresh-db-migrations` --- ### 📝 Commits (2) - [`614c1cf`](https://github.com/open-webui/open-webui/commit/614c1cff6c311732391ac428bdc76b1ca6528e7c) fix: don't break fresh-DB installs in user-related migrations - [`c8a6f05`](https://github.com/open-webui/open-webui/commit/c8a6f051ce96643e4e0afccef783d29906d5954a) fix: make alembic downgrade chain reversible on fresh DBs ### 📊 Changes **5 files changed** (+60 additions, -48 deletions) <details> <summary>View changed files</summary> 📝 `backend/open_webui/migrations/versions/38d63c18f30f_add_oauth_session_table.py` (+8 -12) 📝 `backend/open_webui/migrations/versions/4ace53fd72c8_update_folder_table_datetime.py` (+8 -4) 📝 `backend/open_webui/migrations/versions/56359461a091_add_calendar_tables.py` (+23 -13) 📝 `backend/open_webui/migrations/versions/b10670c03dd5_update_user_table.py` (+9 -7) 📝 `backend/open_webui/migrations/versions/d4e5f6a7b8c9_add_automation_tables.py` (+12 -12) </details> ### 📄 Description After 2c2d06c31 ("refac: deprecate peewee migration layer") removed the peewee bootstrap, fresh databases now enter alembic with the schema as declared by the init migration (7e5b5dc7342b). Two later migrations were written under the old assumption that the schema came from peewee and silently break the fresh-install path: 1. 38d63c18f30f (Add oauth_session table) — gated its user-PK fixup on `not id_column.get('unique', False)`. Postgres reports PK columns as not-separately-unique, so the gate is True on fresh DBs and the block unconditionally calls `create_primary_key('pk_user_id', ['id'])` on a table that already has a PK on `id`. Postgres rejects with: InvalidTableDefinition: multiple primary keys for table "user" That rolls the entire alembic transaction back (DDL is transactional on Postgres), so on startup the config table doesn't exist either, and STATE.load() crashes with: psycopg2.errors.UndefinedTable: relation "config" does not exist SQLite tolerated this because batch_alter_table rebuilds the table from scratch — the duplicate PK was lost in the copy. Reported by urbenlegend on open-webui#24560 (last reply on dev). Fix: gate on "PK isn't already on `id`", so the block only runs on legacy peewee-shaped schemas. 2. b10670c03dd5 (Update user table) — `_drop_sqlite_indexes_for_column` issued DROP INDEX on every index referencing the target column, including the auto-created indexes that back UNIQUE constraints (e.g. `sqlite_autoindex_user_2` for `oauth_sub`). SQLite refuses: index associated with UNIQUE or PRIMARY KEY constraint cannot be dropped This is invisible on Postgres (the dialect branch is gated to `if conn.dialect.name == 'sqlite'`), but stops the migration chain partway through on every fresh SQLite install. Fix: skip `sqlite_autoindex_*` indexes — `batch_alter_table.drop_column` later handles them through its table-rebuild path. Verified both fixes with embedded postgres (via pgserver) and SQLite — all 44 migrations run cleanly to head on both, and `STATE.load()` no longer raises. ### Contributor License Agreement <!-- 🚨 DO NOT DELETE THE TEXT BELOW 🚨 Keep the "Contributor License Agreement" confirmation text intact. Deleting it will trigger the CLA-Bot to INVALIDATE your PR. Your PR will NOT be reviewed or merged until you check the box below confirming that you have read and agree to the terms of the CLA. --> - [x] By submitting this pull request, I confirm that I have read and fully agree to the [Contributor License Agreement (CLA)](https://github.com/open-webui/open-webui/blob/main/CONTRIBUTOR_LICENSE_AGREEMENT), and I am providing my contributions under its terms. > [!NOTE] > Deleting the CLA section will lead to immediate closure of your PR and it will not be merged in. --- <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-05-16 01:43:59 -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#98823