简体   繁体   English

在没有现有主键的情况下恢复审计表的最佳方法 - postgres

[英]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.表 A 有一个审计表,我需要从中恢复特定列。

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.行从表 A 中删除,然后我重新生成它们并通过在特定时间匹配(这些是约会记录)我使用以下 SQL 找到了表 A 和它的审计表之间的匹配。

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.如您所见,我正在返回元数据和受影响的行的 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.我试过使用 with 子句(report_answers 是上面描述的子查询),但无论我如何编写,我都会不断收到多行返回错误。 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:更新是更新表 B 架构上的“元”列:

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.表 B 中的 1 行对应表 A 中 student_ids 中的每个值。

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'}

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:根据您在问题中发布的查询,如果我理解正确,您想将第一个查询生成的数据更新到表 B 中。假设您的第一个查询工作正常,请尝试以下查询:

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.注意:我认为更新查询不需要表 a 的连接。 you can use it or remove it as per your requirement.您可以根据需要使用或删除它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM