Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ticket 37 - move init.sql from sbl-project repo to user-fi-management #43

Merged
merged 22 commits into from
Nov 6, 2023
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 3 additions & 6 deletions db_revisions/env.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,10 @@
import os
from dotenv import load_dotenv

from logging.config import fileConfig

from sqlalchemy import engine_from_config
from sqlalchemy import pool

from alembic import context
from entities import models
from src.models import Base
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why did we create a new models file when we already have all the models?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oops, I didn't see the existing models, will modify it.


# this is the Alembic Config object, which provides
# access to the values within the .ini file in use.
Expand All @@ -21,8 +18,8 @@
# add your model's MetaData object here
# for 'autogenerate' support
# from myapp import mymodel
# target_metadata = mymodel.Base.metadata
target_metadata = models.Base.metadata

target_metadata = Base.metadata

# other values from the config, defined by the needs of env.py,
# can be acquired:
Expand Down
2 changes: 1 addition & 1 deletion db_revisions/script.py.mako
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,4 @@ def upgrade() -> None:


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.

79 changes: 79 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,79 @@
"""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:
denied_domains = 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.bulk_insert(denied_domains, [{"domain": "gmail.com"}, {"domain": "yahoo.com"}, {"domain": "aol.com"}])

financial_institutions = 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.bulk_insert(
financial_institutions,
[
{"lei": "TEST1LEI", "name": "Test 1"},
{"lei": "TEST2LEI", "name": "Test 2"},
{"lei": "TEST3LEI", "name": "Test 3"},
],
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@lchen-2101 do you still want to keep these test data?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This data was part of init.sql in sbl-project.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

let's remove the test data.

)

financial_institutions_domains = 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)
op.bulk_insert(
financial_institutions_domains,
[
{"domain": "test1.local", "lei": "TEST1LEI"},
{"domain": "test2.local", "lei": "TEST2LEI"},
{"domain": "test3.local", "lei": "TEST3LEI"},
],
)


def downgrade() -> None:
op.drop_table("financial_institutions_domains")

op.drop_table("financial_institutions")

op.drop_table("denied_domains")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can you check if drop_table instruction includes drop_index ?

49 changes: 49 additions & 0 deletions src/models.py
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why is this file created? we already have all the models.

Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy import Column, DateTime, ForeignKey, Index, PrimaryKeyConstraint, String
from sqlalchemy.sql import func

Base = declarative_base()
metadata = Base.metadata


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", 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, 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"),
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", unique=True),
)

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