diff --git a/src/apscheduler/datastores/sqlalchemy.py b/src/apscheduler/datastores/sqlalchemy.py index 91070220..7ea36690 100644 --- a/src/apscheduler/datastores/sqlalchemy.py +++ b/src/apscheduler/datastores/sqlalchemy.py @@ -37,6 +37,7 @@ false, or_, select, + text, ) from sqlalchemy.engine import URL, Dialect, Result from sqlalchemy.exc import ( @@ -231,6 +232,13 @@ async def _begin_transaction( yield conn + async def _create_schema(self, conn: Connection | AsyncConnection) -> None: + if not self.schema: + return + + t = text(f"CREATE SCHEMA IF NOT EXISTS {self.schema};") + await self._execute(conn, t) + async def _create_metadata(self, conn: Connection | AsyncConnection) -> None: if isinstance(conn, AsyncConnection): await conn.run_sync(self._metadata.create_all) @@ -389,6 +397,7 @@ async def start( async for attempt in self._retry(): with attempt: async with self._begin_transaction() as conn: + await self._create_schema(conn) if self.start_from_scratch: for table in self._metadata.sorted_tables: await self._execute(conn, DropTable(table, if_exists=True))