|
|
|
|
@ -423,25 +423,17 @@ def job_delete(job_id: int):
|
|
|
|
|
job = Job.query.get_or_404(job_id)
|
|
|
|
|
|
|
|
|
|
try:
|
|
|
|
|
# Collect run ids for FK cleanup in auxiliary tables that may not have ON DELETE CASCADE
|
|
|
|
|
run_ids = []
|
|
|
|
|
mail_message_ids = []
|
|
|
|
|
# Collect run IDs up-front for cleanup across dependent tables
|
|
|
|
|
run_ids = [r.id for r in JobRun.query.filter_by(job_id=job.id).all()]
|
|
|
|
|
|
|
|
|
|
for run in job.runs:
|
|
|
|
|
if run.id is not None:
|
|
|
|
|
run_ids.append(run.id)
|
|
|
|
|
if run.mail_message_id:
|
|
|
|
|
mail_message_ids.append(run.mail_message_id)
|
|
|
|
|
|
|
|
|
|
# Put related mails back into the inbox and unlink from job
|
|
|
|
|
if mail_message_ids:
|
|
|
|
|
msgs = MailMessage.query.filter(MailMessage.id.in_(mail_message_ids)).all()
|
|
|
|
|
# Put any related mails back into the inbox and unlink from job
|
|
|
|
|
msgs = MailMessage.query.filter(MailMessage.job_id == job.id).all()
|
|
|
|
|
for msg in msgs:
|
|
|
|
|
if hasattr(msg, "location"):
|
|
|
|
|
msg.location = "inbox"
|
|
|
|
|
msg.job_id = None
|
|
|
|
|
|
|
|
|
|
# Ensure run_object_links doesn't block job_runs deletion (older schemas may miss ON DELETE CASCADE)
|
|
|
|
|
# Clean up tables that may not have ON DELETE CASCADE in older schemas.
|
|
|
|
|
if run_ids:
|
|
|
|
|
db.session.execute(
|
|
|
|
|
text("DELETE FROM run_object_links WHERE run_id IN :run_ids").bindparams(
|
|
|
|
|
@ -449,14 +441,48 @@ def job_delete(job_id: int):
|
|
|
|
|
),
|
|
|
|
|
{"run_ids": run_ids},
|
|
|
|
|
)
|
|
|
|
|
db.session.execute(
|
|
|
|
|
text("DELETE FROM job_run_review_events WHERE run_id IN :run_ids").bindparams(
|
|
|
|
|
bindparam("run_ids", expanding=True)
|
|
|
|
|
),
|
|
|
|
|
{"run_ids": run_ids},
|
|
|
|
|
)
|
|
|
|
|
db.session.execute(
|
|
|
|
|
text("DELETE FROM ticket_job_runs WHERE job_run_id IN :run_ids").bindparams(
|
|
|
|
|
bindparam("run_ids", expanding=True)
|
|
|
|
|
),
|
|
|
|
|
{"run_ids": run_ids},
|
|
|
|
|
)
|
|
|
|
|
db.session.execute(
|
|
|
|
|
text("DELETE FROM remark_job_runs WHERE job_run_id IN :run_ids").bindparams(
|
|
|
|
|
bindparam("run_ids", expanding=True)
|
|
|
|
|
),
|
|
|
|
|
{"run_ids": run_ids},
|
|
|
|
|
)
|
|
|
|
|
db.session.execute(
|
|
|
|
|
text("DELETE FROM job_objects WHERE job_run_id IN :run_ids").bindparams(
|
|
|
|
|
bindparam("run_ids", expanding=True)
|
|
|
|
|
),
|
|
|
|
|
{"run_ids": run_ids},
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
# Overrides scoped to this job (object overrides)
|
|
|
|
|
db.session.execute(text("UPDATE overrides SET job_id = NULL WHERE job_id = :job_id"), {"job_id": job.id})
|
|
|
|
|
|
|
|
|
|
# Ticket/Remark scopes may reference a specific job
|
|
|
|
|
db.session.execute(text("UPDATE ticket_scopes SET job_id = NULL WHERE job_id = :job_id"), {"job_id": job.id})
|
|
|
|
|
db.session.execute(text("UPDATE remark_scopes SET job_id = NULL WHERE job_id = :job_id"), {"job_id": job.id})
|
|
|
|
|
|
|
|
|
|
# Ensure job_object_links doesn't block jobs deletion (older schemas may miss ON DELETE CASCADE)
|
|
|
|
|
if job.id is not None:
|
|
|
|
|
db.session.execute(
|
|
|
|
|
text("DELETE FROM job_object_links WHERE job_id = :job_id"),
|
|
|
|
|
{"job_id": job.id},
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
# Finally remove runs and the job itself
|
|
|
|
|
if run_ids:
|
|
|
|
|
db.session.execute(text("DELETE FROM job_runs WHERE job_id = :job_id"), {"job_id": job.id})
|
|
|
|
|
|
|
|
|
|
db.session.delete(job)
|
|
|
|
|
db.session.commit()
|
|
|
|
|
flash("Job deleted. Related mails are returned to the inbox.", "success")
|
|
|
|
|
|