felicity-lims/felicity/almigrator.py

57 lines
2.1 KiB
Python
Raw Permalink Normal View History

from alembic import command
from alembic.config import Config
from alembic.script import ScriptDirectory
from sqlalchemy import text
2024-09-28 15:29:22 +08:00
from sqlalchemy.engine import CursorResult
from sqlalchemy.ext.asyncio import create_async_engine
from felicity.core.config import ROOT_DIR
from felicity.core.config import settings
class FelicityMigrator:
def __init__(self, config_path: str = f"{ROOT_DIR}/alembic.ini") -> None:
self.alembic_cfg = Config(config_path)
self.database_url = settings.SQLALCHEMY_DATABASE_URI
2024-09-28 15:29:22 +08:00
def upgrade(self, revision: str = "head") -> None:
command.upgrade(self.alembic_cfg, revision)
2024-09-28 15:29:22 +08:00
def downgrade(self, revision: str) -> None:
command.downgrade(self.alembic_cfg, revision)
2024-09-28 15:29:22 +08:00
def create_revision(self, message: str) -> None:
command.revision(self.alembic_cfg, message=message)
2024-09-28 15:29:22 +08:00
def current(self) -> None:
command.current(self.alembic_cfg)
2024-09-28 15:29:22 +08:00
def history(self) -> None:
command.history(self.alembic_cfg)
2024-09-28 15:29:22 +08:00
async def get_current_db_revision(self) -> str | None:
engine = create_async_engine(self.database_url)
async with engine.connect() as connection:
2024-09-28 15:29:22 +08:00
result: CursorResult = await connection.execute(
2024-09-22 23:15:27 +08:00
text("SELECT version_num FROM alembic_version")
)
return result.scalar()
2024-09-28 15:29:22 +08:00
async def get_latest_revision(self) -> str | None:
script = ScriptDirectory.from_config(self.alembic_cfg)
return script.get_current_head()
2024-09-28 15:29:22 +08:00
async def check_for_updates(self) -> None:
# Get current and latest revisions
current_revision = await self.get_current_db_revision()
latest_revision = await self.get_latest_revision()
if current_revision != latest_revision:
print("Database is not up-to-date!")
print(f"Current revision: {current_revision}")
print(f"Latest revision: {latest_revision}")
print("You should run the Alembic upgrade.")
# Optionally, you can call `run_alembic_upgrade()` here to auto-upgrade.
else:
print("Database is up-to-date.")