Projet

Général

Profil

0001-sql-delay-re-index-operations-on-first-cron-22383.patch

Thomas Noël, 09 avril 2018 21:03

Télécharger (4,32 ko)

Voir les différences:

Subject: [PATCH] sql: delay re-index operations on first cron (#22383)

 wcs/publisher.py   |  4 ++++
 wcs/qommon/cron.py |  5 +++++
 wcs/sql.py         | 55 +++++++++++++++++++++++++++++++++++++++++++++++-------
 3 files changed, 57 insertions(+), 7 deletions(-)
wcs/publisher.py
293 293
        import sql
294 294
        sql.migrate()
295 295

  
296
    def reindex_sql(self):
297
        import sql
298
        sql.reindex()
299

  
296 300
    def cleanup(self):
297 301
        if self.is_using_postgresql():
298 302
            import sql
wcs/qommon/cron.py
35 35
        publisher.set_config()
36 36
    except:
37 37
        return
38

  
39
    # reindex user and formdata if needed (should only be run once)
40
    if publisher.is_using_postgresql():
41
        publisher.reindex_sql()
42

  
38 43
    for job in publisher.cronjobs:
39 44
        if job.days and now[2] not in job.days:
40 45
            continue
wcs/sql.py
1994 1994
    sql_level = int(cur.fetchone()[0])
1995 1995
    return sql_level
1996 1996

  
1997
@guard_postgres
1998
def is_reindex_needed(index, conn, cur):
1999
    do_meta_table(conn, cur, insert_current_sql_level=False)
2000
    key_name = 'reindex_%s' % index
2001
    cur.execute('''SELECT value FROM wcs_meta WHERE key = %s''', (key_name, ))
2002
    row = cur.fetchone()
2003
    if row is None:
2004
        cur.execute('''INSERT INTO wcs_meta (id, key, value)
2005
                       VALUES (DEFAULT, %s, %s)''', (key_name, 'no'))
2006
        return False
2007
    return row[0] == 'needed'
2008

  
2009
@guard_postgres
2010
def set_reindex(index, value, conn, cur):
2011
    do_meta_table(conn, cur, insert_current_sql_level=False)
2012
    key_name = 'reindex_%s' % index
2013
    cur.execute('''SELECT value FROM wcs_meta WHERE key = %s''', (key_name, ))
2014
    row = cur.fetchone()
2015
    if row is None:
2016
        cur.execute('''INSERT INTO wcs_meta (id, key, value)
2017
                       VALUES (DEFAULT, %s, %s)''', (key_name, value))
2018
    else:
2019
        cur.execute('''UPDATE wcs_meta SET value = %s WHERE key = %s''', (
2020
                    value, key_name))
2021

  
1997 2022
def migrate_views(conn, cur):
1998 2023
    drop_views(None, conn, cur)
1999 2024
    from wcs.formdef import FormDef
......
2043 2068
        # 12: (second part), store fts in existing rows
2044 2069
        # 21: (second part), store ascii_name of users
2045 2070
        # 23: (first part), use misc.simplify() over full text queries
2046
        for user_id in SqlUser.keys():
2047
            SqlUser.get(user_id).store()
2071
        set_reindex('user', 'needed', conn=conn, cur=cur)
2048 2072
    if sql_level < 23:
2049 2073
        # 17: store last_update_time in tables
2050 2074
        # 18: add user name to full-text search index
2051 2075
        # 21: (third part), add user ascii_names to full-text index
2052 2076
        # 23: (second part) use misc.simplify() over full text queries
2053
        # load and store all formdatas
2054
        from wcs.formdef import FormDef
2055
        for formdef in FormDef.select():
2056
            for formdata in formdef.data_class().select():
2057
                formdata.store()
2077
        set_reindex('formdata', 'needed', conn=conn, cur=cur)
2058 2078
    if sql_level < 24:
2059 2079
        from wcs.formdef import FormDef
2060 2080
        # 24: add index on evolution(formdata_id)
......
2066 2086

  
2067 2087
    conn.commit()
2068 2088
    cur.close()
2089

  
2090

  
2091
@guard_postgres
2092
def reindex():
2093
    conn, cur = get_connection_and_cursor()
2094

  
2095
    if is_reindex_needed('user', conn=conn, cur=cur):
2096
        for user_id in SqlUser.keys():
2097
            SqlUser.get(user_id).store()
2098
        set_reindex('user', 'done', conn=conn, cur=cur)
2099

  
2100
    if is_reindex_needed('formdata', conn=conn, cur=cur):
2101
        # load and store all formdatas
2102
        from wcs.formdef import FormDef
2103
        for formdef in FormDef.select():
2104
            for formdata in formdef.data_class().select():
2105
                formdata.store()
2106
        set_reindex('formdata', 'done', conn=conn, cur=cur)
2107

  
2108
    conn.commit()
2109
    cur.close()
2069
-