Skip to content

Commit 138c283

Browse files
committed
add postgres db type
1 parent 7a8954c commit 138c283

1 file changed

Lines changed: 15 additions & 6 deletions

File tree

src/basic_memory/db.py

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ class DatabaseType(Enum):
3333

3434
MEMORY = auto()
3535
FILESYSTEM = auto()
36+
POSTGRES = auto()
3637

3738
@classmethod
3839
def get_db_url(
@@ -42,7 +43,7 @@ def get_db_url(
4243
4344
Args:
4445
db_path: Path to SQLite database file (ignored for Postgres)
45-
db_type: Type of database (MEMORY or FILESYSTEM)
46+
db_type: Type of database (MEMORY, FILESYSTEM, or POSTGRES)
4647
config: Optional config to check for database backend and URL
4748
4849
Returns:
@@ -52,14 +53,21 @@ def get_db_url(
5253
if config is None:
5354
config = ConfigManager().config
5455

55-
# Check if Postgres backend is configured
56+
# Handle explicit Postgres type
57+
if db_type == cls.POSTGRES:
58+
if not config.database_url:
59+
raise ValueError("DATABASE_URL must be set when using Postgres backend")
60+
logger.info(f"Using Postgres database: {config.database_url}")
61+
return config.database_url
62+
63+
# Check if Postgres backend is configured (for backward compatibility)
5664
if config.database_backend == DatabaseBackend.POSTGRES:
5765
if not config.database_url:
5866
raise ValueError("DATABASE_URL must be set when using Postgres backend")
5967
logger.info(f"Using Postgres database: {config.database_url}")
6068
return config.database_url
6169

62-
# Default to SQLite
70+
# SQLite databases
6371
if db_type == cls.MEMORY:
6472
logger.info("Using in-memory SQLite database")
6573
return "sqlite+aiosqlite://"
@@ -208,7 +216,7 @@ def _create_engine_and_session(
208216
209217
Args:
210218
db_path: Path to database file (used for SQLite, ignored for Postgres)
211-
db_type: Type of database (MEMORY or FILESYSTEM)
219+
db_type: Type of database (MEMORY, FILESYSTEM, or POSTGRES)
212220
213221
Returns:
214222
Tuple of (engine, session_maker)
@@ -218,7 +226,8 @@ def _create_engine_and_session(
218226
logger.debug(f"Creating engine for db_url: {db_url}")
219227

220228
# Delegate to backend-specific engine creation
221-
if config.database_backend == DatabaseBackend.POSTGRES:
229+
# Check explicit POSTGRES type first, then config setting
230+
if db_type == DatabaseType.POSTGRES or config.database_backend == DatabaseBackend.POSTGRES:
222231
engine = _create_postgres_engine(db_url)
223232
else:
224233
engine = _create_sqlite_engine(db_url, db_type)
@@ -341,7 +350,7 @@ async def run_migrations(
341350
# For SQLite: Create FTS5 virtual table
342351
# For Postgres: No-op (tsvector column added by migrations)
343352
# The project_id is not used for init_search_index, so we pass a dummy value
344-
if app_config.database_backend == DatabaseBackend.POSTGRES:
353+
if database_type == DatabaseType.POSTGRES or app_config.database_backend == DatabaseBackend.POSTGRES:
345354
await PostgresSearchRepository(session_maker, 1).init_search_index()
346355
else:
347356
await SQLiteSearchRepository(session_maker, 1).init_search_index()

0 commit comments

Comments
 (0)