Add Alembic to project for database migrations

This commit is contained in:
Lexi / Zoe 2022-04-15 16:10:38 +02:00
parent 7755bfb46d
commit 7ce43a2bfe
Signed by: binaryDiv
GPG key ID: F8D4956E224DA232
15 changed files with 256 additions and 49 deletions

View file

@ -1,12 +1,11 @@
from typing import Optional, cast
from flask import Flask
from sqlalchemy import MetaData, create_engine
from sqlalchemy import create_engine
from sqlalchemy.engine import Engine
from sqlalchemy.orm import Session, scoped_session, sessionmaker
from tofu_api.common.config import Config
from .metadata import metadata_obj
__all__ = [
'SQLAlchemy',
@ -70,22 +69,3 @@ class SQLAlchemy:
# For all further purposes, the scoped session should be treated like a regular Session object.
# Use cast() so we can use Session as the type annotation.
return cast(Session, self._scoped_session)
@property
def metadata(self) -> MetaData:
"""
Database metadata object.
"""
return metadata_obj
def create_all_tables(self) -> None:
"""
Create tables in the database for all models defined in the metadata.
"""
self.metadata.create_all(self.engine)
def drop_all_tables(self) -> None:
"""
Delete tables in the database for all models defined in the metadata.
"""
self.metadata.drop_all(self.engine)