diff --git a/db_revisions/env.py b/db_revisions/env.py index fe8291e..811e720 100644 --- a/db_revisions/env.py +++ b/db_revisions/env.py @@ -19,7 +19,8 @@ ENV = os.getenv("ENV", "LOCAL") if ENV == "LOCAL": - load_dotenv("src/.env.local") + file_dir = os.path.dirname(os.path.realpath(__file__)) + load_dotenv(f"{file_dir}/../src/.env.local") else: load_dotenv() diff --git a/db_revisions/versions/045aa502e050_update_financial_institutions_table.py b/db_revisions/versions/045aa502e050_update_financial_institutions_table.py index 9f2aca2..2514df6 100644 --- a/db_revisions/versions/045aa502e050_update_financial_institutions_table.py +++ b/db_revisions/versions/045aa502e050_update_financial_institutions_table.py @@ -64,12 +64,9 @@ def upgrade() -> None: op.create_foreign_key(None, "financial_institutions", "address_state", ["hq_address_state"], ["code"]) op.create_foreign_key(None, "financial_institutions", "hmda_institution_type", ["hmda_institution_type_id"], ["id"]) op.create_foreign_key(None, "financial_institutions", "sbl_institution_type", ["sbl_institution_type_id"], ["id"]) - # ### end Alembic commands ### def downgrade() -> None: - # ### commands auto generated by Alembic - please adjust! ### - op.drop_column("financial_institutions", "top_holder_rssd_id") op.drop_column("financial_institutions", "top_holder_legal_name") op.drop_column("financial_institutions", "top_holder_lei") @@ -86,4 +83,3 @@ def downgrade() -> None: op.drop_column("financial_institutions", "primary_federal_regulator_id") op.drop_column("financial_institutions", "rssd_id") op.drop_column("financial_institutions", "tax_id") - # ### end Alembic commands ### diff --git a/db_revisions/versions/1f6c33f20a2e_create_address_state_table.py b/db_revisions/versions/1f6c33f20a2e_create_address_state_table.py index 621e667..ef568d1 100644 --- a/db_revisions/versions/1f6c33f20a2e_create_address_state_table.py +++ b/db_revisions/versions/1f6c33f20a2e_create_address_state_table.py @@ -19,7 +19,6 @@ def upgrade() -> None: - # ### commands auto generated by Alembic - please adjust! ### op.create_table( "address_state", sa.Column("code", sa.String(length=2), nullable=False), @@ -28,10 +27,7 @@ def upgrade() -> None: sa.PrimaryKeyConstraint("code"), sa.UniqueConstraint("name"), ) - # ### end Alembic commands ### def downgrade() -> None: - # ### commands auto generated by Alembic - please adjust! ### op.drop_table("address_state") - # ### end Alembic commands ### diff --git a/db_revisions/versions/549c612bf1c9_create_federal_regulator_table.py b/db_revisions/versions/549c612bf1c9_create_federal_regulator_table.py index 886f588..7439475 100644 --- a/db_revisions/versions/549c612bf1c9_create_federal_regulator_table.py +++ b/db_revisions/versions/549c612bf1c9_create_federal_regulator_table.py @@ -19,8 +19,6 @@ def upgrade() -> None: - # ### commands auto generated by Alembic - please adjust! ### - op.create_table( "federal_regulator", sa.Column("id", sa.String(length=4), nullable=False), @@ -30,11 +28,6 @@ def upgrade() -> None: sa.UniqueConstraint("name"), ) - # ### end Alembic commands ### - def downgrade() -> None: - # ### commands auto generated by Alembic - please adjust! ### op.drop_table("federal_regulator") - - # ### end Alembic commands ### diff --git a/db_revisions/versions/56ef0b5cd2d4_create_sbl_institution_type_table.py b/db_revisions/versions/56ef0b5cd2d4_create_sbl_institution_type_table.py index 3830e00..634e859 100644 --- a/db_revisions/versions/56ef0b5cd2d4_create_sbl_institution_type_table.py +++ b/db_revisions/versions/56ef0b5cd2d4_create_sbl_institution_type_table.py @@ -19,8 +19,6 @@ def upgrade() -> None: - # ### commands auto generated by Alembic - please adjust! ### - op.create_table( "sbl_institution_type", sa.Column("id", sa.String(), nullable=False), @@ -31,12 +29,6 @@ def upgrade() -> None: ) op.create_index(op.f("ix_sbl_institution_type_id"), "sbl_institution_type", ["id"], unique=False) - # ### end Alembic commands ### - def downgrade() -> None: - # ### commands auto generated by Alembic - please adjust! ### - op.drop_table("sbl_institution_type") - - # ### end Alembic commands ### diff --git a/db_revisions/versions/8b1ba6a3275b_create_hmda_institution_type_table.py b/db_revisions/versions/8b1ba6a3275b_create_hmda_institution_type_table.py index efb0ced..d43f215 100644 --- a/db_revisions/versions/8b1ba6a3275b_create_hmda_institution_type_table.py +++ b/db_revisions/versions/8b1ba6a3275b_create_hmda_institution_type_table.py @@ -19,8 +19,6 @@ def upgrade() -> None: - # ### commands auto generated by Alembic - please adjust! ### - op.create_table( "hmda_institution_type", sa.Column("id", sa.String(), nullable=False), @@ -31,11 +29,6 @@ def upgrade() -> None: ) op.create_index(op.f("ix_hmda_institution_type_id"), "hmda_institution_type", ["id"], unique=False) - # ### end Alembic commands ### - def downgrade() -> None: - # ### commands auto generated by Alembic - please adjust! ### op.drop_table("hmda_institution_type") - - # ### end Alembic commands ### diff --git a/src/main.py b/src/main.py index 7940eb8..0b6fba8 100644 --- a/src/main.py +++ b/src/main.py @@ -1,3 +1,4 @@ +import os import logging from http import HTTPStatus from fastapi import FastAPI, HTTPException, Request @@ -5,6 +6,8 @@ from fastapi.security import OAuth2AuthorizationCodeBearer from fastapi.middleware.cors import CORSMiddleware from starlette.middleware.authentication import AuthenticationMiddleware +from alembic.config import Config +from alembic import command from routers import admin_router, institutions_router @@ -17,6 +20,15 @@ app = FastAPI() +@app.on_event("startup") +async def app_start(): + file_dir = os.path.dirname(os.path.realpath(__file__)) + alembic_cfg = Config(f"{file_dir}/../alembic.ini") + alembic_cfg.set_main_option("script_location", f"{file_dir}/../db_revisions") + alembic_cfg.set_main_option("prepend_sys_path", f"{file_dir}/../") + command.upgrade(alembic_cfg, "head") + + @app.exception_handler(HTTPException) async def http_exception_handler(request: Request, exception: HTTPException) -> JSONResponse: log.error(exception, exc_info=True, stack_info=True)