mirror of https://github.com/stijndcl/didier
Check current migration on startup
parent
9518bbe168
commit
37dd5ba3e8
|
@ -0,0 +1,26 @@
|
||||||
|
import logging
|
||||||
|
|
||||||
|
from alembic import config, script
|
||||||
|
from alembic.runtime import migration
|
||||||
|
|
||||||
|
from database.engine import engine
|
||||||
|
|
||||||
|
|
||||||
|
async def ensure_latest_migration():
|
||||||
|
"""Make sure we are currently on the latest revision, otherwise raise an exception"""
|
||||||
|
alembic_config = config.Config("alembic.ini")
|
||||||
|
alembic_script = script.ScriptDirectory.from_config(alembic_config)
|
||||||
|
|
||||||
|
async with engine.begin() as connection:
|
||||||
|
current_revision = await connection.run_sync(
|
||||||
|
lambda sync_connection: migration.MigrationContext.configure(sync_connection).get_current_revision()
|
||||||
|
)
|
||||||
|
|
||||||
|
alembic_head = alembic_script.get_current_head()
|
||||||
|
|
||||||
|
if current_revision != alembic_head:
|
||||||
|
error_message = (
|
||||||
|
f"Pending migrations (current revision is {current_revision}, while head is at {alembic_head})"
|
||||||
|
)
|
||||||
|
logging.error(error_message)
|
||||||
|
raise RuntimeError(error_message)
|
10
main.py
10
main.py
|
@ -4,6 +4,7 @@ from logging.handlers import RotatingFileHandler
|
||||||
import asyncio
|
import asyncio
|
||||||
|
|
||||||
import settings
|
import settings
|
||||||
|
from database.migrations import ensure_latest_migration
|
||||||
from didier import Didier
|
from didier import Didier
|
||||||
|
|
||||||
|
|
||||||
|
@ -28,7 +29,12 @@ def setup_logging():
|
||||||
logging.getLogger("discord").setLevel(logging.ERROR)
|
logging.getLogger("discord").setLevel(logging.ERROR)
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
async def main():
|
||||||
|
"""Do some setup & checks, and then run the bot"""
|
||||||
setup_logging()
|
setup_logging()
|
||||||
|
await ensure_latest_migration()
|
||||||
|
await run_bot()
|
||||||
|
|
||||||
asyncio.run(run_bot())
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
asyncio.run(main())
|
||||||
|
|
Loading…
Reference in New Issue