sql: use column types from table_static_fields for migrations (#73674)

This commit is contained in:
Frédéric Péters 2022-09-06 16:37:39 +02:00 committed by Gitea
parent 484ef75682
commit c5a5870523
1 changed files with 7 additions and 43 deletions

View File

@ -779,57 +779,21 @@ def do_formdef_tables(formdef, conn=None, cur=None, rebuild_views=False, rebuild
# migrations
if 'fts' not in existing_fields:
# full text search
# full text search, column and index
cur.execute('''ALTER TABLE %s ADD COLUMN fts tsvector''' % table_name)
cur.execute('''CREATE INDEX %s_fts ON %s USING gin(fts)''' % (table_name, table_name))
if 'workflow_roles' not in existing_fields:
cur.execute('''ALTER TABLE %s ADD COLUMN workflow_roles bytea''' % table_name)
cur.execute('''ALTER TABLE %s ADD COLUMN workflow_roles_array text[]''' % table_name)
if 'workflow_merged_roles_dict' not in existing_fields:
cur.execute('''ALTER TABLE %s ADD COLUMN workflow_merged_roles_dict jsonb''' % table_name)
if 'concerned_roles_array' not in existing_fields:
cur.execute('''ALTER TABLE %s ADD COLUMN concerned_roles_array text[]''' % table_name)
if 'actions_roles_array' not in existing_fields:
cur.execute('''ALTER TABLE %s ADD COLUMN actions_roles_array text[]''' % table_name)
if 'page_no' not in existing_fields:
cur.execute('''ALTER TABLE %s ADD COLUMN page_no varchar''' % table_name)
if 'anonymised' not in existing_fields:
cur.execute('''ALTER TABLE %s ADD COLUMN anonymised timestamptz''' % table_name)
if 'tracking_code' not in existing_fields:
cur.execute('''ALTER TABLE %s ADD COLUMN tracking_code varchar''' % table_name)
if 'backoffice_submission' not in existing_fields:
cur.execute('''ALTER TABLE %s ADD COLUMN backoffice_submission boolean''' % table_name)
if 'submission_context' not in existing_fields:
cur.execute('''ALTER TABLE %s ADD COLUMN submission_context bytea''' % table_name)
if 'submission_agent_id' not in existing_fields:
cur.execute('''ALTER TABLE %s ADD COLUMN submission_agent_id varchar''' % table_name)
if 'submission_channel' not in existing_fields:
cur.execute('''ALTER TABLE %s ADD COLUMN submission_channel varchar''' % table_name)
if 'criticality_level' not in existing_fields:
# criticality leve, with default value
existing_fields.add('criticality_level')
cur.execute(
'''ALTER TABLE %s ADD COLUMN criticality_level integer NOT NULL DEFAULT(0)''' % table_name
)
if 'last_update_time' not in existing_fields:
cur.execute('''ALTER TABLE %s ADD COLUMN last_update_time timestamp''' % table_name)
if 'digests' not in existing_fields:
cur.execute('''ALTER TABLE %s ADD COLUMN digests jsonb''' % table_name)
if 'user_label' not in existing_fields:
cur.execute('''ALTER TABLE %s ADD COLUMN user_label varchar''' % table_name)
if 'prefilling_data' not in existing_fields:
cur.execute('''ALTER TABLE %s ADD COLUMN prefilling_data bytea''' % table_name)
# generic migration for new columns
for field_name, field_type in formdef.data_class()._table_static_fields:
if field_name not in existing_fields:
cur.execute('''ALTER TABLE %s ADD COLUMN %s %s''' % (table_name, field_name, field_type))
# add new fields
for field in formdef.get_all_fields():