Skip to content

RAGFlow is an agent-native database: a Retrieval-Augmented Generation (RAG) engine that grew into context infrastructure.

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

NTLxc4c3f744c058
diff --git a/api/db/db_models.py b/api/db/db_models.py
index c9db474b80..95ad570c58 100644
--- a/api/db/db_models.py
+++ b/api/db/db_models.py
@@ -48,6 +48,8 @@ AUTO_DATE_TIMESTAMP_FIELD_PREFIX = {"create", "start", "end", "update", "read_ac
class TextFieldType(Enum):
MYSQL = "LONGTEXT"
+ OCEANBASE = "LONGTEXT"
+ POSTGRES = "TEXT"
POSTGRES = "TEXT"
@@ -383,13 +385,95 @@ class RetryingPooledPostgresqlDatabase(PooledPostgresqlDatabase):
return None
+class RetryingPooledOceanBaseDatabase(PooledMySQLDatabase):
+ """Pooled OceanBase database with retry mechanism.
+
+ OceanBase is compatible with MySQL protocol, so we inherit from PooledMySQLDatabase.
+ This class provides connection pooling and automatic retry for connection issues.
+ """
+ def __init__(self, *args, **kwargs):
+ self.max_retries = kwargs.pop("max_retries", 5)
+ self.retry_delay = kwargs.pop("retry_delay", 1)
+ super().__init__(*args, **kwargs)
+
+ def execute_sql(self, sql, params=None, commit=True):
+ for attempt in range(self.max_retries + 1):
+ try:
+ return super().execute_sql(sql, params, commit)
+ except (OperationalError, InterfaceError) as e:
+ # OceanBase/MySQL specific error codes
+ # 2013: Lost connection to MySQL server during query
+ # 2006: MySQL server has gone away
+ error_codes = [2013, 2006]
+ error_messages = ['', 'Lost connection', 'gone away']
+
+ should_retry = (
+ (hasattr(e, 'args') and e.args and e.args[0] in error_codes) or
+ any(msg in str(e).lower() for msg in error_messages) or
+ (hasattr(e, '__class__') and e.__class__.__name__ == 'InterfaceError')
+ )
+
+ if should_retry and attempt < self.max_retries:
+ logging.warning(
+ f"OceanBase connection issue (attempt {attempt+1}/{self.max_retries}): {e}"
+ )
+ self._handle_connection_loss()
+ time.sleep(self.retry_delay * (2 ** attempt))
+ else:
+ logging.error(f"OceanBase execution failure: {e}")
+ raise
+ return None
+
+ def _handle_connection_loss(self):
+ try:
+ self.close()
+ except Exception:
+ pass
+ try:
+ self.connect()
+ except Exception as e:
+ logging.error(f"Failed to reconnect to OceanBase: {e}")
+ time.sleep(0.1)
+ try:
+ self.connect()
+ except Exception as e2:
+ logging.error(f"Failed to reconnect to OceanBase on second attempt: {e2}")
+ raise
+
+ def begin(self):
+ for attempt in range(self.max_retries + 1):
+ try:
+ return super().begin()
+ except (OperationalError, InterfaceError) as e:
+ error_codes = [2013, 2006]
+ error_messages = ['', 'Lost connection']
+
+ should_retry = (
+ (hasattr(e, 'args') and e.args and e.args[0] in error_codes) or
+ (str(e) in error_messages) or
+ (hasattr(e, '__class__') and e.__class__.__name__ == 'InterfaceError')
+ )
+
+ if should_retry and attempt < self.max_retries:
+ logging.warning(
+ f"Lost connection during transaction (attempt {attempt+1}/{self.max_retries})"
+ )
+ self._handle_connection_loss()
+ time.sleep(self.retry_delay * (2 ** attempt))
+ else:
+ raise
+ return None
+
+
class PooledDatabase(Enum):
MYSQL = RetryingPooledMySQLDatabase
+ OCEANBASE = RetryingPooledOceanBaseDatabase
POSTGRES = RetryingPooledPostgresqlDatabase
class DatabaseMigrator(Enum):
MYSQL = MySQLMigrator
+ OCEANBASE = MySQLMigrator
POSTGRES = PostgresqlMigrator
@@ -548,6 +632,7 @@ class MysqlDatabaseLock:
class DatabaseLock(Enum):
MYSQL = MysqlDatabaseLock
+ OCEANBASE = MysqlDatabaseLock
POSTGRES = PostgresDatabaseLock
diff --git a/tests/unit/test_oceanbase_peewee.py b/tests/unit/test_oceanbase_peewee.py
new file mode 100644
index 0000000000..9156e8dc5c
--- /dev/null
+++ b/tests/unit/test_oceanbase_peewee.py
@@ -0,0 +1,127 @@
+"""
+Tests for OceanBase Peewee ORM support.
+"""
+
+import pytest
+from unittest.mock import Mock, patch, MagicMock
+from api.db.db_models import (
+ RetryingPooledOceanBaseDatabase,
+ PooledDatabase,
+ DatabaseLock,
+ TextFieldType,
+)
+
+
+class TestOceanBaseDatabase:
+ """Test cases for OceanBase database support."""
+
+ def test_oceanbase_database_class_exists(self):
+ """Test that RetryingPooledOceanBaseDatabase class exists."""
+ assert RetryingPooledOceanBaseDatabase is not None
+
+ def test_oceanbase_in_pooled_database_enum(self):
+ """Test that OCEANBASE is in PooledDatabase enum."""
+ assert hasattr(PooledDatabase, 'OCEANBASE')
+ assert PooledDatabase.OCEANBASE.value == RetryingPooledOceanBaseDatabase
+
+ def test_oceanbase_in_database_lock_enum(self):
+ """Test that OCEANBASE is in DatabaseLock enum."""
+ assert hasattr(DatabaseLock, 'OCEANBASE')
+
+ def test_oceanbase_in_text_field_type_enum(self):
+ """Test that OCEANBASE is in TextFieldType enum."""
+ assert hasattr(TextFieldType, 'OCEANBASE')
+ # OceanBase should use LONGTEXT like MySQL
+ assert TextFieldType.OCEANBASE.value == "LONGTEXT"
+
+ def test_oceanbase_database_inherits_mysql(self):
+ """Test that OceanBase database inherits from PooledMySQLDatabase."""
+ from playhouse.pool import PooledMySQLDatabase
+ assert issubclass(RetryingPooledOceanBaseDatabase, PooledMySQLDatabase)
+
+ def test_oceanbase_database_init(self):
+ """Test OceanBase database initialization."""
+ db = RetryingPooledOceanBaseDatabase(
+ "test_db",
+ host="localhost",
+ port=2881,
+ user="root",
+ password="password",
+ )
+ assert db is not None
+ assert db.max_retries == 5 # default value
+ assert db.retry_delay == 1 # default value
+
+ def test_oceanbase_database_custom_retries(self):
+ """Test OceanBase database with custom retry settings."""
+ db = RetryingPooledOceanBaseDatabase(
+ "test_db",
+ host="localhost",
+ max_retries=10,
+ retry_delay=2,
+ )
+ assert db.max_retries == 10
+ assert db.retry_delay == 2
+
+ def test_pooled_database_enum_values(self):
+ """Test PooledDatabase enum has all expected values."""
+ expected = {'MYSQL', 'OCEANBASE', 'POSTGRES'}
+ actual = {e.name for e in PooledDatabase}
+ assert expected.issubset(actual), f"Missing: {expected - actual}"
+
+ 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}"
+
+
+class TestOceanBaseConfiguration:
+ """Test cases for OceanBase configuration via environment variables."""
+
+ def test_settings_default_to_mysql(self):
+ """Test that default DB_TYPE is mysql."""
+ import os
+ # Save original value
+ original = os.environ.get('DB_TYPE')
+
+ try:
+ # Remove DB_TYPE to test default
+ if 'DB_TYPE' in os.environ:
+ del os.environ['DB_TYPE']
+
+ # Reload settings
+ from common import settings
+ settings.DATABASE_TYPE = os.getenv("DB_TYPE", "mysql")
+
+ assert settings.DATABASE_TYPE == "mysql"
+ finally:
+ # Restore original value
+ if original:
+ os.environ['DB_TYPE'] = original
+
+ def test_settings_can_use_oceanbase(self):
+ """Test that DB_TYPE can be set to oceanbase."""
+ import os
+ # Save original value
+ original = os.environ.get('DB_TYPE')
+
+ try:
+ os.environ['DB_TYPE'] = 'oceanbase'
+
+ # Reload settings
+ from common import settings
+ settings.DATABASE_TYPE = os.getenv("DB_TYPE", "mysql")
+
+ assert settings.DATABASE_TYPE == "oceanbase"
+ finally:
+ # Restore original value
+ if original:
+ os.environ['DB_TYPE'] = original
+ else:
+ if 'DB_TYPE' in os.environ:
+ del os.environ['DB_TYPE']
+
+
+if __name__ == "__main__":
+ pytest.main([__file__, "-v"])