Create an async SQLAlchemy engine and table classes. Returns: For task schema: (async_engine, table_cls) For experience/sft/dpo schema: (async_engine, meta_cls, blob_cls)
(db_url: str, table_name: str, schema_type: Optional[str])
| 174 | |
| 175 | |
| 176 | async def init_async_engine(db_url: str, table_name: str, schema_type: Optional[str]) -> Tuple: |
| 177 | """Create an async SQLAlchemy engine and table classes. |
| 178 | |
| 179 | Returns: |
| 180 | For task schema: (async_engine, table_cls) |
| 181 | For experience/sft/dpo schema: (async_engine, meta_cls, blob_cls) |
| 182 | """ |
| 183 | from trinity.buffer.utils import to_async_url |
| 184 | |
| 185 | logger = get_logger(__name__) |
| 186 | async_url = to_async_url(db_url) |
| 187 | engine = create_async_engine(async_url, pool_pre_ping=True) |
| 188 | |
| 189 | if schema_type is None: |
| 190 | schema_type = "task" |
| 191 | |
| 192 | classes = _create_table_classes(table_name, schema_type) |
| 193 | |
| 194 | try: |
| 195 | async with engine.begin() as conn: |
| 196 | await conn.run_sync(Base.metadata.create_all) |
| 197 | logger.info(f"Created async tables for {table_name} (schema={schema_type}).") |
| 198 | except OperationalError: |
| 199 | logger.warning(f"Failed to create async tables for {table_name}, assuming they exist.") |
| 200 | |
| 201 | if schema_type == "task": |
| 202 | return engine, classes[0] |
| 203 | return engine, classes[0], classes[1] |
no test coverage detected