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

added delete_work pg function #2326

Merged
merged 1 commit into from
Jun 11, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
"""delete work pg function

Revision ID: 2220cdfec801
Revises: 7ef1104f2a2d
Create Date: 2024-06-10 19:24:54.977231

"""
from alembic import op
import sqlalchemy as sa


# revision identifiers, used by Alembic.
revision = '2220cdfec801'
down_revision = '7ef1104f2a2d'
branch_labels = None
depends_on = None


def upgrade():
# ### commands auto generated by Alembic - please adjust! ###
with op.batch_alter_table('works', schema=None) as batch_op:
batch_op.drop_constraint('works_current_work_phase_id_fkey', type_='foreignkey')

with op.batch_alter_table('works_history', schema=None) as batch_op:
batch_op.drop_constraint('works_history_current_work_phase_id_fkey', type_='foreignkey')
op.execute("""
CREATE OR REPLACE FUNCTION delete_work(in int) returns void AS $$
declare
calendar_ids int[];
event_configuration_ids int[];
BEGIN
delete from staff_work_roles swr where swr.work_id = $1;
delete from staff_work_roles_history swrh where swrh.work_id = $1;
delete from indigenous_works iw where iw.work_id = $1;
delete from indigenous_works_history iwh where iwh.work_id = $1;
delete from work_issue_updates_history wiu where wiu.work_issue_id in (select id from work_issues where work_id = $1);
delete from work_issue_updates wiu where wiu.work_issue_id in (select id from work_issues where work_id = $1);
delete from work_issues wi where wi.work_id = $1;
delete from work_issues_history wih where wih.work_id = $1;
delete from work_statuses ws where ws.work_id = $1;
delete from work_statuses_history wsh where wsh.work_id = $1;
select array_agg(id) into event_configuration_ids from event_configurations where work_phase_id in (select id from work_phases where work_id = $1);
delete from events_history eh where eh.event_configuration_id = any (event_configuration_ids);
delete from events eh where eh.event_configuration_id = any (event_configuration_ids);
select array_agg(calendar_event_id) into calendar_ids from work_calendar_events where event_configuration_id = any (event_configuration_ids);
raise notice 'Calendar Event Ids %', calendar_ids;
delete from work_calendar_events_history wceh where wceh.event_configuration_id = any (event_configuration_ids);
delete from work_calendar_events wce where wce.event_configuration_id = any (event_configuration_ids);
delete from calendar_events_history ce where ce.id = any(calendar_ids);
delete from calendar_events ce where ce.id = any(calendar_ids);
delete from action_configurations_history where outcome_configuration_id in (select id from outcome_configurations where event_configuration_id = any (event_configuration_ids));
delete from action_configurations where outcome_configuration_id in (select id from outcome_configurations where event_configuration_id = any (event_configuration_ids));
delete from outcome_configurations_history where event_configuration_id = any (event_configuration_ids);
delete from outcome_configurations where event_configuration_id = any (event_configuration_ids);
delete from event_configurations_history ech where ech.work_phase_id in (select id from work_phases wp where wp.work_id = $1);
delete from event_configurations ec where ec.work_phase_id in (select id from work_phases wp where wp.work_id = $1);
delete from work_phases_history wph where wph.work_id = $1;
delete from work_phases wp where wp.work_id = $1;
delete from works_history wh where wh.id = $1;
delete from works where id = $1;
raise notice 'Work with ID % has been deleted', $1;
end;
$$ LANGUAGE plpgsql
""")
# ### end Alembic commands ###


def downgrade():
# ### commands auto generated by Alembic - please adjust! ###
with op.batch_alter_table('works_history', schema=None) as batch_op:
batch_op.create_foreign_key('works_history_current_work_phase_id_fkey', 'work_phases', ['current_work_phase_id'], ['id'])

with op.batch_alter_table('works', schema=None) as batch_op:
batch_op.create_foreign_key('works_current_work_phase_id_fkey', 'work_phases', ['current_work_phase_id'], ['id'])
# ### end Alembic commands ###
10 changes: 7 additions & 3 deletions epictrack-api/src/api/models/work.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ class Work(BaseModelVersioned):
responsible_epd_id = Column(ForeignKey('staffs.id'), nullable=False)
work_lead_id = Column(ForeignKey('staffs.id'), nullable=False)
work_type_id = Column(ForeignKey('work_types.id'), nullable=False)
current_work_phase_id = Column(ForeignKey('work_phases.id'), nullable=True, default=None)
current_work_phase_id = Column(Integer, nullable=True, default=None)
substitution_act_id = Column(ForeignKey('substitution_acts.id'), nullable=True, default=None)
eac_decision_by_id = Column(ForeignKey('staffs.id'), nullable=True)
decision_by_id = Column(ForeignKey('staffs.id'), nullable=False)
Expand All @@ -113,8 +113,12 @@ class Work(BaseModelVersioned):
federal_involvement = relationship('FederalInvolvement', foreign_keys=[federal_involvement_id], lazy='select')
responsible_epd = relationship('Staff', foreign_keys=[responsible_epd_id], lazy='select')
work_lead = relationship('Staff', foreign_keys=[work_lead_id], lazy='select')
work_type = relationship('WorkType', foreign_keys=[work_type_id], lazy='select')
current_work_phase = relationship("WorkPhase", foreign_keys=[current_work_phase_id], lazy='select')
work_type = relationship("WorkType", foreign_keys=[work_type_id], lazy="select")
current_work_phase = relationship(
"WorkPhase",
primaryjoin="WorkPhase.id == foreign(Work.current_work_phase_id)",
lazy="select",
)
substitution_act = relationship("SubstitutionAct", foreign_keys=[substitution_act_id], lazy='select')
eac_decision_by = relationship("Staff", foreign_keys=[eac_decision_by_id], lazy='select')
decision_by = relationship("Staff", foreign_keys=[decision_by_id], lazy='select')
Expand Down
2 changes: 1 addition & 1 deletion epictrack-api/src/api/services/event.py
Original file line number Diff line number Diff line change
Expand Up @@ -1159,7 +1159,7 @@ def _post_process_actions(cls, source_event: Event):
len(all_work_phases) > 0
and work.current_work_phase_id != all_work_phases[0].id
):
work.current_work_phase = all_work_phases[0]
work.current_work_phase_id = all_work_phases[0].id
work.update(work.as_dict(recursive=False), commit=False)

@classmethod
Expand Down
Loading