redo search tokens creation
gitea/wcs/pipeline/head There was a failure building this commit Details

This commit is contained in:
Pierre Ducroquet 2024-02-29 12:46:48 +01:00
parent 4dd193988a
commit 01f74d5c4b
2 changed files with 16 additions and 6 deletions

View File

@ -445,6 +445,7 @@ class WcsPublisher(QommonPublisher):
sql.ApplicationElement.do_table()
sql.SearchableFormDef.do_table()
sql.TranslatableMessage.do_table()
sql.init_search_tokens()
sql.do_meta_table()
from .carddef import CardDef
from .formdef import FormDef

View File

@ -95,6 +95,10 @@ SQL_TYPE_MAPPING = {
'computed': 'jsonb',
}
def _table_exists(cur, table_name):
cur.execute("SELECT 1 FROM pg_class WHERE relname = %s;", (table_name,))
rows = cur.fetchall()
return len(rows) > 0;
class WcsPgConnection(psycopg2.extensions.connection):
def __init__(self, *args, **kwargs):
@ -1579,7 +1583,7 @@ def do_global_views(conn, cur):
% (name, category.id)
)
init_search_tokens(cur)
init_search_tokens_triggers(cur)
def clean_global_views(conn, cur):
@ -1681,14 +1685,11 @@ def init_global_table(conn=None, cur=None):
def init_search_tokens(conn=None, cur=None):
own_cur = False
if not cur:
if cur is None:
own_cur = True
conn, cur = get_connection_and_cursor()
cur.execute("SELECT 1 FROM pg_class WHERE relname = 'wcs_search_tokens';")
rows = cur.fetchall()
if len(rows) == 1:
# table already exists, nothing to do.
if _table_exists(cur, 'wcs_search_tokens'):
if own_cur:
cur.close()
return
@ -1738,6 +1739,10 @@ def init_search_tokens_triggers(cur):
# Instead, a weekly cron job will delete obsolete entries, thus making it sure no
# personal data is kept uselessly.
if not(_table_exists(cur, 'wcs_search_tokens')):
# abort trigger creation if tokens table doesn't exist yet
return
# First part: the appending function
cur.execute("""CREATE OR REPLACE FUNCTION wcs_search_tokens_trigger_fn ()
RETURNS trigger
@ -1758,6 +1763,10 @@ $function$;""")
cur.execute("CREATE TRIGGER searchable_formdefs_fts_trg_upd AFTER UPDATE OF fts ON searchable_formdefs FOR EACH ROW WHEN (NEW.fts IS NOT NULL) EXECUTE PROCEDURE wcs_search_tokens_trigger_fn();")
def init_search_tokens_data(cur):
if not(_table_exists(cur, 'wcs_search_tokens')):
# abort table data initialization if tokens table doesn't exist yet
return
cur.execute("INSERT INTO wcs_search_tokens SELECT unnest(tsvector_to_array(fts)) FROM wcs_all_forms ON CONFLICT(token) DO NOTHING;")
cur.execute("INSERT INTO wcs_search_tokens SELECT unnest(tsvector_to_array(fts)) FROM searchable_formdefs ON CONFLICT(token) DO NOTHING;")