Skip to content

Commit 07cd91e

Browse files
Put everything into a 'wiki' schema
1 parent 65bdc70 commit 07cd91e

File tree

4 files changed

+55
-54
lines changed

4 files changed

+55
-54
lines changed

diff.sql

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
1-
create or replace function update_page(page_id int, new_content text,
2-
editor_id int, new_comment text DEFAULT '', context_len int DEFAULT 3) returns int as $$
1+
create or replace function wiki.update_page(page_id int, new_content text,
2+
editor_id int, new_comment text DEFAULT '', context_len int DEFAULT 3)
3+
returns int as $$
34
declare
4-
latest page_latest;
5+
latest wiki.page_latest;
56
new_revision int;
67
hunk text[];
78
context text[]; -- only contains consecutive lines in LCS
@@ -18,14 +19,14 @@ declare
1819
i int;
1920
j int;
2021
begin
21-
SELECT * INTO latest FROM page_latest WHERE id = page_id;
22+
SELECT * INTO latest FROM wiki.page_latest WHERE id = page_id;
2223
IF NOT FOUND THEN
2324
RAISE 'Page % not found', id;
2425
END IF;
2526
new_revision := latest.revision + 1;
2627
--raise notice 'new revision: %', new_revision;
2728
-- write out new diff object
28-
INSERT INTO page_diff (page_id, revision, editor, comment)
29+
INSERT INTO wiki.page_diff (page_id, revision, editor, comment)
2930
VALUES (page_id, latest.revision, latest.editor, latest.comment);
3031
-- make hunks
3132
X := string_to_array(latest.content, E'\n');
@@ -55,14 +56,14 @@ begin
5556
hunk_lines_context := hunk_lines_context + context_length;
5657
END IF;
5758
-- write out the last hunk
58-
INSERT INTO page_diff_hunk (page_id, revision, start,
59+
INSERT INTO wiki.page_diff_hunk (page_id, revision, start,
5960
content, lines_added, lines_deleted, lines_context)
6061
VALUES
6162
(page_id, latest.revision, i, array_to_string(hunk, E'\n'),
6263
hunk_lines_added, hunk_lines_deleted, hunk_lines_context);
6364
END IF;
6465
-- update the page_latest object
65-
UPDATE page_latest SET content = new_content, revision = new_revision,
66+
UPDATE wiki.page_latest SET content = new_content, revision = new_revision,
6667
num_lines = array_length(Y, 1), comment = new_comment,
6768
editor = editor_id, edited_on = now()
6869
WHERE id = page_id;
@@ -97,7 +98,7 @@ begin
9798
hunk := context[context_len+1:2*context_len] || hunk;
9899
hunk_lines_context = hunk_lines_context + context_len;
99100
-- write out the hunk
100-
INSERT INTO page_diff_hunk (page_id, revision, start,
101+
INSERT INTO wiki.page_diff_hunk (page_id, revision, start,
101102
content, lines_added, lines_deleted, lines_context)
102103
VALUES
103104
(page_id, latest.revision, i-1, array_to_string(hunk, E'\n'),

patch.sql

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
create or replace function apply_hunk(original text[], hunk text[],
1+
create or replace function wiki.apply_hunk(original text[], hunk text[],
22
start int DEFAULT 1, reverse boolean DEFAULT FALSE) returns text[] as $$
33
declare
44
result text[];
@@ -77,23 +77,22 @@ end;
7777
$$ language plpgsql
7878
IMMUTABLE;
7979

80-
create or replace function get_page_at_revision(page_id int, revision int)
81-
-- returns page as $$
82-
returns page_latest as $$
80+
create or replace function wiki.get_page_at_revision(page_id int, revision int)
81+
returns wiki.page as $$
8382
#variable_conflict use_variable
8483
declare
85-
latest page_latest;
86-
diff page_diff;
87-
hunk page_diff_hunk;
84+
latest wiki.page_latest;
85+
diff wiki.page_diff;
86+
hunk wiki.page_diff_hunk;
8887
content text[];
8988
cur_rev int := -1;
9089
line_offset int := 0;
91-
result page_latest;
90+
result page;
9291
begin
9392
IF revision < 1 THEN
9493
RAISE 'Revision must be positive (got %)', revision;
9594
END IF;
96-
SELECT * INTO latest FROM page_latest WHERE id = page_id;
95+
SELECT * INTO latest FROM wiki.page_latest WHERE id = page_id;
9796
IF NOT FOUND THEN
9897
RAISE 'Page % not found', id;
9998
END IF;
@@ -102,17 +101,17 @@ begin
102101
latest.revision;
103102
END IF;
104103
IF revision = latest.revision THEN
105-
result := latest::page_latest;
104+
result := latest::page;
106105
RETURN result;
107106
END IF;
108-
SELECT * INTO diff FROM page_diff AS pd
107+
SELECT * INTO diff FROM wiki.page_diff AS pd
109108
WHERE pd.page_id = page_id
110109
AND pd.revision = revision;
111110
IF NOT FOUND THEN
112111
RAISE 'Revision % for page % not found', revision, page_id;
113112
END IF;
114113
content = string_to_array(latest.content, E'\n');
115-
FOR hunk IN SELECT * FROM page_diff_hunk AS pdh
114+
FOR hunk IN SELECT * FROM wiki.page_diff_hunk AS pdh
116115
WHERE pdh.page_id = page_id
117116
AND pdh.revision >= revision
118117
ORDER BY pdh.revision desc, pdh.start asc

tables.sql

Lines changed: 34 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
1-
DROP TABLE IF EXISTS "user";
2-
CREATE TABLE "user" (
1+
DROP SCHEMA IF EXISTS wiki;
2+
CREATE SCHEMA wiki;
3+
4+
DROP TABLE IF EXISTS wiki.editor;
5+
CREATE TABLE wiki.editor (
36
id serial PRIMARY KEY,
47
username varchar(256) NOT NULL,
58
email varchar(256) NOT NULL,
@@ -8,8 +11,8 @@ CREATE TABLE "user" (
811
admin boolean DEFAULT FALSE
912
);
1013

11-
DROP TABLE IF EXISTS page_latest CASCADE;
12-
CREATE TABLE page_latest (
14+
DROP TABLE IF EXISTS wiki.page_latest CASCADE;
15+
CREATE TABLE wiki.page_latest (
1316
id serial PRIMARY KEY,
1417
title varchar(256) NOT NULL,
1518
slug varchar(256) NOT NULL,
@@ -18,25 +21,25 @@ CREATE TABLE page_latest (
1821
comment text DEFAULT '',
1922
num_lines int NOT NULL,
2023
revision int DEFAULT 1 CONSTRAINT "revision must be positive" CHECK (revision > 0),
21-
editor int REFERENCES "user"(id),
24+
editor int REFERENCES wiki.editor(id),
2225
markup varchar(64) DEFAULT 'plain',
2326
language varchar(8) NOT NULL,
2427
edited_on timestamp DEFAULT now() NOT NULL
2528
);
2629

27-
DROP TABLE IF EXISTS page_diff CASCADE;
28-
CREATE TABLE page_diff (
30+
DROP TABLE IF EXISTS wiki.page_diff CASCADE;
31+
CREATE TABLE wiki.page_diff (
2932
page_id int,
3033
revision int,
31-
editor int REFERENCES "user"(id),
34+
editor int REFERENCES wiki.editor(id),
3235
created_on timestamp DEFAULT now() NOT NULL,
3336
comment text DEFAULT '',
3437
PRIMARY KEY (page_id, revision),
35-
FOREIGN KEY (page_id) REFERENCES page_latest(id) ON DELETE CASCADE
38+
FOREIGN KEY (page_id) REFERENCES wiki.page_latest(id) ON DELETE CASCADE
3639
);
3740

38-
DROP TABLE IF EXISTS page_diff_hunk;
39-
CREATE TABLE page_diff_hunk (
41+
DROP TABLE IF EXISTS wiki.page_diff_hunk;
42+
CREATE TABLE wiki.page_diff_hunk (
4043
page_id int,
4144
revision int,
4245
start int NOT NULL CONSTRAINT "start must be positive" CHECK (start > 0),
@@ -45,17 +48,13 @@ CREATE TABLE page_diff_hunk (
4548
lines_deleted int NOT NULL DEFAULT 0,
4649
lines_context int NOT NULL DEFAULT 0,
4750
PRIMARY KEY (page_id, revision, start),
48-
FOREIGN KEY (page_id, revision) REFERENCES page_diff ON DELETE CASCADE
49-
-- EXCLUDE USING gist (page_id WITH =, revision WITH =, [overlapping])
50-
-- Would need to define a page_diff_hunk_meta type, define the "&&" operation
51-
-- over it, and add a "meta" type to this table. Not worth the effort right now.
52-
-- See: http://www.pgcon.org/2010/schedule/attachments/136_exclusion_constraints2.pdf
51+
FOREIGN KEY (page_id, revision) REFERENCES wiki.page_diff ON DELETE CASCADE
5352
);
5453

55-
CREATE OR REPLACE FUNCTION hunk_overlap(page_diff_hunk, page_diff_hunk) returns boolean as $$
54+
CREATE OR REPLACE FUNCTION wiki.hunk_overlap(wiki.page_diff_hunk, wiki.page_diff_hunk) returns boolean as $$
5655
declare
57-
first page_diff_hunk;
58-
second page_diff_hunk;
56+
first wiki.page_diff_hunk;
57+
second wiki.page_diff_hunk;
5958
begin
6059
if $1.page_id != $2.page_id or $1.revision != $2.revision then
6160
RETURN FALSE;
@@ -79,24 +78,26 @@ $$ language plpgsql
7978
IMMUTABLE STRICT;
8079

8180
CREATE OPERATOR && (
82-
leftarg = page_diff_hunk,
83-
rightarg = page_diff_hunk,
84-
procedure = hunk_overlap,
81+
leftarg = wiki.page_diff_hunk,
82+
rightarg = wiki.page_diff_hunk,
83+
procedure = wiki.hunk_overlap,
8584
commutator = &&
8685
);
8786

88-
create function compare() returns boolean as $$
87+
DROP TABLE IF EXISTS wiki.page;
88+
CREATE TABLE wiki.page (LIKE page_latest);
89+
90+
CREATE OR REPLACE FUNCTION wiki.page_latest_to_page(wiki.page_latest)
91+
returns wiki.page as $$
8992
declare
90-
one page_diff_hunk;
91-
two page_diff_hunk;
93+
result wiki.page;
9294
begin
93-
SELECT * into one FROM page_diff_hunk WHERE page_id = 1 AND revision = 1
94-
AND start = 1;
95-
SELECT * into two FROM page_diff_hunk WHERE page_id = 1 AND revision = 1
96-
AND start = 2;
97-
RETURN one && two;
95+
result := ($1.id, $1.title, $1.slug, $1.namespace, $1.content, $1.comment,
96+
$1.num_lines, $1.revision, $1.editor, $1.markup, $1.language,
97+
$1.edited_on);
98+
RETURN result;
9899
end;
99-
$$ language plpgsql;
100+
$$ language plpgsql IMMUTABLE STRICT;
100101

101-
DROP TABLE IF EXISTS page;
102-
CREATE TABLE page (LIKE page_latest);
102+
CREATE CAST (wiki.page_latest AS wiki.page)
103+
WITH FUNCTION wiki.page_latest_to_page(wiki.page_latest);

test.sql

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
truncate page_latest, page_diff, page_diff_hunk;
2-
INSERT INTO user(id, username, email) VALUES (1, 'foo', 'foo');
2+
INSERT INTO "user"(id, username, email) VALUES (1, 'foo', 'foo');
33
INSERT INTO page_latest(id, title, slug, num_lines, editor, language, content)
44
VALUES (1, 'page', 'page', 3, 1, 'en-us',
55
'a

0 commit comments

Comments
 (0)