-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Feature/add last modified field for all entities (#183)
- Loading branch information
1 parent
8fb7c41
commit 6f9ce33
Showing
12 changed files
with
699 additions
and
66 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
102 changes: 102 additions & 0 deletions
102
alembic/versions/0021_updated_document_triggers_and_last_.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 ### |
Oops, something went wrong.