Skip to content

Commit

Permalink
add setting.db_table_prefix variable (#100)
Browse files Browse the repository at this point in the history
## Description
Adding `setting.db_table_prefix` so customers can optionally change all
the table names with a prefix.

## Additional Notes
Allowing customers to use an existing database for storing the MPI
tables comes at a risk of not knowing what existing tables, the customer
might have in their database. To guarantee that the customer doesn't
have any naming conflicts with the tables created by Record Linker, we
can allow for an optional prefix ENV variable, given them the opportunity
to override the table names.
  • Loading branch information
ericbuckley authored Oct 28, 2024
1 parent 2a8acbe commit 59d13a2
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 5 deletions.
4 changes: 4 additions & 0 deletions src/recordlinker/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@ class Settings(pydantic_settings.BaseSettings):
)

db_uri: str = pydantic.Field(description="The URI for the MPI database")
db_table_prefix: str = pydantic.Field(
description="The prefix for all database tables",
default="",
)
test_db_uri: str = pydantic.Field(
description="The URI for the MPI database to run tests against",
default="sqlite:///testdb.sqlite3",
Expand Down
2 changes: 1 addition & 1 deletion src/recordlinker/models/algorithm.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ class AlgorithmPass(Base):

id: orm.Mapped[int] = orm.mapped_column(primary_key=True)
algorithm_id: orm.Mapped[int] = orm.mapped_column(
schema.ForeignKey("algorithm.id", ondelete="CASCADE")
schema.ForeignKey(f"{Algorithm.__tablename__}.id", ondelete="CASCADE")
)
algorithm: orm.Mapped["Algorithm"] = orm.relationship(back_populates="passes")
blocking_keys: orm.Mapped[list[str]] = orm.mapped_column(sqltypes.JSON)
Expand Down
14 changes: 12 additions & 2 deletions src/recordlinker/models/base.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,19 @@
from sqlalchemy import orm
from sqlalchemy import types as sqltypes

from recordlinker.config import settings

class Base(orm.DeclarativeBase):
pass

class PrefixerMeta(orm.DeclarativeMeta):

def __init__(cls, name, bases, dict_):
name = dict_.get("__tablename__")
if name:
cls.__tablename__ = f"{settings.db_table_prefix}{name}"
dict_["__tablename__"] = f"{settings.db_table_prefix}{name}"
return super().__init__(name, bases, dict_)

Base = orm.declarative_base(metaclass=PrefixerMeta)


def get_bigint_pk():
Expand Down
4 changes: 2 additions & 2 deletions src/recordlinker/models/mpi.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ class Patient(Base):
__tablename__ = "mpi_patient"

id: orm.Mapped[int] = orm.mapped_column(get_bigint_pk(), autoincrement=True, primary_key=True)
person_id: orm.Mapped[int] = orm.mapped_column(schema.ForeignKey("mpi_person.id"))
person_id: orm.Mapped[int] = orm.mapped_column(schema.ForeignKey(f"{Person.__tablename__}.id"))
person: orm.Mapped["Person"] = orm.relationship(back_populates="patients")
# NOTE: We're using a protected attribute here to store the data string, as we
# want getter/setter access to the data dictionary to trigger updating the
Expand Down Expand Up @@ -157,7 +157,7 @@ class BlockingValue(Base):
)

id: orm.Mapped[int] = orm.mapped_column(get_bigint_pk(), autoincrement=True, primary_key=True)
patient_id: orm.Mapped[int] = orm.mapped_column(schema.ForeignKey("mpi_patient.id"))
patient_id: orm.Mapped[int] = orm.mapped_column(schema.ForeignKey(f"{Patient.__tablename__}.id"))
patient: orm.Mapped["Patient"] = orm.relationship(back_populates="blocking_values")
blockingkey: orm.Mapped[int] = orm.mapped_column(sqltypes.SmallInteger)
value: orm.Mapped[str] = orm.mapped_column(sqltypes.String(BLOCKING_VALUE_MAX_LENGTH))

0 comments on commit 59d13a2

Please sign in to comment.