sql: drop and create views in a single transaction (#6247)
This commit is contained in:
parent
b04822353a
commit
cb074d737f
36
wcs/sql.py
36
wcs/sql.py
|
@ -337,8 +337,7 @@ def do_formdef_tables(formdef, rebuild_views=False):
|
|||
if rebuild_views or len(existing_fields - needed_fields):
|
||||
# views may have been dropped when dropping columns, so we recreate
|
||||
# them even if not asked to.
|
||||
drop_views(formdef)
|
||||
do_views(formdef)
|
||||
redo_views(formdef)
|
||||
|
||||
if not 'concerned_roles_array' in existing_fields:
|
||||
return ['rebuild_security']
|
||||
|
@ -406,9 +405,21 @@ def do_user_table():
|
|||
cur.close()
|
||||
|
||||
@guard_postgres
|
||||
def drop_views(formdef=None):
|
||||
conn, cur = get_connection_and_cursor()
|
||||
def redo_views(formdef):
|
||||
if get_publisher().get_site_option('postgresql_views') == 'false':
|
||||
return
|
||||
|
||||
if formdef.id is None:
|
||||
return
|
||||
|
||||
conn, cur = get_connection_and_cursor()
|
||||
drop_views(formdef, conn, cur)
|
||||
do_views(formdef, conn, cur)
|
||||
conn.commit()
|
||||
cur.close()
|
||||
|
||||
@guard_postgres
|
||||
def drop_views(formdef, conn, cur):
|
||||
# remove the global views
|
||||
cur.execute('''SELECT table_name FROM information_schema.views
|
||||
WHERE table_name LIKE %s''', ('wcs\_category\_%',))
|
||||
|
@ -442,20 +453,8 @@ def drop_views(formdef=None):
|
|||
for view_name in view_names:
|
||||
cur.execute('''DROP VIEW IF EXISTS %s''' % view_name)
|
||||
|
||||
conn.commit()
|
||||
cur.close()
|
||||
|
||||
|
||||
@guard_postgres
|
||||
def do_views(formdef):
|
||||
if get_publisher().get_site_option('postgresql_views') == 'false':
|
||||
return
|
||||
|
||||
if formdef.id is None:
|
||||
return
|
||||
|
||||
conn, cur = get_connection_and_cursor()
|
||||
|
||||
def do_views(formdef, conn, cur):
|
||||
# create new view
|
||||
table_name = get_formdef_table_name(formdef)
|
||||
view_name = 'wcs_view_%s_%s' % (formdef.id,
|
||||
|
@ -524,9 +523,6 @@ def do_views(formdef):
|
|||
cur.execute('''CREATE VIEW wcs_category_%s AS SELECT * from wcs_all_forms
|
||||
WHERE category_id = %s''' % (name, category.id))
|
||||
|
||||
conn.commit()
|
||||
cur.close()
|
||||
|
||||
|
||||
class SqlMixin(object):
|
||||
_table_name = None
|
||||
|
|
Loading…
Reference in New Issue