blob: d03f63b8afa1678428384cc6e7c51b7fc903d369 [file] [log] [blame]
Matthias Andreas Benkard8563a3c2020-09-16 17:57:24 +02001<?xml version="1.1" encoding="UTF-8" standalone="no"?>
2<databaseChangeLog
3 xmlns="http://www.liquibase.org/xml/ns/dbchangelog"
4 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
5 xsi:schemaLocation="
6 http://www.liquibase.org/xml/ns/dbchangelog
7 http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-3.10.xsd">
8
9 <changeSet author="mulk" id="1.6-1">
10 <sql>
11 CREATE FUNCTION language_regconfig(language VARCHAR)
12 RETURNS regconfig
13 AS $$
14 SELECT CASE
15 WHEN $1 = 'de' THEN 'german'
16 WHEN $1 = 'en' THEN 'english'
17 WHEN $1 = 'fr' THEN 'french'
18 ELSE 'simple'
19 END :: regconfig
20 $$ IMMUTABLE LANGUAGE SQL;
21 </sql>
22
23 <sql>
24 CREATE INDEX bookmark_texts_search_idx ON benki.bookmark_texts
25 USING GIN (to_tsvector(language_regconfig(language), title || ' ' || description));
26 </sql>
27
28 <sql>
29 CREATE INDEX lazychat_message_texts_search_idx ON benki.lazychat_message_texts
30 USING GIN (to_tsvector(language_regconfig(language), content));
31 </sql>
32 </changeSet>
33
34 <changeSet id="1.6-2" author="mulk">
35 <createProcedure procedureName="post_matches_websearch">
36 CREATE FUNCTION post_matches_websearch(
37 post_language TEXT,
38 post_text TEXT,
39 websearch_language TEXT,
40 websearch_text TEXT)
41 RETURNS BOOLEAN
42 AS $$
43 SELECT to_tsvector(language_regconfig($1), $2) @@ websearch_to_tsquery(language_regconfig($3), $4)
44 $$ LANGUAGE SQL IMMUTABLE;
45 </createProcedure>
46
47 <createProcedure procedureName="post_matches_websearch">
48 CREATE FUNCTION post_matches_websearch(
49 post_tsvector tsvector,
50 websearch_language TEXT,
51 websearch_text TEXT)
52 RETURNS BOOLEAN
53 AS $$
54 SELECT $1 @@ websearch_to_tsquery(language_regconfig($2), $3)
55 $$ LANGUAGE SQL IMMUTABLE;
56 </createProcedure>
57 </changeSet>
58
59 <changeSet id="1.6-3" author="mulk">
60 <addColumn tableName="bookmark_texts" schemaName="benki">
61 <column name="search_terms" type="tsvector"/>
62 </addColumn>
63 <sql>
64 CREATE INDEX bookmark_texts_search_terms_idx ON benki.bookmark_texts
65 USING GIN (search_terms);
66 </sql>
67
68 <addColumn tableName="lazychat_message_texts" schemaName="benki">
69 <column name="search_terms" type="tsvector"/>
70 </addColumn>
71 <sql>
72 CREATE INDEX lazychat_message_texts_search_terms_idx ON benki.lazychat_message_texts
73 USING GIN (search_terms);
74 </sql>
75
76 <addColumn tableName="post_texts" schemaName="benki">
77 <column name="search_terms" type="tsvector"/>
78 </addColumn>
79
80 <createProcedure procedureName="bookmark_search_term_update_trigger">
81 CREATE FUNCTION bookmark_search_term_update_trigger()
82 RETURNS trigger
83 AS $$
84 BEGIN
85 NEW.search_terms :=
86 setweight(to_tsvector(language_regconfig(NEW.language), coalesce(NEW.title, '')), 'A') ||
87 setweight(to_tsvector(language_regconfig(NEW.language), coalesce(NEW.description, '')), 'B');
88 RETURN NEW;
89 END
90 $$ LANGUAGE plpgsql;
91 </createProcedure>
92
93 <createProcedure procedureName="lazychat_message_search_term_update_trigger">
94 CREATE FUNCTION lazychat_message_search_term_update_trigger()
95 RETURNS trigger
96 AS $$
97 BEGIN
98 NEW.search_terms :=
99 to_tsvector(language_regconfig(NEW.language), coalesce(NEW.content,''));
100 RETURN NEW;
101 END
102 $$ LANGUAGE plpgsql;
103 </createProcedure>
104
105 <sql>
106 CREATE TRIGGER bookmark_search_term_update
107 BEFORE INSERT OR UPDATE
108 ON benki.bookmark_texts
109 FOR EACH ROW
110 EXECUTE FUNCTION
111 bookmark_search_term_update_trigger();
112 </sql>
113
114 <sql>
115 CREATE TRIGGER lazychat_message_search_term_update
116 BEFORE INSERT OR UPDATE
117 ON benki.lazychat_message_texts
118 FOR EACH ROW
119 EXECUTE FUNCTION
120 lazychat_message_search_term_update_trigger();
121 </sql>
122 </changeSet>
123
124 <changeSet id="1.6-4" author="mulk">
125 <sql>
126 UPDATE benki.bookmark_texts
127 SET search_terms =
128 setweight(to_tsvector(language_regconfig(language), coalesce(title, '')), 'A') ||
129 setweight(to_tsvector(language_regconfig(language), coalesce(description, '')), 'B');
130 </sql>
131
132 <sql>
133 UPDATE benki.lazychat_message_texts
134 SET search_terms =
135 to_tsvector(language_regconfig(language), coalesce(content,''));
136 </sql>
137 </changeSet>
138
139</databaseChangeLog>