Projet

Général

Profil

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

Thomas Noël, 20 avril 2018 14:04

Télécharger (6,15 ko)

Voir les différences:

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

 tests/test_sql.py  | 20 +++++++++++++++++
 wcs/publisher.py   |  4 ++++
 wcs/qommon/cron.py |  5 +++++
 wcs/sql.py         | 55 ++++++++++++++++++++++++++++++++++++++++------
 4 files changed, 77 insertions(+), 7 deletions(-)
tests/test_sql.py
8 8
import sys
9 9
import time
10 10

  
11
from django.core.management import call_command
12

  
11 13
from quixote import cleanup
12 14

  
13 15
from wcs import formdef, publisher, fields
......
1102 1104
    assert column_exists_in_table(cur, 'users', 'fts')
1103 1105
    assert migration_level(cur) >= 12
1104 1106

  
1107
    # no fts, migration only prepare re-index
1108
    assert len(sql.SqlUser.get_ids_from_query('pierre')) == 0
1109

  
1110
    assert sql.is_reindex_needed('user', conn=conn, cur=cur) is True
1111
    assert sql.is_reindex_needed('formdata', conn=conn, cur=cur) is True
1112
    call_command('cron')  # first cron = reindex
1113
    assert sql.is_reindex_needed('user', conn=conn, cur=cur) is False
1114
    assert sql.is_reindex_needed('formdata', conn=conn, cur=cur) is False
1115

  
1105 1116
    # make sure the fts is filled after the migration
1106 1117
    assert len(sql.SqlUser.get_ids_from_query('pierre')) == 1
1107 1118

  
......
1127 1138
    assert column_exists_in_table(cur, 'users', 'ascii_name')
1128 1139
    assert migration_level(cur) >= 21
1129 1140

  
1141
    # no fts, migration only prepare re-index
1142
    assert sql.SqlUser.count([st.Equal('ascii_name', 'jean senisme')]) == 0
1143

  
1144
    assert sql.is_reindex_needed('user', conn=conn, cur=cur) is True
1145
    assert sql.is_reindex_needed('formdata', conn=conn, cur=cur) is True
1146
    call_command('cron')  # first cron = reindex
1147
    assert sql.is_reindex_needed('user', conn=conn, cur=cur) is False
1148
    assert sql.is_reindex_needed('formdata', conn=conn, cur=cur) is False
1149

  
1130 1150
    # make sure the ascii_name is filled after the migration
1131 1151
    assert sql.SqlUser.count([st.Equal('ascii_name', 'jean senisme')]) == 1
1132 1152

  
wcs/publisher.py
298 298
        import sql
299 299
        sql.migrate()
300 300

  
301
    def reindex_sql(self):
302
        import sql
303
        sql.reindex()
304

  
301 305
    def cleanup(self):
302 306
        if self.is_using_postgresql():
303 307
            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
2123 2123
    sql_level = int(cur.fetchone()[0])
2124 2124
    return sql_level
2125 2125

  
2126
@guard_postgres
2127
def is_reindex_needed(index, conn, cur):
2128
    do_meta_table(conn, cur, insert_current_sql_level=False)
2129
    key_name = 'reindex_%s' % index
2130
    cur.execute('''SELECT value FROM wcs_meta WHERE key = %s''', (key_name, ))
2131
    row = cur.fetchone()
2132
    if row is None:
2133
        cur.execute('''INSERT INTO wcs_meta (id, key, value)
2134
                       VALUES (DEFAULT, %s, %s)''', (key_name, 'no'))
2135
        return False
2136
    return row[0] == 'needed'
2137

  
2138
@guard_postgres
2139
def set_reindex(index, value, conn, cur):
2140
    do_meta_table(conn, cur, insert_current_sql_level=False)
2141
    key_name = 'reindex_%s' % index
2142
    cur.execute('''SELECT value FROM wcs_meta WHERE key = %s''', (key_name, ))
2143
    row = cur.fetchone()
2144
    if row is None:
2145
        cur.execute('''INSERT INTO wcs_meta (id, key, value)
2146
                       VALUES (DEFAULT, %s, %s)''', (key_name, value))
2147
    else:
2148
        cur.execute('''UPDATE wcs_meta SET value = %s WHERE key = %s''', (
2149
                    value, key_name))
2150

  
2126 2151
def migrate_views(conn, cur):
2127 2152
    drop_views(None, conn, cur)
2128 2153
    from wcs.formdef import FormDef
......
2172 2197
        # 12: (second part), store fts in existing rows
2173 2198
        # 21: (second part), store ascii_name of users
2174 2199
        # 23: (first part), use misc.simplify() over full text queries
2175
        for user_id in SqlUser.keys():
2176
            SqlUser.get(user_id).store()
2200
        set_reindex('user', 'needed', conn=conn, cur=cur)
2177 2201
    if sql_level < 23:
2178 2202
        # 17: store last_update_time in tables
2179 2203
        # 18: add user name to full-text search index
2180 2204
        # 21: (third part), add user ascii_names to full-text index
2181 2205
        # 23: (second part) use misc.simplify() over full text queries
2182
        # load and store all formdatas
2183
        from wcs.formdef import FormDef
2184
        for formdef in FormDef.select():
2185
            for formdata in formdef.data_class().select():
2186
                formdata.store()
2206
        set_reindex('formdata', 'needed', conn=conn, cur=cur)
2187 2207
    if sql_level < 24:
2188 2208
        from wcs.formdef import FormDef
2189 2209
        # 24: add index on evolution(formdata_id)
......
2197 2217

  
2198 2218
    conn.commit()
2199 2219
    cur.close()
2220

  
2221

  
2222
@guard_postgres
2223
def reindex():
2224
    conn, cur = get_connection_and_cursor()
2225

  
2226
    if is_reindex_needed('user', conn=conn, cur=cur):
2227
        for user in SqlUser.select(iterator=True):
2228
            user.store()
2229
        set_reindex('user', 'done', conn=conn, cur=cur)
2230

  
2231
    if is_reindex_needed('formdata', conn=conn, cur=cur):
2232
        # load and store all formdatas
2233
        from wcs.formdef import FormDef
2234
        for formdef in FormDef.select():
2235
            for formdata in formdef.data_class().select(iterator=True):
2236
                formdata.store()
2237
        set_reindex('formdata', 'done', conn=conn, cur=cur)
2238

  
2239
    conn.commit()
2240
    cur.close()
2200
-