Skip to content

Commit

Permalink
Add create shcmea for sqlalchemy datastore
Browse files Browse the repository at this point in the history
  • Loading branch information
zhu0629 committed Sep 22, 2024
1 parent 0bcb319 commit 3cbebbb
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 0 deletions.
2 changes: 2 additions & 0 deletions docs/versionhistory.rst
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,8 @@ APScheduler, see the :doc:`migration section <migration>`.
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

**4.0.0a5**

Expand Down
8 changes: 8 additions & 0 deletions src/apscheduler/datastores/sqlalchemy.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -231,6 +232,12 @@ async def _begin_transaction(

yield conn

async def _create_schema(self, conn: Connection | AsyncConnection) -> None:
if not self.schema:
return
t = CreateSchema(name=self.schema, if_not_exists=True)
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)
Expand Down Expand Up @@ -389,6 +396,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))
Expand Down

0 comments on commit 3cbebbb

Please sign in to comment.