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
	SELECT  newer.guid, newer.obsoleted_by
28
	FROM    systemMetadata newer
29
	JOIN    q
30
	ON      q.obsoleted_by = newer.guid
31
)
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
 * but still have current revisions
112 8574 leinfelder
 * do not also have archived=true flag set
113 8565 leinfelder
 * (Avoids encountering this issue again)
114
 */
115 8578 leinfelder
116
/* Check the numbers
117
 */
118
SELECT count(id.guid)
119
FROM xml_revisions x,
120
	identifier id,
121
	systemMetadata sm
122
WHERE x.docid = id.docid
123
AND x.rev = id.rev
124
AND id.guid = sm.guid
125
AND sm.obsoleted_by IS NOT null
126
AND sm.archived = 'true'
127
AND EXISTS (SELECT * from xml_documents xd WHERE xd.docid = x.docid);
128
129
/*Do the update
130
 */
131 8565 leinfelder
UPDATE systemMetadata sm
132
SET sm.archived = false
133 8578 leinfelder
FROM xml_revisions x,
134 8574 leinfelder
	identifier id
135
WHERE x.docid = id.docid
136 8578 leinfelder
AND x.rev = id.rev
137 8574 leinfelder
AND id.guid = sm.guid
138 8578 leinfelder
AND sm.obsoleted_by IS NOT null
139
AND sm.archived = 'true'
140
AND EXISTS (SELECT * from xml_documents xd WHERE xd.docid = x.docid);
141 8565 leinfelder
142
/* Clean up
143
 */
144 8576 leinfelder
DROP TABLE IF EXISTS current_documents;
145
DROP TABLE IF EXISTS restore_documents;
146 8565 leinfelder
147 8555 leinfelder
/*
148
 * update the database version
149
 */
150
UPDATE db_version SET status=0;
151
152
INSERT INTO db_version (version, status, date_created)
153
  VALUES ('2.4.0', 1, CURRENT_DATE);