From 00dfe44bf3c4342fa48f0611b0a16538bb75fde1 Mon Sep 17 00:00:00 2001 From: Nargis Sultani Date: Wed, 18 Oct 2023 11:17:57 -0400 Subject: [PATCH] ticket 37 - made changes for alembic revision script --- alembic.ini | 1 + db/database.py | 31 ---------- db_revisions/env.py | 6 +- db_revisions/script.py.mako | 3 +- db_revisions/versions/README | 2 - ...7dcc6128bc_create_a_baseline_migrations.py | 60 +++++++++++++++++++ docker-compose.yml | 14 ----- src/main.py | 6 +- {db => src}/models.py | 36 ++++++----- 9 files changed, 86 insertions(+), 73 deletions(-) delete mode 100644 db/database.py delete mode 100644 db_revisions/versions/README create mode 100644 db_revisions/versions/ed7dcc6128bc_create_a_baseline_migrations.py delete mode 100644 docker-compose.yml rename {db => src}/models.py (58%) diff --git a/alembic.ini b/alembic.ini index d00656c..4a3b106 100644 --- a/alembic.ini +++ b/alembic.ini @@ -1,5 +1,6 @@ # A generic, single database configuration. + [alembic] # path to migration scripts script_location = ./db_revisions diff --git a/db/database.py b/db/database.py deleted file mode 100644 index 84f8168..0000000 --- a/db/database.py +++ /dev/null @@ -1,31 +0,0 @@ -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_revisions/env.py b/db_revisions/env.py index e76784b..36d69c3 100644 --- a/db_revisions/env.py +++ b/db_revisions/env.py @@ -7,7 +7,6 @@ from sqlalchemy import pool from alembic import context -from entities import models # this is the Alembic Config object, which provides # access to the values within the .ini file in use. @@ -21,9 +20,8 @@ # 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 +from src.models import Base +target_metadata = Base.metadata # other values from the config, defined by the needs of env.py, # can be acquired: diff --git a/db_revisions/script.py.mako b/db_revisions/script.py.mako index fbc4b07..ba31c33 100644 --- a/db_revisions/script.py.mako +++ b/db_revisions/script.py.mako @@ -22,5 +22,6 @@ def upgrade() -> None: ${upgrades if upgrades else "pass"} + def downgrade() -> None: - ${downgrades if downgrades else "pass"} + ${downgrades if downgrades else "pass"} \ No newline at end of file diff --git a/db_revisions/versions/README b/db_revisions/versions/README deleted file mode 100644 index 4db807c..0000000 --- a/db_revisions/versions/README +++ /dev/null @@ -1,2 +0,0 @@ -This file added to force git to add `versions` folder. -This file can be removed once revision script is added. \ No newline at end of file diff --git a/db_revisions/versions/ed7dcc6128bc_create_a_baseline_migrations.py b/db_revisions/versions/ed7dcc6128bc_create_a_baseline_migrations.py new file mode 100644 index 0000000..0da9248 --- /dev/null +++ b/db_revisions/versions/ed7dcc6128bc_create_a_baseline_migrations.py @@ -0,0 +1,60 @@ +"""Create a baseline migrations + +Revision ID: ed7dcc6128bc +Revises: +Create Date: 2023-10-18 11:13:57.509078 + +""" +from typing import Sequence, Union + +from alembic import op +import sqlalchemy as sa + + +# revision identifiers, used by Alembic. +revision: str = 'ed7dcc6128bc' +down_revision: Union[str, None] = None +branch_labels: Union[str, Sequence[str], None] = None +depends_on: Union[str, Sequence[str], None] = None + + +def upgrade() -> None: + # ### commands auto generated by Alembic - please adjust! ### + op.create_table('denied_domains', + sa.Column('domain', sa.String(), nullable=False), + sa.Column('event_time', sa.DateTime(timezone=True), server_default=sa.text('now()'), nullable=True), + sa.PrimaryKeyConstraint('domain', name='denied_domains_pkey') + ) + op.create_index('ix_denied_domains_domain', 'denied_domains', ['domain'], unique=True) + op.create_table('financial_institutions', + sa.Column('lei', sa.String(), nullable=False), + sa.Column('name', sa.String(), nullable=False), + sa.Column('event_time', sa.DateTime(timezone=True), server_default=sa.text('now()'), nullable=True), + sa.PrimaryKeyConstraint('lei', name='financial_institutions_pkey') + ) + op.create_index('ix_financial_institutions_lei', 'financial_institutions', ['lei'], unique=True) + op.create_index('ix_financial_institutions_name', 'financial_institutions', ['name'], unique=False) + op.create_table('financial_institutions_domains', + sa.Column('domain', sa.String(), nullable=False), + sa.Column('lei', sa.String(), nullable=False), + sa.Column('event_time', sa.DateTime(timezone=True), server_default=sa.text('now()'), nullable=True), + sa.ForeignKeyConstraint(['lei'], ['financial_institutions.lei'], ), + sa.PrimaryKeyConstraint('domain', 'lei', name='financial_institution_domains_pkey') + ) + op.create_index('ix_financial_institution_domains_domain', 'financial_institutions_domains', ['domain'], unique=False) + op.create_index('ix_financial_institution_domains_lei', 'financial_institutions_domains', ['lei'], unique=False) + # ### end Alembic commands ### + + + +def downgrade() -> None: + # ### commands auto generated by Alembic - please adjust! ### + op.drop_index('ix_financial_institution_domains_lei', table_name='financial_institutions_domains') + op.drop_index('ix_financial_institution_domains_domain', table_name='financial_institutions_domains') + op.drop_table('financial_institutions_domains') + op.drop_index('ix_financial_institutions_name', table_name='financial_institutions') + op.drop_index('ix_financial_institutions_lei', table_name='financial_institutions') + op.drop_table('financial_institutions') + op.drop_index('ix_denied_domains_domain', table_name='denied_domains') + op.drop_table('denied_domains') + # ### end Alembic commands ### \ No newline at end of file diff --git a/docker-compose.yml b/docker-compose.yml deleted file mode 100644 index a83014b..0000000 --- a/docker-compose.yml +++ /dev/null @@ -1,14 +0,0 @@ -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 b9ac815..4e82034 100644 --- a/src/main.py +++ b/src/main.py @@ -1,6 +1,6 @@ import os import logging -from db.database import init_db +import sys import env # noqa: F401 from http import HTTPStatus from fastapi import FastAPI, HTTPException, Request @@ -17,10 +17,6 @@ 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: diff --git a/db/models.py b/src/models.py similarity index 58% rename from db/models.py rename to src/models.py index ed3596d..c1c6db8 100644 --- a/db/models.py +++ b/src/models.py @@ -1,7 +1,10 @@ - -from sqlalchemy import Column, DateTime, ForeignKeyConstraint, Index, PrimaryKeyConstraint, String +from sqlalchemy.ext.declarative import declarative_base +from sqlalchemy import Column, DateTime, ForeignKey, Index, Integer, PrimaryKeyConstraint, String from sqlalchemy.sql import func -from db.database import Base + +Base = declarative_base() +metadata = Base.metadata + class Financial_Institutions(Base): __tablename__ = 'financial_institutions' @@ -10,23 +13,25 @@ class Financial_Institutions(Base): 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"} + Index('ix_financial_institutions_lei', "lei", unique=True), + Index('ix_financial_institutions_name', "name"), ) + def __repr__(self): + return f"lei: {self.lei}, name: {self.name}" class Financial_Institutions_Domains(Base): __tablename__ = 'financial_institutions_domains' domain = Column(String, nullable=False) - lei = Column(String, nullable=False) + lei = Column(String, ForeignKey("financial_institutions.lei")) 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"} + Index('ix_financial_institution_domains_domain', "domain"), + Index('ix_financial_institution_domains_lei', "lei"), + ) + def __repr__(self): + return f"lei: {self.lei}, domain: {self.domain}" class Denied_Domains(Base): __tablename__ = 'denied_domains' @@ -34,9 +39,8 @@ class Denied_Domains(Base): 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"} + Index('ix_denied_domains_domain', "domain", unique=True) + ) - - - \ No newline at end of file + def __repr__(self): + return f"domain: {self.domain}" \ No newline at end of file