Skip to content

Commit

Permalink
Automatically create the schema when starting the SQLAlchemy data sto…
Browse files Browse the repository at this point in the history
…re (#965)

---------

Co-authored-by: Alex Grönholm <alex.gronholm@nextday.fi>
  • Loading branch information
zhu0629 and agronholm authored Sep 22, 2024
1 parent 0bcb319 commit d4d0670
Show file tree
Hide file tree
Showing 2 changed files with 9 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 (PR by @zhu0629)

**4.0.0a5**

Expand Down
7 changes: 7 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 @@ -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))
Expand Down

0 comments on commit d4d0670

Please sign in to comment.