Skip to content

Commit

Permalink
Added created and last_modified columns to family documents. (#181)
Browse files Browse the repository at this point in the history
  • Loading branch information
katybaulch authored Nov 16, 2023
1 parent 9221f3f commit 80cd88e
Show file tree
Hide file tree
Showing 10 changed files with 343 additions and 26 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -190,3 +190,4 @@ backend/models

# PyCharm
.idea/
alembic/replaceable_objects/**
7 changes: 6 additions & 1 deletion alembic.ini
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ script_location = alembic

# Logging configuration
[loggers]
keys = root,sqlalchemy,alembic
keys = root,sqlalchemy,alembic,alembic_utils

[handlers]
keys = console
Expand All @@ -71,6 +71,11 @@ level = INFO
handlers =
qualname = alembic

[logger_alembic_utils]
level = INFO
handlers =
qualname = alembic_utils

[handler_console]
class = StreamHandler
args = (sys.stderr,)
Expand Down
37 changes: 34 additions & 3 deletions alembic/env.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
import logging
import os
from alembic import context
from logging.config import fileConfig
from sqlalchemy import engine_from_config
from sqlalchemy import pool

from alembic_utils.pg_function import PGFunction
from alembic_utils.pg_trigger import PGTrigger
from alembic_utils.replaceable_entity import register_entities
from sqlalchemy import engine_from_config, pool

from alembic import context
from app.db.models import Base

logger = logging.getLogger(__name__)
Expand All @@ -30,6 +33,31 @@
# my_important_option = config.get_main_option("my_important_option")
# ... etc.

# Add any custom DDL statements here.
last_modified_procedure = PGFunction(
schema="public",
signature="update_last_modified()",
definition="""
RETURNS TRIGGER AS $$
BEGIN
NEW.last_modified = NOW();
RETURN NEW;
END;
$$ language 'plpgsql'
""",
)

last_modified_trigger = PGTrigger(
schema="public",
signature="update_last_modified",
on_entity="public.family_document",
definition="""
BEFORE INSERT OR UPDATE ON public.family_document
FOR EACH ROW
EXECUTE PROCEDURE public.update_last_modified()
""",
)


def get_url():
db_url = os.environ["DATABASE_URL"]
Expand Down Expand Up @@ -103,6 +131,9 @@ def run_migrations_online():
context.run_migrations()


register_entities([last_modified_procedure, last_modified_trigger])


if context.is_offline_mode():
run_migrations_offline()
else:
Expand Down
96 changes: 96 additions & 0 deletions alembic/versions/0020_added_created_and_last_modified_columns_.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
"""
Added created and last modified columns on documents.
Revision ID: 0020
Revises: 0019
Create Date: 2023-11-16 17:01:21.091516
"""
import sqlalchemy as sa
from alembic_utils.pg_function import PGFunction
from alembic_utils.pg_trigger import PGTrigger

from alembic import op

# revision identifiers, used by Alembic.
revision = "0020"
down_revision = "0019"
branch_labels = None
depends_on = None


def upgrade():
# ### commands auto generated by Alembic - please adjust! ###
op.add_column(
"family_document",
sa.Column(
"created",
sa.DateTime(timezone=True),
server_default=sa.text("now()"),
nullable=False,
),
)
op.add_column(
"family_document",
sa.Column("last_modified", sa.DateTime(timezone=True), nullable=False),
)
public_update_last_modified = PGFunction(
schema="public",
signature="update_last_modified()",
definition="""
RETURNS TRIGGER AS $$
BEGIN
NEW.last_modified = NOW();
RETURN NEW;
END;
$$ language 'plpgsql'
""",
)
op.create_entity(public_update_last_modified) # type: ignore

public_family_document_update_last_modified = PGTrigger(
schema="public",
signature="update_last_modified",
on_entity="public.family_document",
definition="""
BEFORE INSERT OR UPDATE ON public.family_document
FOR EACH ROW
EXECUTE PROCEDURE public.update_last_modified()
""",
)
op.create_entity(public_family_document_update_last_modified) # type: ignore
# ### end Alembic commands ###


def downgrade():
# ### commands auto generated by Alembic - please adjust! ###
public_family_document_update_last_modified = PGTrigger(
schema="public",
signature="update_last_modified",
on_entity="public.family_document",
is_constraint=False,
definition="""
BEFORE INSERT OR UPDATE ON public.family_document
FOR EACH ROW
EXECUTE PROCEDURE public.update_last_modified()
""",
)
op.drop_entity(public_family_document_update_last_modified) # type: ignore

public_update_last_modified = PGFunction(
schema="public",
signature="update_last_modified()",
definition="""
RETURNS TRIGGER AS $$
BEGIN
NEW.last_modified = NOW();
RETURN NEW;
END;
$$ language 'plpgsql'
""",
)
op.drop_entity(public_update_last_modified) # type: ignore

op.drop_column("family_document", "last_modified")
op.drop_column("family_document", "created")
# ### end Alembic commands ###
6 changes: 2 additions & 4 deletions app/core/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,9 +68,7 @@
os.getenv("VESPA_SEARCH_MAX_MATCHES_PER_DOC", "20")
)
VESPA_SECRETS_LOCATION: str = os.getenv("VESPA_SECRETS_LOCATION", "/secrets")
VESPA_URL: str = os.environ["VESPA_URL"]
VESPA_URL: str = os.getenv("VESPA_URL", "")

# Shared search config
INDEX_ENCODER_CACHE_FOLDER: str = os.getenv(
"INDEX_ENCODER_CACHE_FOLDER", "/models"
)
INDEX_ENCODER_CACHE_FOLDER: str = os.getenv("INDEX_ENCODER_CACHE_FOLDER", "/models")
10 changes: 10 additions & 0 deletions app/db/models/law_policy/family.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
from app.db.models.app.enum import BaseModelEnum
from app.db.models.document import PhysicalDocument
from app.db.session import Base

from .geography import Geography


Expand Down Expand Up @@ -202,6 +203,15 @@ class FamilyDocument(Base):
)
document_type = sa.Column(sa.ForeignKey(FamilyDocumentType.name), nullable=True)
document_role = sa.Column(sa.ForeignKey(FamilyDocumentRole.name), nullable=True)
created = sa.Column(
sa.DateTime(timezone=True), server_default=sa.func.now(), nullable=False
)
last_modified = sa.Column(
sa.DateTime(timezone=True),
default=sa.func.now(),
onupdate=sa.func.now(),
nullable=False,
)

slugs: list["Slug"] = relationship("Slug", lazy="joined")
physical_document: PhysicalDocument = relationship(
Expand Down
98 changes: 97 additions & 1 deletion poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 80cd88e

Please sign in to comment.