Project

General

Profile

1 8565 leinfelder
/**
2
 * Restore archived documents
3
 */
4
5 8574 leinfelder
/*
6 8576 leinfelder
 * Gather most recent docids that:
7
 * - have access_log event='delete' by the CN
8
 * - are obsoleted by a newer version
9
 * Then we know the current version should be restored
10
 */
11
12
/* Find the most recent version by traversing system metadata
13
 * see: http://www.postgresql.org/docs/8.4/static/queries-with.html
14
 */
15
DROP TABLE IF EXISTS current_documents;
16
WITH RECURSIVE q AS
17
(
18
	SELECT  id.guid, sm.obsoleted_by
19
	FROM access_log al, identifier id, systemmetadata sm
20
	WHERE al.event = 'delete'
21
	AND al.date_logged >= '20140101'
22
	AND al.principal LIKE '%urn:node:CN%'
23
	AND al.docid = id.docid || '.' || id.rev
24
	AND id.guid = sm.guid
25
	AND sm.obsoleted_by IS NOT null
26
UNION ALL
27 8581 leinfelder
	SELECT newer.guid, newer.obsoleted_by
28
	FROM systemMetadata newer
29
	JOIN q
30
	ON q.obsoleted_by = newer.guid
31 8576 leinfelder
)
32
SELECT guid, obsoleted_by
33
INTO current_documents
34
FROM q
35
WHERE obsoleted_by is null
36
ORDER BY guid;
37
38
/**
39
 * Gather the details of the documents to restore
40
 */
41
DROP TABLE IF EXISTS restore_documents;
42 8565 leinfelder
CREATE TABLE restore_documents (
43
	docid VARCHAR(250),
44
	rev INT8,
45
	rootnodeid INT8,
46
	guid text
47
);
48
INSERT INTO restore_documents (
49
	docid,
50
	rev,
51
	rootnodeid,
52
	guid
53
)
54
SELECT
55
	x.docid,
56
	x.rev,
57
	x.rootnodeid,
58
	id.guid
59 8576 leinfelder
FROM current_documents cd,
60 8565 leinfelder
	xml_revisions x,
61 8576 leinfelder
	identifier id
62 8565 leinfelder
WHERE x.docid = id.docid
63
AND x.rev = id.rev
64 8576 leinfelder
AND id.guid = cd.guid;
65 8565 leinfelder
66 8576 leinfelder
-- look at them
67
SELECT *
68
FROM restore_documents;
69 8574 leinfelder
70 8576 leinfelder
--STOP HERE WHEN TESTING
71
72 8577 leinfelder
/* Move xml_nodes_revisions back into xml_nodes for the affected docids
73
 */
74
INSERT INTO xml_nodes
75
	(nodeid, nodeindex, nodetype, nodename, nodeprefix,
76
	nodedata, parentnodeid, rootnodeid, docid, date_created,
77
	date_updated, nodedatanumerical, nodedatadate)
78
SELECT
79
	nodeid, nodeindex, nodetype, nodename, nodeprefix,
80
	nodedata, parentnodeid, x.rootnodeid, x.docid, date_created,
81
	date_updated, nodedatanumerical, nodedatadate
82
FROM xml_nodes_revisions x, restore_documents rd
83
WHERE x.rootnodeid = rd.rootnodeid;
84
85 8565 leinfelder
/* Move xml_revisions back into xml_documents for the affected docids
86
 */
87
INSERT INTO xml_documents
88
	(docid, rootnodeid, docname, doctype,
89
	user_owner, user_updated, date_created, date_updated,
90
	server_location, rev, public_access, catalog_id)
91
SELECT
92 8577 leinfelder
	x.docid, x.rootnodeid, docname, doctype,
93 8565 leinfelder
	user_owner, user_updated , date_created, date_updated,
94 8577 leinfelder
	server_location, x.rev, public_access, catalog_id
95 8565 leinfelder
FROM xml_revisions x, restore_documents rd
96
WHERE x.rootnodeid = rd.rootnodeid;
97
98 8577 leinfelder
/* Remove the records from revisions
99
 * Order matters here because of foreign key constraints
100 8565 leinfelder
 */
101 8577 leinfelder
DELETE FROM xml_revisions x
102
USING restore_documents rd
103 8565 leinfelder
WHERE x.rootnodeid = rd.rootnodeid;
104
105 8577 leinfelder
DELETE FROM xml_nodes_revisions x
106
USING restore_documents rd
107
WHERE x.rootnodeid = rd.rootnodeid;
108
109 8578 leinfelder
/* Ensure ALL previous revisions of docids
110
 * that have been obsoleted_by something else
111 8574 leinfelder
 * do not also have archived=true flag set
112 8565 leinfelder
 * (Avoids encountering this issue again)
113
 */
114 8578 leinfelder
115
/* Check the numbers
116
 */
117
SELECT count(id.guid)
118
FROM xml_revisions x,
119
	identifier id,
120
	systemMetadata sm
121
WHERE x.docid = id.docid
122
AND x.rev = id.rev
123
AND id.guid = sm.guid
124
AND sm.obsoleted_by IS NOT null
125 8601 leinfelder
AND sm.archived = 'true';
126 8578 leinfelder
127 8601 leinfelder
/*Do the update on xml_revisions
128 8578 leinfelder
 */
129 8565 leinfelder
UPDATE systemMetadata sm
130
SET sm.archived = false
131 8578 leinfelder
FROM xml_revisions x,
132 8574 leinfelder
	identifier id
133
WHERE x.docid = id.docid
134 8578 leinfelder
AND x.rev = id.rev
135 8574 leinfelder
AND id.guid = sm.guid
136 8578 leinfelder
AND sm.obsoleted_by IS NOT null
137 8601 leinfelder
AND sm.archived = 'true';
138 8565 leinfelder
139 8601 leinfelder
/**
140
 * Check numbers in xml_documents
141
 */
142
SELECT count(id.guid)
143
FROM xml_documents x,
144
	identifier id,
145
	systemMetadata sm
146
WHERE x.docid = id.docid
147
AND x.rev = id.rev
148
AND id.guid = sm.guid
149
AND sm.obsoleted_by IS NOT null
150
AND sm.archived = 'true';
151
152
/*Do the update on xml_documents
153
 */
154
UPDATE systemMetadata sm
155
SET sm.archived = false
156
FROM xml_documents x,
157
	identifier id
158
WHERE x.docid = id.docid
159
AND x.rev = id.rev
160
AND id.guid = sm.guid
161
AND sm.obsoleted_by IS NOT null
162
AND sm.archived = 'true';
163
164 8565 leinfelder
/* Clean up
165
 */
166 8576 leinfelder
DROP TABLE IF EXISTS current_documents;
167
DROP TABLE IF EXISTS restore_documents;
168 8565 leinfelder
169 8555 leinfelder
/*
170
 * update the database version
171
 */
172
UPDATE db_version SET status=0;
173
174
INSERT INTO db_version (version, status, date_created)
175
  VALUES ('2.4.0', 1, CURRENT_DATE);