sql: add indexes on formdata id in evolution tables (#22234)
This commit is contained in:
parent
95651fc809
commit
b5293358bf
|
@ -666,6 +666,25 @@ def test_sql_table_wipe_and_drop():
|
|||
conn, cur = sql.get_connection_and_cursor()
|
||||
assert table_exists(cur, test_formdef.table_name)
|
||||
|
||||
@postgresql
|
||||
def test_sql_indexes():
|
||||
test_formdef = FormDef()
|
||||
test_formdef.name = 'tests indexes'
|
||||
test_formdef.fields = []
|
||||
test_formdef.store()
|
||||
data_class = test_formdef.data_class(mode='sql')
|
||||
assert data_class.count() == 0
|
||||
conn, cur = sql.get_connection_and_cursor()
|
||||
assert index_exists(cur, test_formdef.table_name + '_evolutions_fid')
|
||||
conn.commit()
|
||||
cur.close()
|
||||
|
||||
data_class.wipe(drop=True)
|
||||
conn, cur = sql.get_connection_and_cursor()
|
||||
assert not index_exists(cur, test_formdef.table_name + '_evolutions_fid')
|
||||
conn.commit()
|
||||
cur.close()
|
||||
|
||||
@postgresql
|
||||
def test_sql_table_select():
|
||||
test_formdef = FormDef()
|
||||
|
@ -924,6 +943,12 @@ def column_exists_in_table(cur, table_name, column_name):
|
|||
AND column_name = %s''', (table_name, column_name))
|
||||
return bool(cur.fetchone()[0] == 1)
|
||||
|
||||
def index_exists(cur, index_name):
|
||||
cur.execute('''SELECT COUNT(*) FROM pg_indexes
|
||||
WHERE schemaname = 'public'
|
||||
AND indexname = %s''', (index_name,))
|
||||
return bool(cur.fetchone()[0] == 1)
|
||||
|
||||
@postgresql
|
||||
def test_sql_level():
|
||||
conn, cur = sql.get_connection_and_cursor()
|
||||
|
@ -1104,6 +1129,32 @@ def test_migration_21_users_ascii_name():
|
|||
conn.commit()
|
||||
cur.close()
|
||||
|
||||
@postgresql
|
||||
def test_migration_24_evolution_index():
|
||||
formdef = FormDef()
|
||||
formdef.name = 'tests migration 24'
|
||||
formdef.fields = []
|
||||
formdef.store()
|
||||
|
||||
conn, cur = sql.get_connection_and_cursor()
|
||||
cur.execute('DROP INDEX %s_evolutions_fid' % formdef.table_name)
|
||||
cur.execute('UPDATE wcs_meta SET value = 23 WHERE key = %s', ('sql_level',))
|
||||
conn.commit()
|
||||
cur.close()
|
||||
|
||||
conn, cur = sql.get_connection_and_cursor()
|
||||
assert not index_exists(cur, formdef.table_name + '_evolutions_fid')
|
||||
conn.commit()
|
||||
cur.close()
|
||||
|
||||
sql.migrate()
|
||||
|
||||
conn, cur = sql.get_connection_and_cursor()
|
||||
assert index_exists(cur, formdef.table_name + '_evolutions_fid')
|
||||
|
||||
conn.commit()
|
||||
cur.close()
|
||||
|
||||
def drop_formdef_tables():
|
||||
conn, cur = sql.get_connection_and_cursor()
|
||||
cur.execute('''SELECT table_name FROM information_schema.tables''')
|
||||
|
|
28
wcs/sql.py
28
wcs/sql.py
|
@ -375,6 +375,8 @@ def do_formdef_tables(formdef, conn=None, cur=None, rebuild_views=False, rebuild
|
|||
parts bytea,
|
||||
formdata_id integer REFERENCES %s (id) ON DELETE CASCADE)''' % (
|
||||
table_name, table_name))
|
||||
do_formdef_indexes(formdef, created=True, conn=conn, cur=cur)
|
||||
|
||||
cur.execute('''SELECT column_name FROM information_schema.columns
|
||||
WHERE table_schema = 'public'
|
||||
AND table_name = %s''', (table_name,))
|
||||
|
@ -482,6 +484,25 @@ def do_formdef_tables(formdef, conn=None, cur=None, rebuild_views=False, rebuild
|
|||
|
||||
return actions
|
||||
|
||||
def do_formdef_indexes(formdef, created, conn, cur, concurrently=False):
|
||||
table_name = get_formdef_table_name(formdef)
|
||||
evolutions_table_name = table_name + '_evolutions'
|
||||
existing_indexes = set()
|
||||
if not created:
|
||||
cur.execute('''SELECT indexname
|
||||
FROM pg_indexes
|
||||
WHERE schemaname = 'public'
|
||||
AND tablename IN (%s, %s)''', (table_name, evolutions_table_name))
|
||||
existing_indexes = set([x[0] for x in cur.fetchall()])
|
||||
|
||||
create_index = 'CREATE INDEX'
|
||||
if concurrently:
|
||||
create_index = 'CREATE INDEX CONCURRENTLY'
|
||||
|
||||
if not evolutions_table_name + '_fid' in existing_indexes:
|
||||
cur.execute('''%s %s_fid ON %s (formdata_id)''' % (
|
||||
create_index, evolutions_table_name, evolutions_table_name))
|
||||
|
||||
@guard_postgres
|
||||
def do_user_table():
|
||||
conn, cur = get_connection_and_cursor()
|
||||
|
@ -1955,7 +1976,7 @@ def get_yearly_totals(period_start=None, period_end=None, criterias=None):
|
|||
return result
|
||||
|
||||
|
||||
SQL_LEVEL = 23
|
||||
SQL_LEVEL = 24
|
||||
|
||||
def migrate_global_views(conn, cur):
|
||||
cur.execute('''SELECT COUNT(*) FROM information_schema.tables
|
||||
|
@ -2034,6 +2055,11 @@ def migrate():
|
|||
for formdef in FormDef.select():
|
||||
for formdata in formdef.data_class().select():
|
||||
formdata.store()
|
||||
if sql_level < 24:
|
||||
from wcs.formdef import FormDef
|
||||
# 24: add index on evolution(formdata_id)
|
||||
for formdef in FormDef.select():
|
||||
do_formdef_indexes(formdef, created=False, conn=conn, cur=cur)
|
||||
|
||||
cur.execute('''UPDATE wcs_meta SET value = %s WHERE key = %s''', (
|
||||
str(SQL_LEVEL), 'sql_level'))
|
||||
|
|
Loading…
Reference in New Issue