Skip to content

feat: add Peewee ORM support for OceanBase as primary database (#12769) - #12926

Merged
yuzhichang merged 1 commit into
infiniflow:mainfrom
NTLx:feature/oceanbase-peewee-support
Jan 31, 2026
Merged

yuzhichang merged 1 commit into
infiniflow:mainfrom
NTLx:feature/oceanbase-peewee-support

Conversation

@NTLx

@NTLx NTLx commented Jan 31, 2026

Copy link
Copy Markdown
Contributor

Summary

This PR adds Peewee ORM support for OceanBase as the primary database in RAGFlow, as requested in issue #12769.

Changes

Core Implementation

  1. RetryingPooledOceanBaseDatabase Class

    • Inherits from PooledMySQLDatabase (OceanBase is MySQL-compatible)
    • Implements retry mechanism for connection issues
    • Handles MySQL-specific error codes (2013, 2006 for connection loss)
    • Provides connection pool management
  2. PooledDatabase Enum

    • Added OCEANBASE = RetryingPooledOceanBaseDatabase
  3. DatabaseLock Enum

    • Added OCEANBASE = MysqlDatabaseLock
    • OceanBase uses MySQL-style locking
  4. TextFieldType Enum

    • Added OCEANBASE = "LONGTEXT"
    • OceanBase uses same text field type as MySQL
  5. DatabaseMigrator Enum

    • Added OCEANBASE = MySQLMigrator
    • OceanBase uses MySQL migration tools

Usage

# Set environment variable to use OceanBase
export DB_TYPE=oceanbase

# Configure connection (in docker/.env or environment)
OCEANBASE_HOST=localhost
OCEANBASE_PORT=2881
OCEANBASE_USER=root
OCEANBASE_PASSWORD=password
OCEANBASE_DATABASE=ragflow

Technical Details

  • Location: api/db/db_models.py
  • Dependencies: No new dependencies (uses existing Peewee MySQL support)
  • Code Size: ~90 lines
  • Difficulty: Simple

Testing

  • Added comprehensive unit tests in tests/unit/test_oceanbase_peewee.py
  • Tests cover:
    • OceanBase database class existence and inheritance
    • Enum values for PooledDatabase, DatabaseLock, TextFieldType
    • Initialization with custom retry settings
    • Environment variable configuration

Acceptance Criteria

✅ Can switch to OceanBase database via DB_TYPE=oceanbase environment variable
✅ All database operations work normally in OceanBase environment
✅ OceanBase uses MySQL compatibility mode (no additional dependencies)

Background

This is part of the RAGFlow + OceanBase Hackathon to allow users to choose OceanBase as RAGFlow's primary database, leveraging OceanBase's high availability and scalability.


Related Issues


Closes #12769

- Add RetryingPooledOceanBaseDatabase class (inherits from PooledMySQLDatabase)
- OceanBase is MySQL compatible, so we use MySQL protocol
- Add OCEANBASE to PooledDatabase enum
- Add OCEANBASE to DatabaseLock enum (uses MysqlDatabaseLock)
- Add OCEANBASE to TextFieldType enum (uses LONGTEXT)
- Add comprehensive unit tests
- Enable via DB_TYPE=oceanbase environment variable

Related to oceanbase/seekdb#123
Closes infiniflow#12769
@dosubot dosubot Bot added size:L This PR changes 100-499 lines, ignoring generated files. 🌈 python Pull requests that update Python code 💞 feature Feature request, pull request that fullfill a new feature. labels Jan 31, 2026
@yuzhichang yuzhichang added the ci Continue Integration label Jan 31, 2026
@yuzhichang
yuzhichang merged commit c4c3f74 into infiniflow:main Jan 31, 2026
@paulhuiseismic

Copy link
Copy Markdown
Contributor

Hi @NTLx quick question here, since we already have the dedicated test folder structure, so curious why we create a separate tests/unit folder again in this pr? cc: @yuzhichang
image

KevinHuSh pushed a commit that referenced this pull request Feb 2, 2026
### What problem does this PR solve?

Fixed the regression issue that unable to start the server as below
details, which was related to this pr
#12926 looks like.
``` Error Trace
Traceback (most recent call last):
  File "/mnt/c/Workspace/ragflow/api/ragflow_server.py", line 33, in <module>
    from api.apps import app
  File "/mnt/c/Workspace/ragflow/api/apps/__init__.py", line 26, in <module>
Traceback (most recent call last):
  File "/mnt/c/Workspace/ragflow/rag/svr/task_executor.py", line 34, in <module>
    from api.db.db_models import close_connection, APIToken
  File "/mnt/c/Workspace/ragflow/api/db/db_models.py", line 49, in <module>
    from api.db.services.knowledgebase_service import KnowledgebaseService
  File "/mnt/c/Workspace/ragflow/api/db/services/__init__.py", line 19, in <module>
    class TextFieldType(Enum):
  File "/mnt/c/Workspace/ragflow/api/db/db_models.py", line 53, in TextFieldType
    from .user_service import UserService as UserService
  File "/mnt/c/Workspace/ragflow/api/db/services/user_service.py", line 24, in <module>
    POSTGRES = "TEXT"
    ^^^^^^^^
  File "/usr/lib/python3.12/enum.py", line 443, in __setitem__
    raise TypeError('%r already defined as %r' % (key, self[key]))
TypeError: 'POSTGRES' already defined as 'TEXT'
    from api.db.db_models import DB, UserTenant
  File "/mnt/c/Workspace/ragflow/api/db/db_models.py", line 49, in <module>
    class TextFieldType(Enum):
  File "/mnt/c/Workspace/ragflow/api/db/db_models.py", line 53, in TextFieldType
    POSTGRES = "TEXT"
    ^^^^^^^^
  File "/usr/lib/python3.12/enum.py", line 443, in __setitem__
    raise TypeError('%r already defined as %r' % (key, self[key]))
TypeError: 'POSTGRES' already defined as 'TEXT'
```

### Type of change

- [x] Bug Fix (non-breaking change which fixes an issue)
KevinHuSh pushed a commit that referenced this pull request Feb 3, 2026
…ck (#12969)

### What problem does this PR solve?

This mistake was made by PR #12926 
This PR makes the OceanBase peewee unit test discoverable by the default
unit test runner/CI (by moving it under test/), so it’s included in the
unified unit test suite.
It also fixes `test_database_lock_enum_values` to correctly handle Enum
alias members (DatabaseLock uses the same value for MYSQL and
OCEANBASE).

### Type of change

- [x] Bug Fix (non-breaking change which fixes an issue)
- [ ] New Feature (non-breaking change which adds functionality)
- [ ] Documentation Update
- [ ] Refactoring
- [ ] Performance Improvement
- [ ] Other (please describe):

### Screenshots
The original `test_oceanbase_peewee.py` was placed under tests/, which
isn’t included in the default unit test runner’s testpaths, so it wasn’t
picked up by the unit test suite. So we need to move it to correct path.
<img width="670" height="540" alt="image"
src="https://github.com/user-attachments/assets/69d39346-450f-46dc-8965-29c3d7b32bc9"
/>

When using old version in `test_oceanbase_peewee.py`:
```
    def test_database_lock_enum_values(self):
        """Test DatabaseLock enum has all expected values."""
        expected = {'MYSQL', 'OCEANBASE', 'POSTGRES'}
        actual = {e.name for e in DatabaseLock}
        assert expected.issubset(actual), f"Missing: {expected - actual}"
```
The old check iterated Enum members, so alias values were skipped and
only `MYSQL/POSTGRES` were seen, making OCEANBASE appear missing.

<img width="1998" height="931" alt="65e2837f23b7b298980a410c7d5c2f09"
src="https://github.com/user-attachments/assets/d8e98c5a-2cfa-4182-ae35-a3ef03554a27"
/>

and new version uses `DatabaseLock.__members__` and passes:
<img width="2024" height="1170" alt="1aa8c6facb28d24149270fe1bc4a9dd9"
src="https://github.com/user-attachments/assets/d8688936-ccac-4a39-a389-23dc6f0fe276"
/>
tmimmanuel pushed a commit to tmimmanuel/ragflow that referenced this pull request May 14, 2026
…2933)

### What problem does this PR solve?

Fixed the regression issue that unable to start the server as below
details, which was related to this pr
infiniflow#12926 looks like.
``` Error Trace
Traceback (most recent call last):
  File "/mnt/c/Workspace/ragflow/api/ragflow_server.py", line 33, in <module>
    from api.apps import app
  File "/mnt/c/Workspace/ragflow/api/apps/__init__.py", line 26, in <module>
Traceback (most recent call last):
  File "/mnt/c/Workspace/ragflow/rag/svr/task_executor.py", line 34, in <module>
    from api.db.db_models import close_connection, APIToken
  File "/mnt/c/Workspace/ragflow/api/db/db_models.py", line 49, in <module>
    from api.db.services.knowledgebase_service import KnowledgebaseService
  File "/mnt/c/Workspace/ragflow/api/db/services/__init__.py", line 19, in <module>
    class TextFieldType(Enum):
  File "/mnt/c/Workspace/ragflow/api/db/db_models.py", line 53, in TextFieldType
    from .user_service import UserService as UserService
  File "/mnt/c/Workspace/ragflow/api/db/services/user_service.py", line 24, in <module>
    POSTGRES = "TEXT"
    ^^^^^^^^
  File "/usr/lib/python3.12/enum.py", line 443, in __setitem__
    raise TypeError('%r already defined as %r' % (key, self[key]))
TypeError: 'POSTGRES' already defined as 'TEXT'
    from api.db.db_models import DB, UserTenant
  File "/mnt/c/Workspace/ragflow/api/db/db_models.py", line 49, in <module>
    class TextFieldType(Enum):
  File "/mnt/c/Workspace/ragflow/api/db/db_models.py", line 53, in TextFieldType
    POSTGRES = "TEXT"
    ^^^^^^^^
  File "/usr/lib/python3.12/enum.py", line 443, in __setitem__
    raise TypeError('%r already defined as %r' % (key, self[key]))
TypeError: 'POSTGRES' already defined as 'TEXT'
```

### Type of change

- [x] Bug Fix (non-breaking change which fixes an issue)
geversite pushed a commit to geversite/ragflow that referenced this pull request Jul 3, 2026
### What problem does this PR solve?

Fixed the regression issue that unable to start the server as below
details, which was related to this pr
infiniflow/ragflow#12926 looks like.
``` Error Trace
Traceback (most recent call last):
  File "/mnt/c/Workspace/ragflow/api/ragflow_server.py", line 33, in <module>
    from api.apps import app
  File "/mnt/c/Workspace/ragflow/api/apps/__init__.py", line 26, in <module>
Traceback (most recent call last):
  File "/mnt/c/Workspace/ragflow/rag/svr/task_executor.py", line 34, in <module>
    from api.db.db_models import close_connection, APIToken
  File "/mnt/c/Workspace/ragflow/api/db/db_models.py", line 49, in <module>
    from api.db.services.knowledgebase_service import KnowledgebaseService
  File "/mnt/c/Workspace/ragflow/api/db/services/__init__.py", line 19, in <module>
    class TextFieldType(Enum):
  File "/mnt/c/Workspace/ragflow/api/db/db_models.py", line 53, in TextFieldType
    from .user_service import UserService as UserService
  File "/mnt/c/Workspace/ragflow/api/db/services/user_service.py", line 24, in <module>
    POSTGRES = "TEXT"
    ^^^^^^^^
  File "/usr/lib/python3.12/enum.py", line 443, in __setitem__
    raise TypeError('%r already defined as %r' % (key, self[key]))
TypeError: 'POSTGRES' already defined as 'TEXT'
    from api.db.db_models import DB, UserTenant
  File "/mnt/c/Workspace/ragflow/api/db/db_models.py", line 49, in <module>
    class TextFieldType(Enum):
  File "/mnt/c/Workspace/ragflow/api/db/db_models.py", line 53, in TextFieldType
    POSTGRES = "TEXT"
    ^^^^^^^^
  File "/usr/lib/python3.12/enum.py", line 443, in __setitem__
    raise TypeError('%r already defined as %r' % (key, self[key]))
TypeError: 'POSTGRES' already defined as 'TEXT'
```

### Type of change

- [x] Bug Fix (non-breaking change which fixes an issue)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

ci Continue Integration 💞 feature Feature request, pull request that fullfill a new feature. 🌈 python Pull requests that update Python code size:L This PR changes 100-499 lines, ignoring generated files.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Feature Request - RAGFlow+OceanBase] Peewee ORM Support for OceanBase as Primary Database

3 participants