Sqlite works, made fixes in config so it becomes a basis, added a few mods on top
This commit is contained in:
parent
2bb1da1487
commit
6c5d6b6d8a
1 changed files with 19 additions and 42 deletions
|
|
@ -5,7 +5,7 @@ from sqlalchemy.ext.asyncio import create_async_engine, AsyncSession
|
||||||
from sqlalchemy.orm import declarative_base, sessionmaker
|
from sqlalchemy.orm import declarative_base, sessionmaker
|
||||||
from dotenv import load_dotenv
|
from dotenv import load_dotenv
|
||||||
from cognitive_architecture.config import Config
|
from cognitive_architecture.config import Config
|
||||||
|
config = Config()
|
||||||
|
|
||||||
load_dotenv()
|
load_dotenv()
|
||||||
|
|
||||||
|
|
@ -15,53 +15,30 @@ load_dotenv()
|
||||||
MAX_RETRIES = 3
|
MAX_RETRIES = 3
|
||||||
RETRY_DELAY = 5
|
RETRY_DELAY = 5
|
||||||
|
|
||||||
class DatabaseConfig:
|
def get_sqlalchemy_database_url(db_type='sqlite', db_name=config.db_name, base_path=config.db_path, user=config.db_user, password=config.db_password, host=config.db_host, port=config.db_port):
|
||||||
"""Configuration for the database connection."""
|
"""Get the SQLAlchemy database URL based on parameters."""
|
||||||
def __init__(
|
db_path = (Path(base_path) / db_name).absolute()
|
||||||
self,
|
if db_type == "sqlite":
|
||||||
db_type=None,
|
return f"sqlite+aiosqlite:///{db_path}" # SQLite uses file path
|
||||||
db_name=None,
|
elif db_type == "duckdb":
|
||||||
host=None,
|
return f"duckdb+aiosqlite:///{db_path}"
|
||||||
user=None,
|
elif db_type == "postgresql":
|
||||||
password=None,
|
# Ensure optional parameters are handled gracefully
|
||||||
port=None,
|
port_str = f":{port}" if port else ""
|
||||||
):
|
password_str = f":{password}" if password else ""
|
||||||
self.config = Config()
|
if not all([user, host]):
|
||||||
self.config.load()
|
raise ValueError("User and host are required for PostgreSQL connections.")
|
||||||
self.base_path = Path(self.config.db_path)
|
return f"postgresql+asyncpg://{user}{password_str}@{host}{port_str}/{db_name}"
|
||||||
# Load default values from environment variables or use provided values
|
else:
|
||||||
self.db_type = db_type or self.config.db_type
|
raise ValueError(f"Unsupported DB_TYPE: {db_type}")
|
||||||
self.db_name = db_name or self.config.db_name
|
|
||||||
self.host = host or self.config.db_host
|
|
||||||
self.user = user or self.config.db_user
|
|
||||||
self.password = password or self.config.db_password
|
|
||||||
self.port = port or self.config.db_port
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
def get_sqlalchemy_database_url(self):
|
|
||||||
"""Get the SQLAlchemy database URL based on the configuration."""
|
|
||||||
db_path = (self.base_path / self.db_name).absolute()
|
|
||||||
if self.db_type == "sqlite":
|
|
||||||
return f"sqlite+aiosqlite:///{db_path}" # SQLite uses file path
|
|
||||||
elif self.db_type == "duckdb":
|
|
||||||
return f"duckdb+aiosqlite:///{db_path}"
|
|
||||||
elif self.db_type == "postgresql":
|
|
||||||
# Ensure optional parameters are handled gracefully
|
|
||||||
port_str = f":{self.port}" if self.port else ""
|
|
||||||
password_str = f":{self.password}" if self.password else ""
|
|
||||||
return f"postgresql+asyncpg://{self.user}{password_str}@{self.host}{port_str}/{self.db_name}"
|
|
||||||
else:
|
|
||||||
raise ValueError(f"Unsupported DB_TYPE: {self.db_type}")
|
|
||||||
|
|
||||||
|
|
||||||
# Example usage with a configuration file:
|
# Example usage with a configuration file:
|
||||||
# config = DatabaseConfig(config_file='path/to/config.json')
|
# config = DatabaseConfig(config_file='path/to/config.json')
|
||||||
# Or set them programmatically:
|
# Or set them programmatically:
|
||||||
config = DatabaseConfig(
|
|
||||||
)
|
|
||||||
|
|
||||||
SQLALCHEMY_DATABASE_URL = config.get_sqlalchemy_database_url()
|
|
||||||
|
SQLALCHEMY_DATABASE_URL = get_sqlalchemy_database_url()
|
||||||
|
|
||||||
|
|
||||||
engine = create_async_engine(
|
engine = create_async_engine(
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue