Skip to content

Commit 862492c

Browse files
get_page_at_revision is working
1 parent 058ec9d commit 862492c

File tree

4 files changed

+109
-17
lines changed

4 files changed

+109
-17
lines changed

diff.sql

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ begin
3838
--raise notice 'i = %, j = %', i, j;
3939
--raise notice 'hunk = %', hunk;
4040
--raise notice 'context here = %', context;
41-
if i = 1 or j = 1 then
41+
if i = 1 and j = 1 then
4242
-- we're done!
4343
IF in_hunk THEN
4444
context_length := array_length(context, 1);

patch.sql

Lines changed: 61 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ begin
2727
hunk_line := hunk[hunk_ptr];
2828

2929
LOOP
30-
RAISE notice 'op = %, hp = %, ol = %, hl = %', orig_ptr, hunk_ptr, orig_line, hunk_line;
30+
--RAISE notice 'op = %, hp = %, ol = %, hl = %', orig_ptr, hunk_ptr, orig_line, hunk_line;
3131
IF hunk_line IS NULL THEN
3232
-- End of the hunk. Wrap up the rest, and leave.
3333
RETURN result || original[orig_ptr:orig_length];
@@ -76,3 +76,63 @@ begin
7676
end;
7777
$$ language plpgsql
7878
IMMUTABLE;
79+
80+
create or replace function get_page_at_revision(page_id int, revision int)
81+
-- returns page as $$
82+
returns page_latest as $$
83+
#variable_conflict use_variable
84+
declare
85+
latest page_latest;
86+
diff page_diff;
87+
hunk page_diff_hunk;
88+
content text[];
89+
cur_rev int := -1;
90+
line_offset int := 0;
91+
result page_latest;
92+
begin
93+
IF revision < 1 THEN
94+
RAISE 'Revision must be positive (got %)', revision;
95+
END IF;
96+
SELECT * INTO latest FROM page_latest WHERE id = page_id;
97+
IF NOT FOUND THEN
98+
RAISE 'Page % not found', id;
99+
END IF;
100+
IF revision > latest.revision THEN
101+
RAISE 'Revision does not exist (requested %, latest is %)', revision,
102+
latest.revision;
103+
END IF;
104+
IF revision = latest.revision THEN
105+
result := latest::page_latest;
106+
RETURN result;
107+
END IF;
108+
SELECT * INTO diff FROM page_diff AS pd
109+
WHERE pd.page_id = page_id
110+
AND pd.revision = revision;
111+
IF NOT FOUND THEN
112+
RAISE 'Revision % for page % not found', revision, page_id;
113+
END IF;
114+
content = string_to_array(latest.content, E'\n');
115+
FOR hunk IN SELECT * FROM page_diff_hunk AS pdh
116+
WHERE pdh.page_id = page_id
117+
AND pdh.revision >= revision
118+
ORDER BY pdh.revision desc, pdh.start asc
119+
LOOP
120+
IF cur_rev != hunk.revision THEN
121+
line_offset := 0;
122+
cur_rev := hunk.revision;
123+
END IF;
124+
--raise notice 'start = %, offset = %, rev = %', hunk.start, line_offset, cur_rev;
125+
content := apply_hunk(content,
126+
string_to_array(hunk.content, E'\n'),
127+
hunk.start + line_offset,
128+
TRUE);
129+
line_offset := line_offset + hunk.lines_added - hunk.lines_deleted;
130+
END LOOP;
131+
result := (latest.id, latest.title, latest.slug, latest.namespace,
132+
array_to_string(content, E'\n'), diff.comment,
133+
array_length(content, 1), revision, diff.editor, latest.markup,
134+
latest.language, diff.created_on);
135+
RETURN result;
136+
end;
137+
$$ language plpgsql
138+
STABLE STRICT;

tables.sql

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ CREATE TABLE page_latest (
1717
content text DEFAULT '',
1818
comment text DEFAULT '',
1919
num_lines int NOT NULL,
20-
revision int DEFAULT 0 CONSTRAINT "revision must be positive" CHECK (revision >= 0),
20+
revision int DEFAULT 1 CONSTRAINT "revision must be positive" CHECK (revision > 0),
2121
editor int REFERENCES "user"(id),
2222
markup varchar(64) DEFAULT 'plain',
2323
language varchar(8) NOT NULL,
@@ -39,7 +39,7 @@ DROP TABLE IF EXISTS page_diff_hunk;
3939
CREATE TABLE page_diff_hunk (
4040
page_id int,
4141
revision int,
42-
start int NOT NULL CONSTRAINT "start must be positive" CHECK (start >= 0),
42+
start int NOT NULL CONSTRAINT "start must be positive" CHECK (start > 0),
4343
content text NOT NULL DEFAULT '',
4444
lines_added int NOT NULL DEFAULT 0,
4545
lines_deleted int NOT NULL DEFAULT 0,

test.sql

Lines changed: 45 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,50 @@
11
truncate page_latest, page_diff, page_diff_hunk;
22
INSERT INTO user(id, username, email) VALUES (1, 'foo', 'foo');
33
INSERT INTO page_latest(id, title, slug, num_lines, editor, language, content)
4-
VALUES (1, 'page', 'page', 15, 1, 'en-us',
4+
VALUES (1, 'page', 'page', 3, 1, 'en-us',
55
'a
66
b
7+
c');
8+
9+
SELECT update_page(1, 'b
710
c
11+
d', 1);
12+
SELECT update_page(1, 'c
813
d
14+
e', 1);
15+
SELECT update_page(1, 'd
916
e
17+
f', 1);
18+
19+
20+
SELECT update_page(1,
21+
'a
22+
b
23+
c
24+
d
25+
carrot
1026
f
1127
g
1228
h
1329
i
14-
j
30+
u
1531
k
1632
l
1733
m
18-
n
34+
stick
1935
o
20-
p');
36+
p', 1);
2137

38+
INSERT INTO page_latest(id, title, slug, num_lines, editor, language, content)
39+
VALUES (2, 'short', 'short', 3, 1, 'en-us', E'a\nb\nc');
40+
SELECT update_page(2, E'a\nx\nc\nd', 1);
2241

23-
SELECT update_page(1,
24-
'a
42+
43+
select apply_hunk('a
2544
b
2645
c
2746
d
28-
carrot
47+
e
2948
f
3049
g
3150
h
@@ -34,10 +53,23 @@ j
3453
k
3554
l
3655
m
37-
stick
56+
n
3857
o
39-
p', 1);
40-
41-
INSERT INTO page_latest(id, title, slug, num_lines, editor, language, content)
42-
VALUES (2, 'short', 'short', 3, 1, 'en-us', E'a\nb\nc');
43-
SELECT update_page(2, E'a\nx\nc\nd', 1);
58+
p', ' b
59+
c
60+
d
61+
+carrot
62+
-e
63+
f
64+
g
65+
h
66+
i
67+
+u
68+
-j
69+
k
70+
l
71+
m
72+
+stick
73+
-n
74+
o
75+
p', 2);

0 commit comments

Comments
 (0)