Skip to content

Commit

Permalink
Feature/add last modified field for all entities (#183)
Browse files Browse the repository at this point in the history
  • Loading branch information
katybaulch authored Nov 28, 2023
1 parent 8fb7c41 commit 6f9ce33
Show file tree
Hide file tree
Showing 12 changed files with 699 additions and 66 deletions.
133 changes: 128 additions & 5 deletions alembic/env.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
# Add any custom DDL statements here.
last_modified_procedure = PGFunction(
schema="public",
signature="update_last_modified()",
signature="update_1_last_modified()",
definition="""
RETURNS TRIGGER AS $$
BEGIN
Expand All @@ -47,14 +47,124 @@
""",
)

last_modified_trigger = PGTrigger(
update_family_when_related_entity_updated = PGFunction(
schema="public",
signature="update_2_family_last_modified()",
definition="""
RETURNS TRIGGER AS $$
BEGIN
if tg_op = 'DELETE' then
UPDATE family
SET last_modified = NOW()
WHERE import_id = OLD.family_import_id;
else
UPDATE family
SET last_modified = NOW()
WHERE import_id = NEW.family_import_id;
end if;
RETURN NEW;
END;
$$ language 'plpgsql'
""",
)

update_collection_when_related_entity_updated = PGFunction(
schema="public",
signature="update_2_collection_last_modified()",
definition="""
RETURNS TRIGGER AS $$
BEGIN
if tg_op = 'DELETE' then
UPDATE collection
SET last_modified = NOW()
WHERE import_id = OLD.collection_import_id;
else
UPDATE collection
SET last_modified = NOW()
WHERE import_id = NEW.collection_import_id;
end if;
RETURN NEW;
END;
$$ language 'plpgsql'
""",
)

family_last_modified_trigger = PGTrigger(
schema="public",
signature="update_last_modified",
on_entity="public.family",
definition="""
BEFORE UPDATE ON public.family
FOR EACH ROW
EXECUTE PROCEDURE public.update_1_last_modified()
""",
)


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

family_doc_last_modified_trigger = PGTrigger(
schema="public",
signature="update_family_last_modified",
on_entity="public.family_document",
definition="""
BEFORE INSERT OR UPDATE ON public.family_document
BEFORE INSERT OR UPDATE OR DELETE ON public.family_document
FOR EACH ROW
EXECUTE PROCEDURE public.update_2_family_last_modified()
""",
)

event_last_modified_trigger = PGTrigger(
schema="public",
signature="update_last_modified",
on_entity="public.family_event",
definition="""
BEFORE UPDATE ON public.family_event
FOR EACH ROW
EXECUTE PROCEDURE public.update_last_modified()
EXECUTE PROCEDURE public.update_1_last_modified()
""",
)

family_event_last_modified_trigger = PGTrigger(
schema="public",
signature="update_family_last_modified",
on_entity="public.family_event",
definition="""
BEFORE INSERT OR UPDATE OR DELETE ON public.family_event
FOR EACH ROW
EXECUTE PROCEDURE public.update_2_family_last_modified()
""",
)

collection_last_modified_trigger = PGTrigger(
schema="public",
signature="update_collection_last_modified",
on_entity="public.collection",
definition="""
BEFORE UPDATE ON public.collection
FOR EACH ROW
EXECUTE PROCEDURE public.update_1_last_modified()
""",
)


family_collection_last_modified_trigger = PGTrigger(
schema="public",
signature="update_collection_last_modified ",
on_entity="public.collection_family",
definition="""
BEFORE INSERT OR UPDATE OR DELETE ON public.collection_family
FOR EACH ROW
EXECUTE PROCEDURE public.update_2_collection_last_modified()
""",
)

Expand Down Expand Up @@ -131,7 +241,20 @@ def run_migrations_online():
context.run_migrations()


register_entities([last_modified_procedure, last_modified_trigger])
register_entities(
[
last_modified_procedure,
update_family_when_related_entity_updated,
update_collection_when_related_entity_updated,
doc_last_modified_trigger,
event_last_modified_trigger,
family_last_modified_trigger,
family_doc_last_modified_trigger,
family_event_last_modified_trigger,
collection_last_modified_trigger,
family_collection_last_modified_trigger,
]
)


if context.is_offline_mode():
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,12 @@ def upgrade():
)
op.add_column(
"family_document",
sa.Column("last_modified", sa.DateTime(timezone=True), nullable=False),
sa.Column(
"last_modified",
sa.DateTime(timezone=True),
server_default=sa.text("now()"),
nullable=False,
),
)
public_update_last_modified = PGFunction(
schema="public",
Expand Down
102 changes: 102 additions & 0 deletions alembic/versions/0021_updated_document_triggers_and_last_.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
"""
Updated document triggers and last modified server default
Revision ID: 0021
Revises: 0020
Create Date: 2023-11-22 18:49:04.057129
"""
from alembic_utils.pg_function import PGFunction
from alembic_utils.pg_trigger import PGTrigger

from alembic import op

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


def upgrade():
public_update_1_last_modified = PGFunction(
schema="public",
signature="update_1_last_modified()",
definition="""
RETURNS TRIGGER AS $$
BEGIN
NEW.last_modified = NOW();
RETURN NEW;
END;
$$ language 'plpgsql'""",
)
op.create_entity(public_update_1_last_modified) # type: ignore

public_family_document_update_last_modified = PGTrigger(
schema="public",
signature="update_last_modified",
on_entity="public.family_document",
is_constraint=False,
definition="""
BEFORE UPDATE ON public.family_document
FOR EACH ROW
EXECUTE PROCEDURE public.update_1_last_modified()""",
)
op.replace_entity(public_family_document_update_last_modified) # type: ignore

public_update_last_modified = PGFunction(
schema="public",
signature="update_last_modified()",
definition="""
returns trigger
LANGUAGE plpgsql
AS $function$
BEGIN
NEW.last_modified = NOW();
RETURN NEW;
END;
$function$""",
)
op.drop_entity(public_update_last_modified) # type: ignore


def downgrade():
public_update_last_modified = PGFunction(
schema="public",
signature="update_last_modified()",
definition="""
returns trigger
LANGUAGE plpgsql
AS $function$
BEGIN
NEW.last_modified = NOW();
RETURN NEW;
END;
$function$""",
)
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",
is_constraint=False,
definition="""
BEFORE UPDATE ON public.family_document
FOR EACH ROW
EXECUTE PROCEDURE public.update_last_modified()""",
)
op.replace_entity(public_family_document_update_last_modified) # type: ignore

public_update_1_last_modified = PGFunction(
schema="public",
signature="update_1_last_modified()",
definition="""
RETURNS TRIGGER AS $$
BEGIN
NEW.last_modified = NOW();
RETURN NEW;
END;
$$ language 'plpgsql'""",
)
op.drop_entity(public_update_1_last_modified) # type: ignore
72 changes: 72 additions & 0 deletions alembic/versions/0022_added_audit_trigger_for_events.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
"""
Added audit trigger for events
Revision ID: 0022
Revises: 0021
Create Date: 2023-11-22 22:26:17.873198
"""
import sqlalchemy as sa
from alembic_utils.pg_trigger import PGTrigger

from alembic import op

# revision identifiers, used by Alembic.
revision = "0022"
down_revision = "0021"
branch_labels = None
depends_on = None


def upgrade():
# ### commands auto generated by Alembic - please adjust! ###
op.add_column(
"family_event",
sa.Column(
"created",
sa.DateTime(timezone=True),
server_default=sa.text("now()"),
nullable=False,
),
)
op.add_column(
"family_event",
sa.Column(
"last_modified",
sa.DateTime(timezone=True),
server_default=sa.text("now()"),
nullable=False,
),
)
public_family_event_update_last_modified = PGTrigger(
schema="public",
signature="update_last_modified",
on_entity="public.family_event",
is_constraint=False,
definition="""
BEFORE UPDATE ON public.family_event
FOR EACH ROW
EXECUTE PROCEDURE public.update_1_last_modified()""",
)
op.create_entity(public_family_event_update_last_modified) # type: ignore

# ### end Alembic commands ###


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

op.drop_column("family_event", "last_modified")
op.drop_column("family_event", "created")
# ### end Alembic commands ###
Loading

0 comments on commit 6f9ce33

Please sign in to comment.