From 773cbb0cbe5c56ab36223a7d66bf0c0ef684caad Mon Sep 17 00:00:00 2001 From: Nargis Sultani Date: Tue, 17 Oct 2023 10:28:24 -0400 Subject: [PATCH] Ticket 37 - move init.sql from sbl-project repo to user-fi-management --- db/database.py | 31 +++++++++++++++++++++++++++++++ db/models.py | 42 ++++++++++++++++++++++++++++++++++++++++++ db_revisions/env.py | 1 + docker-compose.yml | 14 ++++++++++++++ src/main.py | 5 +++++ 5 files changed, 93 insertions(+) create mode 100644 db/database.py create mode 100644 db/models.py create mode 100644 docker-compose.yml diff --git a/db/database.py b/db/database.py new file mode 100644 index 0000000..84f8168 --- /dev/null +++ b/db/database.py @@ -0,0 +1,31 @@ +import os +from dotenv import load_dotenv +from sqlalchemy import create_engine +from sqlalchemy.ext.declarative import declarative_base +from sqlalchemy.orm import sessionmaker + +from sqlalchemy.orm import sessionmaker + +ENV = os.getenv("ENV", "LOCAL") + +if ENV == "LOCAL": + load_dotenv("src/.env.local") +else: + load_dotenv() + +DATABASE_URL = os.environ.get("INST_CONN") + +engine = create_engine(DATABASE_URL) +SessionLocal = sessionmaker(autocommit=False, autoflush=False, bind=engine) + +Base = declarative_base() + +def init_db(): + Base.metadata.create_all(engine) + +def get_db(): + db = SessionLocal() + try: + yield db + except: + db.close() diff --git a/db/models.py b/db/models.py new file mode 100644 index 0000000..ed3596d --- /dev/null +++ b/db/models.py @@ -0,0 +1,42 @@ + +from sqlalchemy import Column, DateTime, ForeignKeyConstraint, Index, PrimaryKeyConstraint, String +from sqlalchemy.sql import func +from db.database import Base + +class Financial_Institutions(Base): + __tablename__ = 'financial_institutions' + lei = Column(String, nullable=False) + name = Column(String, nullable=False) + event_time = Column(DateTime(timezone=True), server_default=func.now()) + __table_args__ = ( + PrimaryKeyConstraint("lei", name="financial_institutions_pkey"), + Index('ix_financial_institutions_lei', "lei", postgresql_using='gin', unique=True), + Index('ix_financial_institutions_name', "name", postgresql_using='gin'), + {"schema": "fi"} + ) + +class Financial_Institutions_Domains(Base): + __tablename__ = 'financial_institutions_domains' + domain = Column(String, nullable=False) + lei = Column(String, nullable=False) + event_time = Column(DateTime(timezone=True), server_default=func.now()) + __table_args__ = ( + PrimaryKeyConstraint("domain", "lei", name="financial_institution_domains_pkey"), + Index('ix_financial_institution_domains_domain', "domain", postgresql_using='gin'), + Index('ix_financial_institution_domains_lei', "lei", postgresql_using='gin'), + ForeignKeyConstraint(["lei"], ["financial_institutions.lei"]), + {"schema": "fi"} + ) + +class Denied_Domains(Base): + __tablename__ = 'denied_domains' + domain = Column(String, nullable=False) + event_time = Column(DateTime(timezone=True), server_default=func.now()) + __table_args__ = ( + PrimaryKeyConstraint("domain", name="denied_domains_pkey"), + Index('ix_denied_domains_domain', "domain", postgresql_using='gin', unique=True), + {"schema": "fi"} + ) + + + \ No newline at end of file diff --git a/db_revisions/env.py b/db_revisions/env.py index 4ddfad5..e76784b 100644 --- a/db_revisions/env.py +++ b/db_revisions/env.py @@ -21,6 +21,7 @@ # add your model's MetaData object here # for 'autogenerate' support # from myapp import mymodel +import db.models as models # target_metadata = mymodel.Base.metadata target_metadata = models.Base.metadata diff --git a/docker-compose.yml b/docker-compose.yml new file mode 100644 index 0000000..a83014b --- /dev/null +++ b/docker-compose.yml @@ -0,0 +1,14 @@ +version: '3' + +services: + pg_user_fi: + image: postgres + ports: + - "5432:5432" + environment: + POSTGRES_USER: fi + POSTGRES_PASSWORD: fi + POSTGRES_DB: financial_institutions + volumes: + - db:/var/lib/postgresql/data + \ No newline at end of file diff --git a/src/main.py b/src/main.py index 1111860..b9ac815 100644 --- a/src/main.py +++ b/src/main.py @@ -1,5 +1,6 @@ import os import logging +from db.database import init_db import env # noqa: F401 from http import HTTPStatus from fastapi import FastAPI, HTTPException, Request @@ -16,6 +17,10 @@ app = FastAPI() +@app.on_event("startup") +def on_startup(): + init_db() + @app.exception_handler(HTTPException) async def http_exception_handler(request: Request, exception: HTTPException) -> JSONResponse: