Skip to content

Commit

Permalink
ticket 37 - made changes for alembic revision script
Browse files Browse the repository at this point in the history
  • Loading branch information
Nargis Sultani committed Oct 18, 2023
1 parent 773cbb0 commit 00dfe44
Show file tree
Hide file tree
Showing 9 changed files with 86 additions and 73 deletions.
1 change: 1 addition & 0 deletions alembic.ini
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# A generic, single database configuration.


[alembic]
# path to migration scripts
script_location = ./db_revisions
Expand Down
31 changes: 0 additions & 31 deletions db/database.py

This file was deleted.

6 changes: 2 additions & 4 deletions db_revisions/env.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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:
Expand Down
3 changes: 2 additions & 1 deletion db_revisions/script.py.mako
Original file line number Diff line number Diff line change
Expand Up @@ -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"}
2 changes: 0 additions & 2 deletions db_revisions/versions/README

This file was deleted.

60 changes: 60 additions & 0 deletions db_revisions/versions/ed7dcc6128bc_create_a_baseline_migrations.py
Original file line number Diff line number Diff line change
@@ -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 ###
14 changes: 0 additions & 14 deletions docker-compose.yml

This file was deleted.

6 changes: 1 addition & 5 deletions src/main.py
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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:
Expand Down
36 changes: 20 additions & 16 deletions db/models.py → src/models.py
Original file line number Diff line number Diff line change
@@ -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'
Expand All @@ -10,33 +13,34 @@ 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'
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"}
Index('ix_denied_domains_domain', "domain", unique=True)

)



def __repr__(self):
return f"domain: {self.domain}"

0 comments on commit 00dfe44

Please sign in to comment.