Skip to content
Advertisement

Best way to restoring Audit table without existing primary keys – postgres

Table A has an audit table that I need to restore a specific column from.

Rows were deleted from table A, I then regenerated them and via matching on specific times (these are appointment records) I’ve found the match between table A and it’s audit tables using the following SQL.

select 
    b_aud.meta,
    a.id as a_id,
    b.id as b_id
from a
join b on a.id = b.id
join a_aud on 
    a.course_id = a_aud.course_id and
    a.occurrence_start_date_time = a_aud.occurrence_start_date_time and
    a.occurrence_end_date_time = saa_audoccurrence_end_date_time and
    a.tutor_id = a_aud.tutor_id and
    a.student_ids = a_aud.student_ids and
    a.term_id = a_aud.term_id
join b_aud on 
    b_aud.student_id = b.student_id and
    b_aud.session_id = a.ac2_session_id
where 
    a_aud.audit_action = 'DELETE' and
    a.occurrence_start_date_time <= current_timestamp and
    b_aud.meta::text != '{}'

As you can see, I’m returning the meta and the affected row’s id. I now need to loop through and update each affected row and update the meta, but I’m struggling to write a query that will do that.

I’ve tried using the with clause (report_answers being the subquery described above), but I keep getting multiple rows returned error no matter how I write it. Any tips?

update b b_outer
set meta = (
        select report_answers.meta 
        from report_answers
        join a on
            a.id = report_answers.a_id
        join b on
            b.id = report_answers.b_id
        where 
            b_outer.id = report_answers.b_id
    )
where 
    b.id in (
        select report_answers.b_id 
        from report_answers
    )

The update is to update the column ‘meta’ on table B Schema:

table A:
pk
occ_start_date_time
occ_end_date_time
student_ids
tutor_id
Term

Table B:
pk
FK to table A
student_id 
meta

1 row in table B for each value in student_ids in table A.

Example would be

a:
1 (pk)
'2020-01-01 00:00:00'
'2020-01-01 01:00:00'
[1,2]
1
1

b:
1 (pk) 
1 (fk to a)
1(student_id)
{'note': 'something'}

b:
2 (pk) 
1 (fk to a)
2 (student_id)
{'note': 'something'}

Advertisement

Answer

Based on your queries posted in your question, If I understood correctly you want to update the data generated by first query into table B. Assuming your first query is working fine then try below query:

with report_answers as (
select 
    b_aud.meta,
    a.id as a_id,
    b.id as b_id
from a
join b on a.id = b.id
join a_aud on 
    a.course_id = a_aud.course_id and
    a.occurrence_start_date_time = a_aud.occurrence_start_date_time and
    a.occurrence_end_date_time = saa_audoccurrence_end_date_time and
    a.tutor_id = a_aud.tutor_id and
    a.student_ids = a_aud.student_ids and
    a.term_id = a_aud.term_id
join b_aud on 
    b_aud.student_id = b.student_id and
    b_aud.session_id = a.ac2_session_id
where 
    a_aud.audit_action = 'DELETE' and
    a.occurrence_start_date_time <= current_timestamp and
    b_aud.meta::text != '{}'
)

update b t1
set meta= t2.meta
from report_answers t2 join a t3 on t3.id = t2.a_id
where t1.id=t2.b_id

Note: I don’t think join of Table a is required in update query. you can use it or remove it as per your requirement.

User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement