diff --git a/docs/versionhistory.rst b/docs/versionhistory.rst index e8eda3c1..9614762c 100644 --- a/docs/versionhistory.rst +++ b/docs/versionhistory.rst @@ -60,6 +60,8 @@ APScheduler, see the :doc:`migration section `. useful and reasonable - Fixed race condition in ``MongoDBDataStore`` that allowed multiple schedulers to acquire the same schedules at once +- Changed ``SQLAlchemyDataStore`` to automatically create the explicitly specified + schema if it's missing (PR by @zhu0629) **4.0.0a5** diff --git a/src/apscheduler/datastores/sqlalchemy.py b/src/apscheduler/datastores/sqlalchemy.py index dd680a87..0c4e8d74 100644 --- a/src/apscheduler/datastores/sqlalchemy.py +++ b/src/apscheduler/datastores/sqlalchemy.py @@ -48,6 +48,7 @@ ) from sqlalchemy.ext.asyncio import AsyncConnection, AsyncEngine, create_async_engine from sqlalchemy.future import Connection, Engine +from sqlalchemy.schema import CreateSchema from sqlalchemy.sql import Executable from sqlalchemy.sql.ddl import DropTable from sqlalchemy.sql.elements import BindParameter, literal @@ -389,6 +390,12 @@ async def start( async for attempt in self._retry(): with attempt: async with self._begin_transaction() as conn: + # Create the schema first if it doesn't exist yet + if self.schema: + await self._execute( + conn, CreateSchema(name=self.schema, if_not_exists=True) + ) + if self.start_from_scratch: for table in self._metadata.sorted_tables: await self._execute(conn, DropTable(table, if_exists=True))