Skip to content

πŸ§ͺ SQLAlchemy Databases

trulens_eval.database.sqlalchemy

Attributes

Classes

SQLAlchemyDB

Bases: DB

Database implemented using sqlalchemy.

See abstract class DB for method reference.

Attributes
table_prefix class-attribute instance-attribute
table_prefix: str = DEFAULT_DATABASE_PREFIX

The prefix to use for all table names.

DB interface requirement.

engine_params class-attribute instance-attribute
engine_params: dict = Field(default_factory=dict)

Sqlalchemy-related engine params.

session_params class-attribute instance-attribute
session_params: dict = Field(default_factory=dict)

Sqlalchemy-related session.

engine class-attribute instance-attribute
engine: Optional[Engine] = None

Sqlalchemy engine.

session class-attribute instance-attribute
session: Optional[sessionmaker] = None

Sqlalchemy session(maker).

orm instance-attribute
orm: Type[ORM]

Container of all the ORM classes for this database.

This should be set to a subclass of ORM upon initialization.

Functions
from_tru_args classmethod
from_tru_args(database_url: Optional[str] = None, database_file: Optional[str] = None, database_redact_keys: Optional[bool] = mod_db.DEFAULT_DATABASE_REDACT_KEYS, database_prefix: Optional[str] = mod_db.DEFAULT_DATABASE_PREFIX, **kwargs: Dict[str, Any]) -> SQLAlchemyDB

Process database-related configuration provided to the Tru class to create a database.

Emits warnings if appropriate.

from_db_url classmethod
from_db_url(url: str, **kwargs: Dict[str, Any]) -> SQLAlchemyDB

Create a database for the given url.

PARAMETER DESCRIPTION
url

The database url. This includes database type.

TYPE: str

kwargs

Additional arguments to pass to the database constructor.

TYPE: Dict[str, Any] DEFAULT: {}

RETURNS DESCRIPTION
SQLAlchemyDB

A database instance.

check_db_revision
check_db_revision()
migrate_database
migrate_database(prior_prefix: Optional[str] = None)
reset_database
reset_database()
insert_record
insert_record(record: Record) -> RecordID
get_app
get_app(app_id: AppID) -> Optional[JSONized[App]]

See DB.get_app.

get_apps
get_apps() -> Iterable[JSON]
insert_app
insert_app(app: AppDefinition) -> AppID
delete_app
delete_app(app_id: AppID) -> None

Deletes an app from the database based on its app_id.

PARAMETER DESCRIPTION
app_id

The unique identifier of the app to be deleted.

TYPE: AppID

insert_feedback_definition
insert_feedback_definition(feedback_definition: FeedbackDefinition) -> FeedbackDefinitionID
get_feedback_defs
get_feedback_defs(feedback_definition_id: Optional[FeedbackDefinitionID] = None) -> DataFrame
insert_feedback
insert_feedback(feedback_result: FeedbackResult) -> FeedbackResultID
get_feedback_count_by_status
get_feedback_count_by_status(record_id: Optional[RecordID] = None, feedback_result_id: Optional[FeedbackResultID] = None, feedback_definition_id: Optional[FeedbackDefinitionID] = None, status: Optional[Union[FeedbackResultStatus, Sequence[FeedbackResultStatus]]] = None, last_ts_before: Optional[datetime] = None, offset: Optional[int] = None, limit: Optional[int] = None, shuffle: bool = False) -> Dict[FeedbackResultStatus, int]
get_feedback
get_feedback(record_id: Optional[RecordID] = None, feedback_result_id: Optional[FeedbackResultID] = None, feedback_definition_id: Optional[FeedbackDefinitionID] = None, status: Optional[Union[FeedbackResultStatus, Sequence[FeedbackResultStatus]]] = None, last_ts_before: Optional[datetime] = None, offset: Optional[int] = None, limit: Optional[int] = None, shuffle: Optional[bool] = False) -> DataFrame
get_records_and_feedback
get_records_and_feedback(app_ids: Optional[List[str]] = None) -> Tuple[DataFrame, Sequence[str]]

Functions

trulens_eval.database.orm

Attributes

TYPE_JSON module-attribute

TYPE_JSON = Text

Database type for JSON fields.

TYPE_TIMESTAMP module-attribute

TYPE_TIMESTAMP = Float

Database type for timestamps.

TYPE_ENUM module-attribute

TYPE_ENUM = Text

Database type for enum fields.

TYPE_ID module-attribute

TYPE_ID = VARCHAR(256)

Database type for unique IDs.

Classes

BaseWithTablePrefix

ORM base class except with __tablename__ defined in terms of a base name and a prefix.

A subclass should set _table_base_name and/or _table_prefix. If it does not set both, make sure to set __abstract__ = True. Current design has subclasses set _table_base_name and then subclasses of that subclass setting _table_prefix as in make_orm_for_prefix.

ORM

Bases: ABC, Generic[T]

Abstract definition of a container for ORM classes.

Functions

new_base cached

new_base(prefix: str) -> Type[T]

Create a new base class for ORM classes.

Note: This is a function to be able to define classes extending different SQLAlchemy delcarative bases. Each different such bases has a different set of mappings from classes to table names. If we only had one of these, our code will never be able to have two different sets of mappings at the same time. We need to be able to have multiple mappings for performing things such as database migrations and database copying from one database configuration to another.

new_orm

new_orm(base: Type[T]) -> Type[ORM[T]]

Create a new orm container from the given base table class.

make_base_for_prefix cached

make_base_for_prefix(base: Type[T], table_prefix: str = DEFAULT_DATABASE_PREFIX) -> Type[T]

Create a base class for ORM classes with the given table name prefix.

PARAMETER DESCRIPTION
base

Base class to extend. Should be a subclass of BaseWithTablePrefix.

TYPE: Type[T]

table_prefix

Prefix to use for table names.

TYPE: str DEFAULT: DEFAULT_DATABASE_PREFIX

RETURNS DESCRIPTION
Type[T]

A class that extends base_type and sets the table prefix to table_prefix.

make_orm_for_prefix cached

make_orm_for_prefix(table_prefix: str = DEFAULT_DATABASE_PREFIX) -> Type[ORM[T]]

Make a container for ORM classes.

This is done so that we can use a dynamic table name prefix and make the ORM classes based on that.

PARAMETER DESCRIPTION
table_prefix

Prefix to use for table names.

TYPE: str DEFAULT: DEFAULT_DATABASE_PREFIX