- CREATE OR REPLACE FUNCTION public.create_plpgsql_language ()
- RETURNS TEXT
- AS $$
- CREATE LANGUAGE plpgsql;
- SELECT 'language plpgsql created'::TEXT;
- $$
- LANGUAGE 'sql';
-
- SELECT CASE WHEN
- (SELECT true::BOOLEAN
- FROM pg_language
- WHERE lanname='plpgsql')
- THEN
- (SELECT 'language already installed'::TEXT)
- ELSE
- (SELECT public.create_plpgsql_language())
- END;
-
- DROP FUNCTION public.create_plpgsql_language ();
-
- CREATE OR REPLACE FUNCTION node_ranking(node_id int, srch text) RETURNS float AS $$
- declare
- v tsvector;
- begin
- SELECT tsv INTO v FROM forum_node WHERE id = node_id;
- RETURN ts_rank_cd(v || children_tsv(node_id), plainto_tsquery(srch), 32);
- end
- $$ LANGUAGE plpgsql;
-
- CREATE OR REPLACE FUNCTION children_tsv(id int) RETURNS tsvector AS $$
- declare
- v tsvector := ''::tsvector;
- r record;
- begin
- FOR r IN SELECT * FROM forum_node WHERE parent_id = id LOOP
- v := v || r.tsv || children_tsv(r.id);
- END LOOP;
- RETURN v;
- end
- $$ LANGUAGE plpgsql;
-
- CREATE OR REPLACE FUNCTION set_node_tsv() RETURNS TRIGGER AS $$
- begin
- IF (tg_op = 'INSERT') THEN
- new.tsv :=
- setweight(to_tsvector('english', coalesce(new.tagnames,'')), 'A') ||
- setweight(to_tsvector('english', coalesce(new.title,'')), 'B') ||
- setweight(to_tsvector('english', coalesce(new.body,'')), 'C');
- ELSIF (new.active_revision_id <> old.active_revision_id) OR (new.tsv IS NULL) THEN
- new.tsv :=
- setweight(to_tsvector('english', coalesce(new.tagnames,'')), 'A') ||
- setweight(to_tsvector('english', coalesce(new.title,'')), 'B') ||
- setweight(to_tsvector('english', coalesce(new.body,'')), 'C');
+CREATE OR REPLACE FUNCTION public.create_plpgsql_language ()
+ RETURNS TEXT
+ AS $$
+ CREATE LANGUAGE plpgsql;
+ SELECT 'language plpgsql created'::TEXT;
+ $$
+LANGUAGE 'sql';
+
+SELECT CASE WHEN
+ (SELECT true::BOOLEAN
+ FROM pg_language
+ WHERE lanname='plpgsql')
+ THEN
+ (SELECT 'language already installed'::TEXT)
+ ELSE
+ (SELECT public.create_plpgsql_language())
+ END;
+
+DROP FUNCTION public.create_plpgsql_language ();
+
+CREATE OR REPLACE FUNCTION public.drop_tsv_noderevision_column () RETURNS VOID AS $$
+begin
+ ALTER TABLE forum_noderevision DROP COLUMN tsv;
+ DROP TRIGGER IF EXISTS tsvectorupdate ON forum_noderevision;
+end
+$$ LANGUAGE plpgsql;
+
+CREATE OR REPLACE FUNCTION public.tsv_noderevision_column_exists() RETURNS int AS $$
+ SELECT COUNT(attname)::int FROM pg_attribute WHERE attrelid = (SELECT oid FROM pg_class WHERE relname = 'forum_noderevision') AND attname = 'tsv';
+$$ LANGUAGE 'sql';
+
+select case when public.tsv_noderevision_column_exists()>0 then public.drop_tsv_noderevision_column()end;
+
+drop function drop_tsv_noderevision_column();
+drop function tsv_noderevision_column_exists();
+
+CREATE OR REPLACE FUNCTION set_doctable_tsv() RETURNS TRIGGER AS $$
+declare
+ root_id int;
+ doc tsvector;
+ rcount int;
+ cv tsvector;
+begin
+ SELECT abs_parent_id INTO root_id FROM forum_node WHERE id = new.node_id;
+
+ IF root_id IS NULL THEN
+ root_id := new.node_id;
+ END IF;
+
+ SELECT count(*)::int INTO rcount FROM forum_node WHERE id = root_id;
+
+ IF rcount = 0 THEN
+ return new;