From 627a6d1eee492454c75e1bb3616195e40f0fee86 Mon Sep 17 00:00:00 2001 From: silentoplayz Date: Fri, 28 Nov 2025 05:43:14 -0500 Subject: [PATCH 01/10] docs: Add manual Alembic database migration guide This change adds a new, comprehensive Docusaurus page that provides instructions on how to manually apply Alembic database migrations. This is in response to a GitHub discussion where users were encountering issues with automatic migrations after updating the application. --- .../manual-database-migration.md | 158 ++++++++++++++++++ 1 file changed, 158 insertions(+) create mode 100644 docs/troubleshooting/manual-database-migration.md diff --git a/docs/troubleshooting/manual-database-migration.md b/docs/troubleshooting/manual-database-migration.md new file mode 100644 index 0000000..21f8ea4 --- /dev/null +++ b/docs/troubleshooting/manual-database-migration.md @@ -0,0 +1,158 @@ +--- +sidebar_position: 900 +title: Manual Alembic Database Migration Guide +--- + +This guide provides step-by-step instructions for manually applying Alembic database migrations in Open WebUI. Migrations are typically run automatically on startup, but you may need to run them manually for maintenance, debugging, or deployment scenarios. + +## Prerequisites + +- Ensure you have a Python environment with Open WebUI dependencies installed. +- Your database connection must be configured via the `DATABASE_URL` environment variable. +- You need access to the backend directory of your Open WebUI installation. + +## Accessing the Container (for Docker installations) + +If you are running Open WebUI in a Docker container, you'll first need to get a shell into the container: + +```bash +docker exec -it open-webui /bin/bash +``` + +:::note +Replace `open-webui` with the name of your container if it's different. +::: + +## Manual Migration Commands + +This section provides instructions for running Alembic commands. The correct directory path depends on whether you are running Open WebUI from a local installation or within a Docker container. + +### 1. Navigate to the Correct Directory + +#### Local Installation +If you are running Open WebUI from a local clone of the repository, navigate to the `backend/open_webui` directory from the repository root: +```bash +cd backend/open_webui +``` + +#### Docker Container +After accessing the container with `docker exec`, you will be in the `/app/backend` directory. From there, navigate to the `open_webui` directory: +```bash +cd open_webui +``` + +:::info +All subsequent Alembic commands must be run from this directory (`/app/backend/open_webui` inside the container or `backend/open_webui` in a local setup), where the `alembic.ini` file is located. +::: + +### 2. Check the Current Database Revision + +To see the current revision of your database, run: + +```bash +alembic current +``` + +### 3. Apply All Pending Migrations (Upgrade) + +To upgrade your database to the latest version, which applies all pending migrations, run: + +```bash +alembic upgrade head +``` + +### 4. Upgrade to a Specific Revision + +You can upgrade to a specific migration by providing its revision ID: + +```bash +alembic upgrade +``` + +### 5. Downgrade to the Previous Revision + +To revert the last migration, use: + +```bash +alembic downgrade -1 +``` + +### 6. Downgrade to a Specific Revision + +You can also downgrade to a specific revision: + +```bash +alembic downgrade +``` + +### 7. View Migration History + +To see the history of all migrations, including their revision IDs, run: + +```bash +alembic history +``` + +:::tip +The `alembic history` command is useful for finding the revision ID to use with the `upgrade` and `downgrade` commands. +::: + +## Common Scenarios + +### Fresh Database Setup + +For a new database, run the following command to apply all migrations: + +```bash +alembic upgrade head +``` + +### Specific Migration Issues + +If a migration fails, you can resolve it with the following steps: +1. Check the current revision: `alembic current` +2. Identify the problematic revision from the error message. +3. Downgrade to the previous revision: `alembic downgrade -1` +4. Fix the underlying issue (e.g., a missing constraint). +5. Run the upgrade again: `alembic upgrade head` + +### Production Deployment + +When deploying to a production environment: + +:::danger[Backup Your Database] +**Always back up your database before running migrations in a production environment.** +::: + +1. Run migrations during a maintenance window to avoid service disruptions. +2. Verify the migration was successful with `alembic current`. + +## Troubleshooting + +### `InvalidForeignKey` Error + +If you encounter an error similar to `sqlalchemy.exc.ProgrammingError: (psycopg2.errors.InvalidForeignKey) there is no unique constraint matching given keys for referenced table "user"`, it's likely that a primary key constraint is missing on the `user` table. + +To fix this, you need to connect to your PostgreSQL database and run the following SQL command: + +```sql +ALTER TABLE public."user" ADD CONSTRAINT user_pk PRIMARY KEY (id); +``` + +After running this command, you can retry the `alembic upgrade head` command. + +### Migration Path Issues + +If you encounter path resolution issues, ensure you are running the `alembic` commands from the correct directory (`open_webui` inside the Docker container, or `backend/open_webui` in a local setup) where the `alembic.ini` file is located. + +### Database Connection + +Verify that your `DATABASE_URL` environment variable is correctly set and that the database is accessible from where you are running the commands. + +## Notes + +:::note +The application automatically runs migrations on startup via the `run_migrations()` function in `config.py`. Manual migrations are useful for debugging, controlled deployments, or when automatic migration is disabled. +::: + +- Some migrations include data transformation logic that may take a significant amount of time to run on large datasets. From 173422191c3cbfa0e376f42f6508ecc69bcae0a8 Mon Sep 17 00:00:00 2001 From: silentoplayz Date: Fri, 28 Nov 2025 06:22:26 -0500 Subject: [PATCH 02/10] Update manual-database-migration.md --- .../manual-database-migration.md | 29 ++++++++++++++----- 1 file changed, 22 insertions(+), 7 deletions(-) diff --git a/docs/troubleshooting/manual-database-migration.md b/docs/troubleshooting/manual-database-migration.md index 21f8ea4..9236f9f 100644 --- a/docs/troubleshooting/manual-database-migration.md +++ b/docs/troubleshooting/manual-database-migration.md @@ -30,13 +30,17 @@ This section provides instructions for running Alembic commands. The correct dir ### 1. Navigate to the Correct Directory #### Local Installation + If you are running Open WebUI from a local clone of the repository, navigate to the `backend/open_webui` directory from the repository root: + ```bash cd backend/open_webui ``` #### Docker Container + After accessing the container with `docker exec`, you will be in the `/app/backend` directory. From there, navigate to the `open_webui` directory: + ```bash cd open_webui ``` @@ -110,11 +114,12 @@ alembic upgrade head ### Specific Migration Issues If a migration fails, you can resolve it with the following steps: -1. Check the current revision: `alembic current` -2. Identify the problematic revision from the error message. -3. Downgrade to the previous revision: `alembic downgrade -1` -4. Fix the underlying issue (e.g., a missing constraint). -5. Run the upgrade again: `alembic upgrade head` + +1. Check the current revision: `alembic current` +2. Identify the problematic revision from the error message. +3. Downgrade to the previous revision: `alembic downgrade -1` +4. Fix the underlying issue (e.g., a missing constraint). +5. Run the upgrade again: `alembic upgrade head` ### Production Deployment @@ -124,8 +129,8 @@ When deploying to a production environment: **Always back up your database before running migrations in a production environment.** ::: -1. Run migrations during a maintenance window to avoid service disruptions. -2. Verify the migration was successful with `alembic current`. +1. Run migrations during a maintenance window to avoid service disruptions. +2. Verify the migration was successful with `alembic current`. ## Troubleshooting @@ -141,6 +146,16 @@ ALTER TABLE public."user" ADD CONSTRAINT user_pk PRIMARY KEY (id); After running this command, you can retry the `alembic upgrade head` command. +### Incorrect Autogenerated Migrations + +When creating a new migration, the `alembic revision --autogenerate` command can sometimes create an incorrect migration file that attempts to drop existing tables. This can happen if Alembic has trouble detecting the current state of your database schema. + +If you encounter this issue, here are a few things to try: + +1. **Ensure Your Database is Up-to-Date:** Before generating a new migration, make sure your database is fully upgraded to the latest revision by running `alembic upgrade head`. An out-of-date database is a common cause of this problem. +2. **Create a Manual Migration:** For simple changes, you can avoid `--autogenerate` altogether by creating a manual migration file. This gives you full control over the `upgrade()` and `downgrade()` functions. +3. **Start Fresh:** If your database schema is in a state that is difficult to recover from, the simplest solution may be to start with a fresh installation. + ### Migration Path Issues If you encounter path resolution issues, ensure you are running the `alembic` commands from the correct directory (`open_webui` inside the Docker container, or `backend/open_webui` in a local setup) where the `alembic.ini` file is located. From c2eb0cbfb88b4c5a14d7d852845a8412de051957 Mon Sep 17 00:00:00 2001 From: silentoplayz Date: Fri, 28 Nov 2025 06:36:14 -0500 Subject: [PATCH 03/10] Update manual-database-migration.md --- docs/troubleshooting/manual-database-migration.md | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/docs/troubleshooting/manual-database-migration.md b/docs/troubleshooting/manual-database-migration.md index 9236f9f..bc8b710 100644 --- a/docs/troubleshooting/manual-database-migration.md +++ b/docs/troubleshooting/manual-database-migration.md @@ -156,6 +156,14 @@ If you encounter this issue, here are a few things to try: 2. **Create a Manual Migration:** For simple changes, you can avoid `--autogenerate` altogether by creating a manual migration file. This gives you full control over the `upgrade()` and `downgrade()` functions. 3. **Start Fresh:** If your database schema is in a state that is difficult to recover from, the simplest solution may be to start with a fresh installation. +### Migrations in Multi-Server Deployments + +:::warning[Update All Instances Simultaneously] +If you are running Open WebUI in a load-balanced, multi-server environment, it is critical that you **update all instances of the application at the same time.** + +A rolling update, where servers are updated one by one, can cause service disruptions. If the first server to be updated applies a destructive migration (e.g., dropping a column), all other servers still running the old code will immediately encounter errors, as they will be trying to access a database schema that is no longer compatible. +::: + ### Migration Path Issues If you encounter path resolution issues, ensure you are running the `alembic` commands from the correct directory (`open_webui` inside the Docker container, or `backend/open_webui` in a local setup) where the `alembic.ini` file is located. From 1b6cfa9850c4b69242baf1159f86e83451784991 Mon Sep 17 00:00:00 2001 From: silentoplayz Date: Fri, 28 Nov 2025 12:13:47 -0500 Subject: [PATCH 04/10] refac --- .../manual-database-migration.md | 42 ++++++++++++++++--- 1 file changed, 36 insertions(+), 6 deletions(-) diff --git a/docs/troubleshooting/manual-database-migration.md b/docs/troubleshooting/manual-database-migration.md index bc8b710..6fae393 100644 --- a/docs/troubleshooting/manual-database-migration.md +++ b/docs/troubleshooting/manual-database-migration.md @@ -49,7 +49,37 @@ cd open_webui All subsequent Alembic commands must be run from this directory (`/app/backend/open_webui` inside the container or `backend/open_webui` in a local setup), where the `alembic.ini` file is located. ::: -### 2. Check the Current Database Revision +### 2. Set Environment Variables (Docker Only) + +:::danger[Critical Step for Docker Users] +When you open a shell in a Docker container with `docker exec`, the application's environment variables are **not** automatically loaded. You must set them manually before running any Alembic commands to avoid errors. +::: + +You will need to export the following variables: + +1. `PYTHONPATH`: This tells Python where to find the application's modules. +2. `DATABASE_URL`: This is the connection string for your database. +3. `WEBUI_SECRET_KEY`: This is a required variable for the application's authentication system. + +You can find the values for `DATABASE_URL` and `WEBUI_SECRET_KEY` in your `docker-compose.yaml` file or the `docker run` command you used to start the container. + +Before proceeding, export the variables in your container shell: + +```bash +export PYTHONPATH=.:../ +export DATABASE_URL="your-database-url-here" +export WEBUI_SECRET_KEY="your-secret-key-here" +``` + +For example, for a PostgreSQL database, it might look like this: + +```bash +export PYTHONPATH=.:../ +export DATABASE_URL="postgresql://user:password@host:port/database" +export WEBUI_SECRET_KEY="t0p-s3cr3t" +``` + +### 3. Check the Current Database Revision To see the current revision of your database, run: @@ -57,7 +87,7 @@ To see the current revision of your database, run: alembic current ``` -### 3. Apply All Pending Migrations (Upgrade) +### 4. Apply All Pending Migrations (Upgrade) To upgrade your database to the latest version, which applies all pending migrations, run: @@ -65,7 +95,7 @@ To upgrade your database to the latest version, which applies all pending migrat alembic upgrade head ``` -### 4. Upgrade to a Specific Revision +### 5. Upgrade to a Specific Revision You can upgrade to a specific migration by providing its revision ID: @@ -73,7 +103,7 @@ You can upgrade to a specific migration by providing its revision ID: alembic upgrade ``` -### 5. Downgrade to the Previous Revision +### 6. Downgrade to the Previous Revision To revert the last migration, use: @@ -81,7 +111,7 @@ To revert the last migration, use: alembic downgrade -1 ``` -### 6. Downgrade to a Specific Revision +### 7. Downgrade to a Specific Revision You can also downgrade to a specific revision: @@ -89,7 +119,7 @@ You can also downgrade to a specific revision: alembic downgrade ``` -### 7. View Migration History +### 8. View Migration History To see the history of all migrations, including their revision IDs, run: From 6bf1cadbb6a1ae403864d6963b0cf43f3febb263 Mon Sep 17 00:00:00 2001 From: silentoplayz Date: Fri, 28 Nov 2025 13:18:14 -0500 Subject: [PATCH 05/10] Update manual-database-migration.md --- .../manual-database-migration.md | 759 ++++++++++++++---- 1 file changed, 613 insertions(+), 146 deletions(-) diff --git a/docs/troubleshooting/manual-database-migration.md b/docs/troubleshooting/manual-database-migration.md index 6fae393..23cc31a 100644 --- a/docs/troubleshooting/manual-database-migration.md +++ b/docs/troubleshooting/manual-database-migration.md @@ -1,211 +1,678 @@ --- sidebar_position: 900 -title: Manual Alembic Database Migration Guide +title: Manual Alembic Database Migration +sidebar_label: Manual Migration +description: Complete guide for manually running Alembic database migrations when Open WebUI's automatic migration fails or requires direct intervention. +keywords: [alembic, migration, database, troubleshooting, sqlite, postgresql, docker] --- -This guide provides step-by-step instructions for manually applying Alembic database migrations in Open WebUI. Migrations are typically run automatically on startup, but you may need to run them manually for maintenance, debugging, or deployment scenarios. +import Tabs from '@theme/Tabs'; +import TabItem from '@theme/TabItem'; -## Prerequisites +## Overview -- Ensure you have a Python environment with Open WebUI dependencies installed. -- Your database connection must be configured via the `DATABASE_URL` environment variable. -- You need access to the backend directory of your Open WebUI installation. +Open WebUI automatically runs database migrations on startup. **Manual migration is rarely needed** and should only be performed in specific failure scenarios or maintenance situations. -## Accessing the Container (for Docker installations) +:::info When Manual Migration is Required +You need manual migration only if: -If you are running Open WebUI in a Docker container, you'll first need to get a shell into the container: - -```bash -docker exec -it open-webui /bin/bash -``` - -:::note -Replace `open-webui` with the name of your container if it's different. +- Open WebUI logs show specific migration errors during startup +- You're performing offline database maintenance +- Automatic migration fails after a version upgrade +- You're migrating between database types (SQLite ↔ PostgreSQL) +- A developer has instructed you to run migrations manually ::: -## Manual Migration Commands - -This section provides instructions for running Alembic commands. The correct directory path depends on whether you are running Open WebUI from a local installation or within a Docker container. - -### 1. Navigate to the Correct Directory - -#### Local Installation - -If you are running Open WebUI from a local clone of the repository, navigate to the `backend/open_webui` directory from the repository root: - -```bash -cd backend/open_webui -``` - -#### Docker Container - -After accessing the container with `docker exec`, you will be in the `/app/backend` directory. From there, navigate to the `open_webui` directory: - -```bash -cd open_webui -``` - -:::info -All subsequent Alembic commands must be run from this directory (`/app/backend/open_webui` inside the container or `backend/open_webui` in a local setup), where the `alembic.ini` file is located. +:::danger Critical Warning +Manual migration can corrupt your database if performed incorrectly. **Always create a verified backup before proceeding.** ::: -### 2. Set Environment Variables (Docker Only) +## Prerequisites Checklist -:::danger[Critical Step for Docker Users] -When you open a shell in a Docker container with `docker exec`, the application's environment variables are **not** automatically loaded. You must set them manually before running any Alembic commands to avoid errors. +Before starting, ensure you have: + +- [ ] **Root/admin access** to your Open WebUI installation +- [ ] **Database location confirmed** (default: `/app/backend/data/webui.db` in Docker) +- [ ] **Open WebUI completely stopped** (no running processes) +- [ ] **Backup created and verified** (see below) +- [ ] **Access to container or Python environment** where Open WebUI runs + +:::warning Stop All Processes First +Database migrations cannot run while Open WebUI is active. You **must** stop all Open WebUI processes before attempting manual migration. ::: -You will need to export the following variables: +## Step 1: Create and Verify Backup -1. `PYTHONPATH`: This tells Python where to find the application's modules. -2. `DATABASE_URL`: This is the connection string for your database. -3. `WEBUI_SECRET_KEY`: This is a required variable for the application's authentication system. +### Backup Your Database -You can find the values for `DATABASE_URL` and `WEBUI_SECRET_KEY` in your `docker-compose.yaml` file or the `docker run` command you used to start the container. + + + ```bash title="Terminal" + # Find your database location first + docker inspect open-webui | grep -A 5 Mounts -Before proceeding, export the variables in your container shell: + # Create timestamped backup + cp /path/to/webui.db /path/to/webui.db.backup.$(date +%Y%m%d_%H%M%S) + ``` + + + ```bash title="Terminal" + pg_dump -h localhost -U your_user -d open_webui_db > backup_$(date +%Y%m%d_%H%M%S).sql + ``` + + -```bash -export PYTHONPATH=.:../ -export DATABASE_URL="your-database-url-here" -export WEBUI_SECRET_KEY="your-secret-key-here" +### Verify Backup Integrity + +**Critical:** Test that your backup is readable before proceeding. + + + + ```bash title="Terminal - Verify Backup" + # Test backup can be opened + sqlite3 /path/to/webui.db.backup "SELECT count(*) FROM user;" + + # Verify schema matches + sqlite3 /path/to/webui.db ".schema" > current-schema.sql + sqlite3 /path/to/webui.db.backup ".schema" > backup-schema.sql + diff current-schema.sql backup-schema.sql + ``` + + + ```bash title="Terminal - Verify Backup" + # Verify backup file is not empty and contains SQL + head -n 20 backup_*.sql + grep -c "CREATE TABLE" backup_*.sql + ``` + + + +:::tip Backup Storage +Store backups on a **different disk or volume** than your database to protect against disk failure. +::: + +## Step 2: Diagnose Current State + +Before attempting any fixes, gather information about your database state. + +### Access Your Environment + + + + ```bash title="Terminal" + # Stop Open WebUI first + docker stop open-webui + + # Enter container for diagnostics + docker run --rm -it \ + -v open-webui:/app/backend/data \ + --entrypoint /bin/bash \ + ghcr.io/open-webui/open-webui:main + ``` + + :::note Default Directory + When you enter the container, you'll be in `/app`. The Alembic configuration is at `/app/backend/open_webui/alembic.ini`. + ::: + + + ```bash title="Terminal" + # Navigate to Open WebUI installation + cd /path/to/open-webui/backend/open_webui + + # Activate virtual environment if used + source ../../venv/bin/activate # Linux/Mac + # venv\Scripts\activate # Windows + ``` + + + +### Run Diagnostic Commands + +Navigate to the directory containing `alembic.ini`: + +```bash title="Terminal - Navigate to Alembic Directory" +cd /app/backend/open_webui # Docker +# OR +cd /path/to/open-webui/backend/open_webui # Local ``` -For example, for a PostgreSQL database, it might look like this: +Execute these read-only diagnostic commands: -```bash -export PYTHONPATH=.:../ -export DATABASE_URL="postgresql://user:password@host:port/database" -export WEBUI_SECRET_KEY="t0p-s3cr3t" -``` +```bash title="Terminal - Diagnostics (Safe - Read Only)" +# Verify alembic.ini exists +ls -la alembic.ini -### 3. Check the Current Database Revision +# Check current migration version +alembic current -v -To see the current revision of your database, run: +# Check target (latest) version +alembic heads -```bash -alembic current -``` - -### 4. Apply All Pending Migrations (Upgrade) - -To upgrade your database to the latest version, which applies all pending migrations, run: - -```bash -alembic upgrade head -``` - -### 5. Upgrade to a Specific Revision - -You can upgrade to a specific migration by providing its revision ID: - -```bash -alembic upgrade -``` - -### 6. Downgrade to the Previous Revision - -To revert the last migration, use: - -```bash -alembic downgrade -1 -``` - -### 7. Downgrade to a Specific Revision - -You can also downgrade to a specific revision: - -```bash -alembic downgrade -``` - -### 8. View Migration History - -To see the history of all migrations, including their revision IDs, run: - -```bash +# List all migration history alembic history + +# Check for branching (indicates issues) +alembic branches ``` -:::tip -The `alembic history` command is useful for finding the revision ID to use with the `upgrade` and `downgrade` commands. +**Expected output:** + +``` +# alembic current should show something like: +ae1027a6acf (head) + +# If you see multiple heads or branching, your migration history has issues +``` + +:::info Understanding Output + +- `alembic current` = what version your database thinks it's at +- `alembic heads` = what version the code expects +- If `current` is older than `heads`, you have pending migrations +- If `current` equals `heads`, your database is up-to-date ::: -## Common Scenarios +
+Check Actual Database Tables -### Fresh Database Setup +Verify what's actually in your database: -For a new database, run the following command to apply all migrations: + + + ```bash title="Terminal" + sqlite3 /app/backend/data/webui.db ".tables" + sqlite3 /app/backend/data/webui.db "SELECT * FROM alembic_version;" + ``` + + + ```bash title="Terminal" + psql -h localhost -U user -d dbname -c "\dt" + psql -h localhost -U user -d dbname -c "SELECT * FROM alembic_version;" + ``` + + -```bash +
+ +## Step 3: Configure Environment + +Set required environment variables for manual Alembic execution. + +```bash title="Terminal - Set Environment Variables" +# Required: Database URL +# For Docker with SQLite (note the 4 slashes for absolute path) +export DATABASE_URL="sqlite:////app/backend/data/webui.db" + +# For local install with SQLite (relative path from open_webui directory) +export DATABASE_URL="sqlite:///../data/webui.db" + +# For PostgreSQL +export DATABASE_URL="postgresql://user:password@localhost:5432/open_webui_db" +``` + +:::warning Path Syntax for SQLite + +- `sqlite:////app/...` = 4 slashes total (absolute path) +- `sqlite:///data/...` = 3 slashes total (relative path) +- The extra slash after `sqlite:///` makes it absolute +::: + +## Step 4: Apply Migrations + +### Standard Upgrade (Most Common) + +If diagnostics show you have pending migrations (`current` < `heads`), upgrade to latest: + +```bash title="Terminal - Upgrade to Latest" +# Ensure you're in the correct directory +cd /app/backend/open_webui + +# Run upgrade alembic upgrade head ``` -### Specific Migration Issues +**Watch for these outputs:** -If a migration fails, you can resolve it with the following steps: +```bash +INFO [alembic.runtime.migration] Context impl SQLiteImpl. +INFO [alembic.runtime.migration] Will assume non-transactional DDL. +# highlight-next-line +INFO [alembic.runtime.migration] Running upgrade abc123 -> def456, add_new_column +``` -1. Check the current revision: `alembic current` -2. Identify the problematic revision from the error message. -3. Downgrade to the previous revision: `alembic downgrade -1` -4. Fix the underlying issue (e.g., a missing constraint). -5. Run the upgrade again: `alembic upgrade head` +:::note "Will assume non-transactional DDL" +This is a **normal informational message** for SQLite, not an error. SQLite doesn't support rollback of schema changes, so migrations run without transaction protection. -### Production Deployment - -When deploying to a production environment: - -:::danger[Backup Your Database] -**Always back up your database before running migrations in a production environment.** +If the process appears to hang after this message, wait 2-3 minutes - some migrations take time. If it's truly stuck, check for database locks (see troubleshooting). ::: -1. Run migrations during a maintenance window to avoid service disruptions. -2. Verify the migration was successful with `alembic current`. +### Upgrade to Specific Version + +If you need to apply migrations up to a specific point: + +```bash title="Terminal - Upgrade to Specific Version" +# List available versions first +alembic history + +# Upgrade to specific revision +alembic upgrade ae1027a6acf +``` + +### Downgrade (Rollback) + +:::danger Data Loss Risk +Downgrading can cause **permanent data loss** if the migration removed columns or tables. Only downgrade if you understand the consequences. +::: + +```bash title="Terminal - Downgrade Migrations" +# Downgrade one version +alembic downgrade -1 + +# Downgrade to specific version +alembic downgrade + +# Nuclear option: Remove all migrations (rarely needed) +alembic downgrade base +``` + +## Step 5: Verify Migration Success + +After running migrations, confirm everything is correct: + +```bash title="Terminal - Post-Migration Verification" +# Verify current version matches expected +alembic current + +# Should show (head) indicating you're at latest +# Example: ae1027a6acf (head) + +# Confirm no pending migrations +alembic upgrade head --sql | head -20 +# If output contains only comments or is empty, you're up to date +``` + +### Test Application Startup + + + + ```bash title="Terminal" + # Exit the diagnostic container + exit + + # Start Open WebUI normally + docker start open-webui + + # Watch logs for migration confirmation + docker logs -f open-webui + ``` + + + ```bash title="Terminal" + # Start Open WebUI + python -m open_webui.main + + # Watch for successful startup messages + ``` + + + +**Successful startup logs:** + +``` +INFO: [db] Database initialization complete +INFO: [main] Open WebUI starting on http://0.0.0.0:8080 +``` ## Troubleshooting -### `InvalidForeignKey` Error +### "No config file 'alembic.ini' found" -If you encounter an error similar to `sqlalchemy.exc.ProgrammingError: (psycopg2.errors.InvalidForeignKey) there is no unique constraint matching given keys for referenced table "user"`, it's likely that a primary key constraint is missing on the `user` table. +**Cause:** You're in the wrong directory. -To fix this, you need to connect to your PostgreSQL database and run the following SQL command: +**Solution:** -```sql -ALTER TABLE public."user" ADD CONSTRAINT user_pk PRIMARY KEY (id); +```bash title="Terminal" +# Find alembic.ini location +find /app -name "alembic.ini" 2>/dev/null # Docker +find . -name "alembic.ini" # Local + +# Navigate to that directory +cd /app/backend/open_webui # Most common path ``` -After running this command, you can retry the `alembic upgrade head` command. +### "Target database is not up to date" -### Incorrect Autogenerated Migrations +**Cause:** Your database version doesn't match expected schema. -When creating a new migration, the `alembic revision --autogenerate` command can sometimes create an incorrect migration file that attempts to drop existing tables. This can happen if Alembic has trouble detecting the current state of your database schema. +**Diagnosis:** -If you encounter this issue, here are a few things to try: +```bash title="Terminal - Diagnose Version Mismatch" +# Check what database thinks its version is +alembic current -1. **Ensure Your Database is Up-to-Date:** Before generating a new migration, make sure your database is fully upgraded to the latest revision by running `alembic upgrade head`. An out-of-date database is a common cause of this problem. -2. **Create a Manual Migration:** For simple changes, you can avoid `--autogenerate` altogether by creating a manual migration file. This gives you full control over the `upgrade()` and `downgrade()` functions. -3. **Start Fresh:** If your database schema is in a state that is difficult to recover from, the simplest solution may be to start with a fresh installation. +# Check what code expects +alembic heads -### Migrations in Multi-Server Deployments +# Compare +``` -:::warning[Update All Instances Simultaneously] -If you are running Open WebUI in a load-balanced, multi-server environment, it is critical that you **update all instances of the application at the same time.** +**Solution depends on diagnosis:** -A rolling update, where servers are updated one by one, can cause service disruptions. If the first server to be updated applies a destructive migration (e.g., dropping a column), all other servers still running the old code will immediately encounter errors, as they will be trying to access a database schema that is no longer compatible. + + + **Scenario:** `alembic current` shows older version than `alembic heads` + + **Fix:** You simply need to apply pending migrations. + + ```bash title="Terminal" + alembic upgrade head + ``` + + + **Scenario:** `alembic current` shows correct version, but you still see errors + + **Cause:** Someone manually modified the database schema without migrations, or a previous migration partially failed. + + **Fix:** Restore from backup - you have database corruption. + + ```bash title="Terminal" + # Stop everything + docker stop open-webui + + # Restore backup + cp /path/to/webui.db.backup /path/to/webui.db + + # Try migration again + alembic upgrade head + ``` + + + **Scenario:** New database that needs initial schema + + **Fix:** Run migrations from scratch. + + ```bash title="Terminal" + alembic upgrade head + ``` + + + +:::danger Never Use "alembic stamp" as a Fix +You may see advice to run `alembic stamp head` to "fix" version mismatches. **This is dangerous.** + +`alembic stamp` tells Alembic "pretend this migration was applied" without actually running it. This creates permanent database corruption where Alembic thinks your schema is up-to-date when it isn't. + +**Only use `alembic stamp head` if:** + +- You manually created all tables using `create_all()` and need to mark them as migrated +- You're a developer initializing a fresh database that matches current schema + +**Never use it to "fix" migration errors.** ::: -### Migration Path Issues +### Process Hangs After "Will assume non-transactional DDL" -If you encounter path resolution issues, ensure you are running the `alembic` commands from the correct directory (`open_webui` inside the Docker container, or `backend/open_webui` in a local setup) where the `alembic.ini` file is located. +**Understanding the message:** This is **not an error**. It's informational. SQLite doesn't support transactional DDL, so Alembic is warning that migrations can't be rolled back automatically. -### Database Connection +**If genuinely stuck:** -Verify that your `DATABASE_URL` environment variable is correctly set and that the database is accessible from where you are running the commands. + + + Some migrations (especially those adding indexes or modifying large tables) take several minutes. -## Notes + **Action:** Wait 3-5 minutes before assuming it's stuck. + + + Another process might have locked the database. + + ```bash title="Terminal - Check for Locks" + # Find processes using database file + fuser /app/backend/data/webui.db + + # Kill any orphaned processes + pkill -f "open-webui" + + # Verify nothing running + ps aux | grep open-webui + + # Try migration again + alembic upgrade head + ``` + + + If the database is corrupted, migration will hang. + + ```bash title="Terminal - Check Integrity" + sqlite3 /app/backend/data/webui.db "PRAGMA integrity_check;" + ``` + + If integrity check fails, restore from backup. + + + +### Autogenerate Detects Removed Tables + +**Symptom:** You ran `alembic revision --autogenerate` and it wants to drop existing tables. + +:::warning Don't Run Autogenerate +**Regular users should NEVER run `alembic revision --autogenerate`.** This command is for developers creating new migration files, not for applying existing migrations. + +The command you want is `alembic upgrade head` (no `revision`, no `--autogenerate`). +::: + +**If you accidentally created a bad migration file:** + +```bash title="Terminal - Remove Bad Migration" +# List migration files +ls -la /app/backend/open_webui/migrations/versions/ + +# Delete the incorrect auto-generated file (newest file) +rm /app/backend/open_webui/migrations/versions/_*.py + +# Restore to known good state +git checkout /app/backend/open_webui/migrations/ # If using git +``` + +**Technical context:** The "autogenerate detects removed tables" issue occurs because Open WebUI's Alembic metadata configuration doesn't import all model definitions. This causes autogenerate to compare against incomplete metadata, thinking tables should be removed. This is a developer-level issue that doesn't affect users running `alembic upgrade`. + +### Peewee to Alembic Transition Issues + +**Background:** Older Open WebUI versions (pre-0.4.x) used Peewee migrations. Current versions use Alembic. + +**Symptoms:** + +- Both `migratehistory` and `alembic_version` tables exist +- Errors about "migration already applied" + +**What happens automatically:** + +1. Open WebUI's `internal/db.py` runs old Peewee migrations first via `handle_peewee_migration()` +2. Then `config.py` runs Alembic migrations via `run_migrations()` +3. Both systems should work transparently + +**If automatic transition fails:** + +```bash title="Terminal - Manual Transition" +# Check if old Peewee migrations exist +sqlite3 /app/backend/data/webui.db "SELECT * FROM migratehistory;" 2>/dev/null + +# If Peewee migrations exist, ensure they completed +# Then run Alembic migrations +cd /app/backend/open_webui +alembic upgrade head +``` + +:::tip +If upgrading from very old Open WebUI versions (< 0.3.x), consider a fresh install with data export/import rather than attempting to migrate the database schema across multiple major version changes. +::: + +## Advanced Operations + +### Generate SQL Without Applying + +For review or audit purposes, generate the SQL that would be executed: + +```bash title="Terminal - Generate Migration SQL" +# Generate SQL for pending migrations +alembic upgrade head --sql > /tmp/migration-plan.sql + +# Review what would be applied +cat /tmp/migration-plan.sql +``` + +**Use cases:** + +- DBA review in enterprise environments +- Understanding what changes will occur +- Debugging migration issues +- Applying migrations in restricted environments + +:::info When to Use This +This is advanced functionality for DBAs or DevOps engineers. Regular users should just run `alembic upgrade head` directly. +::: + +### Offline Migration (No Network) + +If your database server is offline or isolated: + +```bash title="Terminal - Offline Migration Workflow" +# 1. Generate SQL on development machine +alembic upgrade head --sql > upgrade-to-head.sql + +# 2. Transfer SQL file to production +scp upgrade-to-head.sql production-server:/tmp/ + +# 3. On production, apply SQL manually +sqlite3 /app/backend/data/webui.db < /tmp/upgrade-to-head.sql + +# 4. Update alembic_version table manually +sqlite3 /app/backend/data/webui.db \ + "UPDATE alembic_version SET version_num='';" +``` + +:::danger Manual alembic_version Updates +Only update `alembic_version` if you've **actually applied** the corresponding migrations. Lying to Alembic about migration state causes permanent corruption. +::: + +## Recovery Procedures + +### Recovery from Failed Migration + +:::danger SQLite Has No Rollback +SQLite migrations are **non-transactional**. If a migration fails halfway through, your database is in a partially-migrated state. The only safe recovery is restoring from backup. +::: + +**Symptoms of partial migration:** + +- Some tables exist, others don't match expected schema +- Foreign key violations +- Missing columns that migration should have added +- Application errors about missing database fields + +**Recovery steps:** + +```bash title="Terminal - Restore from Backup" +# 1. Stop Open WebUI immediately +docker stop open-webui + +# 2. Verify backup integrity +sqlite3 /path/to/webui.db.backup "PRAGMA integrity_check;" + +# 3. Restore backup +cp /path/to/webui.db.backup /path/to/webui.db + +# 4. Investigate root cause before retrying +docker logs open-webui > migration-failure-logs.txt + +# 5. Get help with logs before attempting migration again +``` + +:::warning Do Not Use "stamp" to Fix Failed Migrations +Never use `alembic stamp` to mark a partially-failed migration as complete. This leaves your database in a corrupt state. +::: + +### Validate Database Integrity + +Before and after migrations, verify your database isn't corrupted: + + + + ```bash title="Terminal - SQLite Integrity Check" + sqlite3 /app/backend/data/webui.db "PRAGMA integrity_check;" + + # Should output: ok + # If it outputs anything else, database is corrupted + ``` + + + ```bash title="Terminal - PostgreSQL Integrity Check" + # Check for table corruption + psql -h localhost -U user -d dbname -c "SELECT * FROM pg_stat_database WHERE datname='open_webui_db';" + + # Vacuum and analyze + psql -h localhost -U user -d dbname -c "VACUUM ANALYZE;" + ``` + + + +## Post-Migration Checklist + +After successful migration, verify: + +- [ ] `alembic current` shows `(head)` indicating latest version +- [ ] Open WebUI starts without errors +- [ ] Can log in successfully +- [ ] Core features work (chat, model selection, etc.) +- [ ] No error messages in logs +- [ ] Data appears intact (users, chats, models) +- [ ] Backup can be safely archived after 1 week of stability + +:::tip Keep Recent Backups +Retain backups from before major migrations for at least 1-2 weeks. Issues sometimes appear days later during specific workflows. +::: + +## Getting Help + +If migrations continue to fail after following this guide: + +**Gather diagnostic information:** + +```bash title="Terminal - Collect Diagnostic Data" +# Version information +docker logs open-webui 2>&1 | head -20 > diagnostics.txt + +# Migration state +cd /app/backend/open_webui +alembic current -v >> diagnostics.txt +alembic history >> diagnostics.txt + +# Database info (SQLite) +sqlite3 /app/backend/data/webui.db ".tables" >> diagnostics.txt +sqlite3 /app/backend/data/webui.db "SELECT * FROM alembic_version;" >> diagnostics.txt + +# Full migration log +alembic upgrade head 2>&1 >> diagnostics.txt +``` + +**Where to get help:** + +1. **Open WebUI GitHub Issues:** https://github.com/open-webui/open-webui/issues + - Search existing issues first + - Include your `diagnostics.txt` file + - Specify your Open WebUI version and installation method + +2. **Open WebUI Discord Community** + - Real-time support from community members + - Share error messages and diagnostics + +3. **Provide this information:** + - Open WebUI version + - Installation method (Docker/local) + - Database type (SQLite/PostgreSQL) + - Output of `alembic current` and `alembic history` + - Complete error messages + - What you were doing when it failed :::note -The application automatically runs migrations on startup via the `run_migrations()` function in `config.py`. Manual migrations are useful for debugging, controlled deployments, or when automatic migration is disabled. +Do not share your `webui.db` database file publicly - it contains user credentials and sensitive data. Only share the diagnostic text output. ::: - -- Some migrations include data transformation logic that may take a significant amount of time to run on large datasets. From 8da0d9f76be2b3951ce4423012bfb3abc32d43d8 Mon Sep 17 00:00:00 2001 From: silentoplayz Date: Fri, 28 Nov 2025 20:49:25 -0500 Subject: [PATCH 06/10] refac --- .../manual-database-migration.md | 103 ++++++++++++------ 1 file changed, 72 insertions(+), 31 deletions(-) diff --git a/docs/troubleshooting/manual-database-migration.md b/docs/troubleshooting/manual-database-migration.md index 23cc31a..a3f2297 100644 --- a/docs/troubleshooting/manual-database-migration.md +++ b/docs/troubleshooting/manual-database-migration.md @@ -126,22 +126,61 @@ Before attempting any fixes, gather information about your database state. -### Run Diagnostic Commands +### Navigate to Alembic Directory and Set Environment -Navigate to the directory containing `alembic.ini`: +Navigate to the directory containing `alembic.ini` and configure required environment variables: -```bash title="Terminal - Navigate to Alembic Directory" +```bash title="Terminal - Navigate and Configure Environment" +# Navigate to Alembic directory cd /app/backend/open_webui # Docker # OR cd /path/to/open-webui/backend/open_webui # Local + +# Verify alembic.ini exists +ls -la alembic.ini ``` +### Set Required Environment Variables + +```bash title="Terminal - Set Environment Variables" +# Required: Database URL +# For Docker with SQLite (4 slashes for absolute path) +export DATABASE_URL="sqlite:////app/backend/data/webui.db" + +# For local install with SQLite (relative path) +export DATABASE_URL="sqlite:///../data/webui.db" + +# For PostgreSQL +export DATABASE_URL="postgresql://user:password@localhost:5432/open_webui_db" + +# Required: WEBUI_SECRET_KEY +# Get from existing file +export WEBUI_SECRET_KEY=$(cat /app/backend/data/.webui_secret_key) + +# If .webui_secret_key doesn't exist, generate one +# export WEBUI_SECRET_KEY=$(python3 -c "import secrets; print(secrets.token_hex(32))") +# echo $WEBUI_SECRET_KEY > /app/backend/data/.webui_secret_key + +# Verify both are set +echo "DATABASE_URL: $DATABASE_URL" +echo "WEBUI_SECRET_KEY: ${WEBUI_SECRET_KEY:0:10}..." +``` + +:::danger Both Variables Required +Alembic commands will fail with `Required environment variable not found` if `WEBUI_SECRET_KEY` is missing. Open WebUI's code imports `env.py` which validates this variable exists before Alembic can even connect to the database. +::: + +:::warning Path Syntax for SQLite + +- `sqlite:////app/...` = 4 slashes total (absolute path: `sqlite://` + `/` + `/app/...`) +- `sqlite:///../data/...` = 3 slashes total (relative path) +::: + +### Run Diagnostic Commands + Execute these read-only diagnostic commands: ```bash title="Terminal - Diagnostics (Safe - Read Only)" -# Verify alembic.ini exists -ls -la alembic.ini - # Check current migration version alembic current -v @@ -194,30 +233,7 @@ Verify what's actually in your database: -## Step 3: Configure Environment - -Set required environment variables for manual Alembic execution. - -```bash title="Terminal - Set Environment Variables" -# Required: Database URL -# For Docker with SQLite (note the 4 slashes for absolute path) -export DATABASE_URL="sqlite:////app/backend/data/webui.db" - -# For local install with SQLite (relative path from open_webui directory) -export DATABASE_URL="sqlite:///../data/webui.db" - -# For PostgreSQL -export DATABASE_URL="postgresql://user:password@localhost:5432/open_webui_db" -``` - -:::warning Path Syntax for SQLite - -- `sqlite:////app/...` = 4 slashes total (absolute path) -- `sqlite:///data/...` = 3 slashes total (relative path) -- The extra slash after `sqlite:///` makes it absolute -::: - -## Step 4: Apply Migrations +## Step 3: Apply Migrations ### Standard Upgrade (Most Common) @@ -275,7 +291,7 @@ alembic downgrade alembic downgrade base ``` -## Step 5: Verify Migration Success +## Step 4: Verify Migration Success After running migrations, confirm everything is correct: @@ -325,6 +341,31 @@ INFO: [main] Open WebUI starting on http://0.0.0.0:8080 ## Troubleshooting +### "Required environment variable not found" + +**Cause:** `WEBUI_SECRET_KEY` environment variable is missing. + +**Solution:** + +```bash title="Terminal - Fix Missing Secret Key" +# Method 1: Use existing key from file +export WEBUI_SECRET_KEY=$(cat /app/backend/data/.webui_secret_key) + +# Method 2: If file doesn't exist, generate new key +export WEBUI_SECRET_KEY=$(python3 -c "import secrets; print(secrets.token_hex(32))") +echo $WEBUI_SECRET_KEY > /app/backend/data/.webui_secret_key + +# Verify it's set +echo "WEBUI_SECRET_KEY: ${WEBUI_SECRET_KEY:0:10}..." + +# Try alembic again +alembic current -v +``` + +:::warning Why This Happens +Open WebUI's `env.py` file imports models, which import `open_webui.env`, which validates that `WEBUI_SECRET_KEY` exists. Without it, Python crashes before Alembic can even connect to the database. +::: + ### "No config file 'alembic.ini' found" **Cause:** You're in the wrong directory. From 5d77524bee737cf42c25c241239d52eed3b5947c Mon Sep 17 00:00:00 2001 From: silentoplayz Date: Fri, 28 Nov 2025 21:20:15 -0500 Subject: [PATCH 07/10] is this what you wanted? (NF reference) --- .../manual-database-migration.md | 219 ++++++++++++++++-- 1 file changed, 204 insertions(+), 15 deletions(-) diff --git a/docs/troubleshooting/manual-database-migration.md b/docs/troubleshooting/manual-database-migration.md index a3f2297..2ca0c60 100644 --- a/docs/troubleshooting/manual-database-migration.md +++ b/docs/troubleshooting/manual-database-migration.md @@ -110,8 +110,12 @@ Before attempting any fixes, gather information about your database state. ghcr.io/open-webui/open-webui:main ``` - :::note Default Directory - When you enter the container, you'll be in `/app`. The Alembic configuration is at `/app/backend/open_webui/alembic.ini`. + :::note Verify Your Location + Check where you are after entering the container: + ```bash + pwd + ``` + The Alembic configuration is at `/app/backend/open_webui/alembic.ini`. Navigate there regardless of your starting directory. ::: @@ -131,24 +135,27 @@ Before attempting any fixes, gather information about your database state. Navigate to the directory containing `alembic.ini` and configure required environment variables: ```bash title="Terminal - Navigate and Configure Environment" -# Navigate to Alembic directory +# First, verify where you are +pwd + +# Navigate to Alembic directory (adjust path if your pwd is different) cd /app/backend/open_webui # Docker # OR cd /path/to/open-webui/backend/open_webui # Local -# Verify alembic.ini exists +# Verify alembic.ini exists in current directory ls -la alembic.ini ``` ### Set Required Environment Variables -```bash title="Terminal - Set Environment Variables" -# Required: Database URL -# For Docker with SQLite (4 slashes for absolute path) -export DATABASE_URL="sqlite:////app/backend/data/webui.db" + + -# For local install with SQLite (relative path) -export DATABASE_URL="sqlite:///../data/webui.db" +```bash title="Terminal - Set Environment Variables (Docker)" +# Required: Database URL +# For SQLite (4 slashes for absolute path) +export DATABASE_URL="sqlite:////app/backend/data/webui.db" # For PostgreSQL export DATABASE_URL="postgresql://user:password@localhost:5432/open_webui_db" @@ -166,6 +173,39 @@ echo "DATABASE_URL: $DATABASE_URL" echo "WEBUI_SECRET_KEY: ${WEBUI_SECRET_KEY:0:10}..." ``` + + + +```bash title="Terminal - Set Environment Variables (Local)" +# Required: Database URL +# For SQLite (relative path from backend/open_webui directory) +export DATABASE_URL="sqlite:///../data/webui.db" + +# For absolute path +export DATABASE_URL="sqlite:////full/path/to/webui.db" + +# For PostgreSQL +export DATABASE_URL="postgresql://user:password@localhost:5432/open_webui_db" + +# Required: WEBUI_SECRET_KEY +# If using .env file, Alembic may not pick it up automatically - export manually +export WEBUI_SECRET_KEY=$(cat ../data/.webui_secret_key) + +# Or if you have it in your environment already +# export WEBUI_SECRET_KEY="your-existing-key" + +# Verify both are set +echo "DATABASE_URL: $DATABASE_URL" +echo "WEBUI_SECRET_KEY: ${WEBUI_SECRET_KEY:0:10}..." +``` + +:::note Local Installation Environment +Local installations often have `DATABASE_URL` in a `.env` file, but Alembic's `env.py` may not automatically load `.env` files. You must explicitly export these variables in your shell before running Alembic commands. +::: + + + + :::danger Both Variables Required Alembic commands will fail with `Required environment variable not found` if `WEBUI_SECRET_KEY` is missing. Open WebUI's code imports `env.py` which validates this variable exists before Alembic can even connect to the database. ::: @@ -190,6 +230,9 @@ alembic heads # List all migration history alembic history +# Show pending migrations (what would be applied) +alembic upgrade head --sql | head -30 + # Check for branching (indicates issues) alembic branches ``` @@ -207,6 +250,7 @@ ae1027a6acf (head) - `alembic current` = what version your database thinks it's at - `alembic heads` = what version the code expects +- `alembic upgrade head --sql` = preview SQL that would be executed (doesn't apply changes) - If `current` is older than `heads`, you have pending migrations - If `current` equals `heads`, your database is up-to-date ::: @@ -259,7 +303,13 @@ INFO [alembic.runtime.migration] Running upgrade abc123 -> def456, add_new_colu :::note "Will assume non-transactional DDL" This is a **normal informational message** for SQLite, not an error. SQLite doesn't support rollback of schema changes, so migrations run without transaction protection. -If the process appears to hang after this message, wait 2-3 minutes - some migrations take time. If it's truly stuck, check for database locks (see troubleshooting). +If the process appears to hang after this message, wait 2-3 minutes - some migrations take time, especially: + +- Migrations that add indexes to large tables (1M+ rows: 1-5 minutes) +- Migrations with data transformations (100K+ rows: 30 seconds to several minutes) +- Migrations that rebuild tables (SQLite doesn't support all ALTER operations) + +For very large databases (10M+ rows), consider running migrations during a maintenance window and monitoring progress with `sqlite3 /path/to/webui.db ".tables"` in another terminal. ::: ### Upgrade to Specific Version @@ -298,13 +348,20 @@ After running migrations, confirm everything is correct: ```bash title="Terminal - Post-Migration Verification" # Verify current version matches expected alembic current - # Should show (head) indicating you're at latest # Example: ae1027a6acf (head) # Confirm no pending migrations alembic upgrade head --sql | head -20 # If output contains only comments or is empty, you're up to date + +# Verify key tables exist (SQLite) +sqlite3 /app/backend/data/webui.db ".tables" | grep -E "user|chat|model" +# Should show user, chat, model tables among others + +# Test a simple query to ensure schema is intact +sqlite3 /app/backend/data/webui.db "SELECT COUNT(*) FROM user;" +# Should return a number, not an error ``` ### Test Application Startup @@ -320,6 +377,9 @@ alembic upgrade head --sql | head -20 # Watch logs for migration confirmation docker logs -f open-webui + + # Look for successful startup, then test in browser + # Navigate to http://localhost:8080 and verify login page loads ``` @@ -328,6 +388,7 @@ alembic upgrade head --sql | head -20 python -m open_webui.main # Watch for successful startup messages + # Test by navigating to http://localhost:8080 ``` @@ -339,6 +400,13 @@ INFO: [db] Database initialization complete INFO: [main] Open WebUI starting on http://0.0.0.0:8080 ``` +**Smoke test after startup:** + +- Can access login page +- Can log in with existing credentials +- Can view chat history +- No JavaScript console errors + ## Troubleshooting ### "Required environment variable not found" @@ -347,7 +415,10 @@ INFO: [main] Open WebUI starting on http://0.0.0.0:8080 **Solution:** -```bash title="Terminal - Fix Missing Secret Key" + + + +```bash title="Terminal - Fix Missing Secret Key (Docker)" # Method 1: Use existing key from file export WEBUI_SECRET_KEY=$(cat /app/backend/data/.webui_secret_key) @@ -362,6 +433,27 @@ echo "WEBUI_SECRET_KEY: ${WEBUI_SECRET_KEY:0:10}..." alembic current -v ``` + + + +```bash title="Terminal - Fix Missing Secret Key (Local)" +# Method 1: Use existing key from file +export WEBUI_SECRET_KEY=$(cat ../data/.webui_secret_key) + +# Method 2: Check if it's in your .env file +grep WEBUI_SECRET_KEY .env +# Then export it: export WEBUI_SECRET_KEY="value-from-env-file" + +# Verify it's set +echo "WEBUI_SECRET_KEY: ${WEBUI_SECRET_KEY:0:10}..." + +# Try alembic again +alembic current -v +``` + + + + :::warning Why This Happens Open WebUI's `env.py` file imports models, which import `open_webui.env`, which validates that `WEBUI_SECRET_KEY` exists. Without it, Python crashes before Alembic can even connect to the database. ::: @@ -373,12 +465,18 @@ Open WebUI's `env.py` file imports models, which import `open_webui.env`, which **Solution:** ```bash title="Terminal" +# Find your container name if not 'open-webui' +docker ps + # Find alembic.ini location find /app -name "alembic.ini" 2>/dev/null # Docker find . -name "alembic.ini" # Local # Navigate to that directory cd /app/backend/open_webui # Most common path + +# Verify you're in the right place +ls -la alembic.ini ``` ### "Target database is not up to date" @@ -443,12 +541,14 @@ You may see advice to run `alembic stamp head` to "fix" version mismatches. **Th `alembic stamp` tells Alembic "pretend this migration was applied" without actually running it. This creates permanent database corruption where Alembic thinks your schema is up-to-date when it isn't. -**Only use `alembic stamp head` if:** +**Only use `alembic stamp ` if:** - You manually created all tables using `create_all()` and need to mark them as migrated - You're a developer initializing a fresh database that matches current schema +- You imported a database backup from another system and need to mark it at the correct revision +- You've manually applied migrations via raw SQL and need to update the version tracking -**Never use it to "fix" migration errors.** +**Never use it to "fix" migration errors or skip failed migrations.** ::: ### Process Hangs After "Will assume non-transactional DDL" @@ -547,8 +647,97 @@ alembic upgrade head If upgrading from very old Open WebUI versions (< 0.3.x), consider a fresh install with data export/import rather than attempting to migrate the database schema across multiple major version changes. ::: +### PostgreSQL Foreign Key Errors + +:::info PostgreSQL Only +This troubleshooting applies only to PostgreSQL databases. SQLite handles foreign keys differently. +::: + +**Symptom:** Errors like `psycopg2.errors.InvalidForeignKey: there is no unique constraint matching given keys for referenced table "user"` + +**Cause:** PostgreSQL requires explicit primary key constraints that were missing in older schema versions. + +**Solution for PostgreSQL:** + +```sql title="PostgreSQL Fix" +-- Connect to your PostgreSQL database +psql -h localhost -U your_user -d open_webui_db + +-- Add missing primary key constraint (PostgreSQL syntax) +ALTER TABLE public."user" ADD CONSTRAINT user_pk PRIMARY KEY (id); + +-- Verify constraint was added +\d+ public."user" +``` + +**Note:** The `public.` schema prefix and quoted `"user"` identifier are PostgreSQL-specific. This SQL will not work on SQLite or MySQL. + ## Advanced Operations +### Production and Multi-Server Deployments + +:::warning Rolling Updates Can Cause Failures +In multi-server deployments, running different code versions simultaneously during rolling updates can cause errors if the new code expects schema changes that haven't been applied yet, or if old code is incompatible with new schema. +::: + +**Recommended deployment strategies:** + + + + +Run migrations as a one-time job before deploying new application code: + +```bash title="Kubernetes Job Example" +# 1. Run migration job +kubectl apply -f migration-job.yaml + +# 2. Wait for completion +kubectl wait --for=condition=complete job/openwebui-migration + +# 3. Deploy new application version +kubectl rollout restart deployment/openwebui +``` + +This ensures schema is updated before any new code runs. + + + + +Take the application offline during migration: + +```bash title="Maintenance Workflow" +# 1. Stop all application instances +docker-compose down + +# 2. Run migrations +docker run --rm -v open-webui:/app/backend/data \ + ghcr.io/open-webui/open-webui:main \ + bash -c "cd /app/backend/open_webui && alembic upgrade head" + +# 3. Start all instances with new code +docker-compose up -d +``` + +Simplest approach but requires downtime. + + + + +Maintain two identical environments and switch traffic after migration: + +```bash title="Blue-Green Workflow" +# 1. Green (new) environment gets migrated database +# 2. Deploy new code to green environment +# 3. Test green environment thoroughly +# 4. Switch traffic from blue to green +# 5. Keep blue as instant rollback option +``` + +Zero downtime but requires double infrastructure. + + + + ### Generate SQL Without Applying For review or audit purposes, generate the SQL that would be executed: From 7b732b64293e79a7a23414df1defd396feb529e0 Mon Sep 17 00:00:00 2001 From: silentoplayz Date: Fri, 28 Nov 2025 23:13:11 -0500 Subject: [PATCH 08/10] Update mcp-notion.mdx --- docs/tutorials/integrations/mcp-notion.mdx | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/tutorials/integrations/mcp-notion.mdx b/docs/tutorials/integrations/mcp-notion.mdx index c344471..ce0111f 100644 --- a/docs/tutorials/integrations/mcp-notion.mdx +++ b/docs/tutorials/integrations/mcp-notion.mdx @@ -255,7 +255,7 @@ By default, users must toggle the tool **ON** in the chat menu. You can configur 4. Check the box for **Notion**. 5. Click **Save & Update**. -## Building a Specialized Notion Agent +## Building a Specialized Notion Agent (Optional) For the most reliable experience, we recommend creating a dedicated "Notion Assistant" model. This allows you to provide a specialized **System Prompt**, a helpful **Knowledge Base**, and quick-start **Prompt Suggestions** that teaches the model how to navigate Notion's structure. From 6d870b26739cb309c56f697ead9345683c8aca93 Mon Sep 17 00:00:00 2001 From: silentoplayz Date: Fri, 28 Nov 2025 23:13:28 -0500 Subject: [PATCH 09/10] Update models.md --- docs/features/workspace/models.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/features/workspace/models.md b/docs/features/workspace/models.md index 78eab8c..add8249 100644 --- a/docs/features/workspace/models.md +++ b/docs/features/workspace/models.md @@ -94,7 +94,7 @@ From the main list view in the `Models` section, click the ellipsis (`...`) next - **Clone**: Create a copy of a model configuration, which will be appended with `-clone`. :::note -You cannot clone a raw Base Model directly; you must create a custom model first before cloning it. +A raw Base Model can be cloned as a custom Workspace model, but it will not clone the raw Base Model itself. ::: - **Copy Link**: Copies a direct URL to the model settings. From 6b8d7c21ec0395510a707f620f9ae6010bda3b72 Mon Sep 17 00:00:00 2001 From: silentoplayz Date: Fri, 28 Nov 2025 23:13:56 -0500 Subject: [PATCH 10/10] Update permissions.md --- docs/features/rbac/permissions.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/docs/features/rbac/permissions.md b/docs/features/rbac/permissions.md index 03acdbe..b82cc6f 100644 --- a/docs/features/rbac/permissions.md +++ b/docs/features/rbac/permissions.md @@ -34,7 +34,7 @@ Chat permissions determine what actions users can perform within chat conversati Features permissions control access to specialized capabilities within Open WebUI: -- **Web Search**: Toggle to allow users to perform web searches during chat sessions. (Environment variable: `ENABLE_RAG_WEB_SEARCH`) +- **Web Search**: Toggle to allow users to perform web searches during chat sessions. (Environment variable: `ENABLE_WEB_SEARCH`) - **Image Generation**: Toggle to allow users to generate images. (Environment variable: `ENABLE_IMAGE_GENERATION`) - **Code Interpreter**: Toggle to allow users to use the code interpreter feature. (Environment variable: `USER_PERMISSIONS_FEATURES_CODE_INTERPRETER`) - **Direct Tool Servers**: Toggle to allow users to connect directly to tool servers. (Environment variable: `USER_PERMISSIONS_FEATURES_DIRECT_TOOL_SERVERS`) @@ -60,7 +60,7 @@ By default, Open WebUI applies the following permission settings: **Features Permissions**: -- Web Search: Enabled (`ENABLE_RAG_WEB_SEARCH=True`) +- Web Search: Enabled (`ENABLE_WEB_SEARCH=True`) - Image Generation: Enabled (`ENABLE_IMAGE_GENERATION=True`) - Code Interpreter: Enabled (`USER_PERMISSIONS_FEATURES_CODE_INTERPRETER`) - Direct Tool Servers: Disabled (`USER_PERMISSIONS_FEATURES_DIRECT_TOOL_SERVERS=False`)