在没有现有主键的情况下恢复审计 table 的最佳方法 - postgres
Best way to restoring Audit table without existing primary keys - postgres
Table A 有一个审计 table 我需要从中恢复特定列。
行已从 table A 中删除,然后我重新生成它们并通过匹配特定时间(这些是约会记录)我找到了 table A 和它的审计 tables 使用以下 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 != '{}'
如您所见,我正在返回元数据和受影响行的 ID。我现在需要遍历并更新每个受影响的行并更新元,但我正在努力编写一个查询来执行此操作。
我试过使用 with 子句(report_answers 是上面描述的子查询),但是无论我怎么写,我总是收到多行返回错误。有什么建议吗?
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
)
更新是在tableB上更新'meta'列
架构:
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
tableB 中的 1 行 tableA 中 student_ids 中的每个值。
例如
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'}
根据您在问题中发布的查询,如果我理解正确,您想将第一个查询生成的数据更新为 table 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
注意:我不认为 Table a 的连接在更新查询中是必需的。您可以根据需要使用或删除它。
Table A 有一个审计 table 我需要从中恢复特定列。
行已从 table A 中删除,然后我重新生成它们并通过匹配特定时间(这些是约会记录)我找到了 table A 和它的审计 tables 使用以下 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 != '{}'
如您所见,我正在返回元数据和受影响行的 ID。我现在需要遍历并更新每个受影响的行并更新元,但我正在努力编写一个查询来执行此操作。
我试过使用 with 子句(report_answers 是上面描述的子查询),但是无论我怎么写,我总是收到多行返回错误。有什么建议吗?
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
)
更新是在tableB上更新'meta'列 架构:
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
tableB 中的 1 行 tableA 中 student_ids 中的每个值。
例如
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'}
根据您在问题中发布的查询,如果我理解正确,您想将第一个查询生成的数据更新为 table 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
注意:我不认为 Table a 的连接在更新查询中是必需的。您可以根据需要使用或删除它。