Skip to content

Commit

Permalink
Ticket 37 - move init.sql from sbl-project repo to user-fi-management
Browse files Browse the repository at this point in the history
  • Loading branch information
Nargis Sultani committed Oct 17, 2023
1 parent 0c9ef87 commit 773cbb0
Show file tree
Hide file tree
Showing 5 changed files with 93 additions and 0 deletions.
31 changes: 31 additions & 0 deletions db/database.py
Original file line number Diff line number Diff line change
@@ -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()
42 changes: 42 additions & 0 deletions db/models.py
Original file line number Diff line number Diff line change
@@ -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"}
)



1 change: 1 addition & 0 deletions db_revisions/env.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
14 changes: 14 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
@@ -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

5 changes: 5 additions & 0 deletions src/main.py
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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:
Expand Down

0 comments on commit 773cbb0

Please sign in to comment.