BEGIN;

UPDATE permission.perm_list SET id = id + 1000 WHERE code NOT IN ( 
    'EVERYTHING',
    'OPAC_LOGIN',
    'STAFF_LOGIN',
    'MR_HOLDS',
    'TITLE_HOLDS',
    'VOLUME_HOLDS',
    'COPY_HOLDS',
    'REQUEST_HOLDS',
    'REQUEST_HOLDS_OVERRIDE',
    'VIEW_HOLD',
    'DELETE_HOLDS',
    'UPDATE_HOLD',
    'RENEW_CIRC',
    'VIEW_USER_FINES_SUMMARY',
    'VIEW_USER_TRANSACTIONS',
    'UPDATE_MARC',
    'CREATE_MARC',
    'IMPORT_MARC',
    'CREATE_VOLUME',
    'UPDATE_VOLUME',
    'DELETE_VOLUME',
    'UPDATE_COPY',
    'DELETE_COPY',
    'RENEW_HOLD_OVERRIDE',
    'CREATE_USER',
    'UPDATE_USER',
    'DELETE_USER',
    'VIEW_USER',
    'COPY_CHECKIN',
    'CREATE_TRANSIT',
    'VIEW_PERMISSION',
    'CHECKIN_BYPASS_HOLD_FULFILL',
    'CREATE_PAYMENT',
    'SET_CIRC_LOST',
    'SET_CIRC_MISSING',
    'SET_CIRC_CLAIMS_RETURNED',
    'CREATE_TRANSACTION',
    'CREATE_BILL',
    'VIEW_CONTAINER',
    'CREATE_CONTAINER',
    'CREATE_COPY',
    'UPDATE_ORG_UNIT',
    'VIEW_CIRCULATIONS',
    'VIEW_TRANSACTION',
    'DELETE_CONTAINER',
    'CREATE_CONTAINER_ITEM',
    'CREATE_USER_GROUP_LINK',
    'REMOVE_USER_GROUP_LINK',
    'VIEW_PERM_GROUPS',
    'VIEW_PERMIT_CHECKOUT',
    'UPDATE_BATCH_COPY',
    'CREATE_PATRON_STAT_CAT',
    'CREATE_COPY_STAT_CAT',
    'CREATE_PATRON_STAT_CAT_ENTRY',
    'CREATE_COPY_STAT_CAT_ENTRY',
    'UPDATE_PATRON_STAT_CAT',
    'UPDATE_COPY_STAT_CAT',
    'UPDATE_PATRON_STAT_CAT_ENTRY',
    'UPDATE_COPY_STAT_CAT_ENTRY',
    'CREATE_COPY_STAT_CAT_ENTRY_MAP',
    'CREATE_PATRON_STAT_CAT_ENTRY_MAP',
    'DELETE_PATRON_STAT_CAT',
    'DELETE_COPY_STAT_CAT',
    'DELETE_PATRON_STAT_CAT_ENTRY',
    'DELETE_COPY_STAT_CAT_ENTRY',
    'DELETE_PATRON_STAT_CAT_ENTRY_MAP',
    'DELETE_COPY_STAT_CAT_ENTRY_MAP',
    'CREATE_NON_CAT_TYPE',
    'UPDATE_NON_CAT_TYPE',
    'CREATE_IN_HOUSE_USE',
    'COPY_CHECKOUT',
    'CREATE_COPY_LOCATION',
    'UPDATE_COPY_LOCATION',
    'DELETE_COPY_LOCATION',
    'CREATE_COPY_TRANSIT',
    'COPY_TRANSIT_RECEIVE',
    'VIEW_HOLD_PERMIT',
    'VIEW_COPY_CHECKOUT_HISTORY',
    'REMOTE_Z3950_QUERY',
    'REGISTER_WORKSTATION',
    'VIEW_COPY_NOTES',
    'VIEW_VOLUME_NOTES',
    'VIEW_TITLE_NOTES',
    'CREATE_VOLUME_NOTE',
    'CREATE_COPY_NOTE',
    'CREATE_TITLE_NOTE',
    'DELETE_COPY_NOTE',
    'DELETE_VOLUME_NOTE',
    'DELETE_TITLE_NOTE',
    'UPDATE_CONTAINER',
    'CREATE_MY_CONTAINER',
    'VIEW_HOLD_NOTIFICATION',
    'CREATE_HOLD_NOTIFICATION',
    'UPDATE_ORG_SETTING',
    'OFFLINE_UPLOAD',
    'OFFLINE_VIEW',
    'OFFLINE_EXECUTE',
    'CIRC_OVERRIDE_DUE_DATE',
    'CIRC_PERMIT_OVERRIDE',
    'COPY_IS_REFERENCE.override',
    'VOID_BILLING',
    'CIRC_CLAIMS_RETURNED.override',
    'COPY_BAD_STATUS.override',
    'COPY_ALERT_MESSAGE.override',
    'COPY_STATUS_LOST.override',
    'COPY_STATUS_MISSING.override',
    'ABORT_TRANSIT',
    'ABORT_REMOTE_TRANSIT',
    'VIEW_ZIP_DATA',
    'CANCEL_HOLDS',
    'CREATE_DUPLICATE_HOLDS',
    'actor.org_unit.closed_date.update',
    'actor.org_unit.closed_date.delete',
    'actor.org_unit.closed_date.create',
    'DELETE_NON_CAT_TYPE',
    'money.collections_tracker.create',
    'money.collections_tracker.delete',
    'BAR_PATRON',
    'UNBAR_PATRON',
    'DELETE_WORKSTATION',
    'group_application.user',
    'group_application.user.patron',
    'group_application.user.staff',
    'group_application.user.staff.circ',
    'group_application.user.staff.cat',
    'group_application.user.staff.admin.global_admin',
    'group_application.user.staff.admin.local_admin',
    'group_application.user.staff.admin.lib_manager',
    'group_application.user.staff.cat.cat1',
    'group_application.user.staff.supercat',
    'group_application.user.sip_client',
    'group_application.user.vendor',
    'ITEM_AGE_PROTECTED.override',
    'MAX_RENEWALS_REACHED.override',
    'PATRON_EXCEEDS_CHECKOUT_COUNT.override',
    'PATRON_EXCEEDS_OVERDUE_COUNT.override',
    'PATRON_EXCEEDS_FINES.override',
    'CIRC_EXCEEDS_COPY_RANGE.override',
    'ITEM_ON_HOLDS_SHELF.override',
    'COPY_NOT_AVAILABLE.override',
    'HOLD_EXISTS.override',
    'RUN_REPORTS',
    'SHARE_REPORT_FOLDER',
    'VIEW_REPORT_OUTPUT',
    'COPY_CIRC_NOT_ALLOWED.override',
    'DELETE_CONTAINER_ITEM',
    'ASSIGN_WORK_ORG_UNIT',
    'CREATE_FUNDING_SOURCE',
    'DELETE_FUNDING_SOURCE',
    'VIEW_FUNDING_SOURCE',
    'UPDATE_FUNDING_SOURCE',
    'CREATE_FUND',
    'DELETE_FUND',
    'VIEW_FUND',
    'UPDATE_FUND',
    'CREATE_FUND_ALLOCATION',
    'DELETE_FUND_ALLOCATION',
    'VIEW_FUND_ALLOCATION',
    'UPDATE_FUND_ALLOCATION',
    'GENERAL_ACQ',
    'CREATE_PROVIDER',
    'DELETE_PROVIDER',
    'VIEW_PROVIDER',
    'UPDATE_PROVIDER',
    'ADMIN_FUNDING_SOURCE',
    'ADMIN_FUND',
    'MANAGE_FUNDING_SOURCE',
    'MANAGE_FUND',
    'CREATE_PICKLIST',
    'ADMIN_PROVIDER',
    'MANAGE_PROVIDER',
    'VIEW_PICKLIST',
    'DELETE_RECORD',
    'ADMIN_CURRENCY_TYPE',
    'MARK_BAD_DEBT',
    'VIEW_BILLING_TYPE',
    'MARK_ITEM_AVAILABLE',
    'MARK_ITEM_CHECKED_OUT',
    'MARK_ITEM_BINDERY',
    'MARK_ITEM_LOST',
    'MARK_ITEM_MISSING',
    'MARK_ITEM_IN_PROCESS',
    'MARK_ITEM_IN_TRANSIT',
    'MARK_ITEM_RESHELVING',
    'MARK_ITEM_ON_HOLDS_SHELF',
    'MARK_ITEM_ON_ORDER',
    'MARK_ITEM_ILL',
    'group_application.user.staff.acq',
    'CREATE_PURCHASE_ORDER',
    'VIEW_PURCHASE_ORDER',
    'IMPORT_ACQ_LINEITEM_BIB_RECORD',
    'RECEIVE_PURCHASE_ORDER',
    'VIEW_ORG_SETTINGS',
    'CREATE_MFHD_RECORD',
    'UPDATE_MFHD_RECORD',
    'DELETE_MFHD_RECORD',
    'ADMIN_ACQ_FUND',
    'group_application.user.staff.acq_admin',
    'ASSIGN_GROUP_PERM',
    'CREATE_AUDIENCE',
    'CREATE_BIB_LEVEL',
    'CREATE_CIRC_DURATION',
    'CREATE_CIRC_MOD',
    'CREATE_COPY_STATUS',
    'CREATE_HOURS_OF_OPERATION',
    'CREATE_ITEM_FORM',
    'CREATE_ITEM_TYPE',
    'CREATE_LANGUAGE',
    'CREATE_LASSO',
    'CREATE_LASSO_MAP',
    'CREATE_LIT_FORM',
    'CREATE_METABIB_FIELD',
    'CREATE_NET_ACCESS_LEVEL',
    'CREATE_ORG_ADDRESS',
    'CREATE_ORG_TYPE',
    'CREATE_ORG_UNIT',
    'CREATE_ORG_UNIT_CLOSING',
    'CREATE_PERM',
    'CREATE_RELEVANCE_ADJUSTMENT',
    'CREATE_SURVEY',
    'CREATE_VR_FORMAT',
    'CREATE_XML_TRANSFORM',
    'DELETE_AUDIENCE',
    'DELETE_BIB_LEVEL',
    'DELETE_CIRC_DURATION',
    'DELETE_CIRC_MOD',
    'DELETE_COPY_STATUS',
    'DELETE_HOURS_OF_OPERATION',
    'DELETE_ITEM_FORM',
    'DELETE_ITEM_TYPE',
    'DELETE_LANGUAGE',
    'DELETE_LASSO',
    'DELETE_LASSO_MAP',
    'DELETE_LIT_FORM',
    'DELETE_METABIB_FIELD',
    'DELETE_NET_ACCESS_LEVEL',
    'DELETE_ORG_ADDRESS',
    'DELETE_ORG_TYPE',
    'DELETE_ORG_UNIT',
    'DELETE_ORG_UNIT_CLOSING',
    'DELETE_PERM',
    'DELETE_RELEVANCE_ADJUSTMENT',
    'DELETE_SURVEY',
    'DELETE_TRANSIT',
    'DELETE_VR_FORMAT',
    'DELETE_XML_TRANSFORM',
    'REMOVE_GROUP_PERM',
    'TRANSIT_COPY',
    'UPDATE_AUDIENCE',
    'UPDATE_BIB_LEVEL',
    'UPDATE_CIRC_DURATION',
    'UPDATE_CIRC_MOD',
    'UPDATE_COPY_NOTE',
    'UPDATE_COPY_STATUS',
    'UPDATE_GROUP_PERM',
    'UPDATE_HOURS_OF_OPERATION',
    'UPDATE_ITEM_FORM',
    'UPDATE_ITEM_TYPE',
    'UPDATE_LANGUAGE',
    'UPDATE_LASSO',
    'UPDATE_LASSO_MAP',
    'UPDATE_LIT_FORM',
    'UPDATE_METABIB_FIELD',
    'UPDATE_NET_ACCESS_LEVEL',
    'UPDATE_ORG_ADDRESS',
    'UPDATE_ORG_TYPE',
    'UPDATE_ORG_UNIT_CLOSING',
    'UPDATE_PERM',
    'UPDATE_RELEVANCE_ADJUSTMENT',
    'UPDATE_SURVEY',
    'UPDATE_TRANSIT',
    'UPDATE_VOLUME_NOTE',
    'UPDATE_VR_FORMAT',
    'UPDATE_XML_TRANSFORM',
    'MERGE_BIB_RECORDS',
    'UPDATE_PICKUP_LIB_FROM_HOLDS_SHELF',
    'CREATE_ACQ_FUNDING_SOURCE',
    'CREATE_AUTHORITY_IMPORT_IMPORT_FIELD_DEF',
    'CREATE_AUTHORITY_IMPORT_QUEUE',
    'CREATE_AUTHORITY_RECORD_NOTE',
    'CREATE_BIB_IMPORT_FIELD_DEF',
    'CREATE_BIB_IMPORT_QUEUE',
    'CREATE_LOCALE',
    'CREATE_MARC_CODE',
    'CREATE_TRANSLATION',
    'DELETE_ACQ_FUNDING_SOURCE',
    'DELETE_AUTHORITY_IMPORT_IMPORT_FIELD_DEF',
    'DELETE_AUTHORITY_IMPORT_QUEUE',
    'DELETE_AUTHORITY_RECORD_NOTE',
    'DELETE_BIB_IMPORT_IMPORT_FIELD_DEF',
    'DELETE_BIB_IMPORT_QUEUE',
    'DELETE_LOCALE',
    'DELETE_MARC_CODE',
    'DELETE_TRANSLATION',
    'UPDATE_ACQ_FUNDING_SOURCE',
    'UPDATE_AUTHORITY_IMPORT_IMPORT_FIELD_DEF',
    'UPDATE_AUTHORITY_IMPORT_QUEUE',
    'UPDATE_AUTHORITY_RECORD_NOTE',
    'UPDATE_BIB_IMPORT_IMPORT_FIELD_DEF',
    'UPDATE_BIB_IMPORT_QUEUE',
    'UPDATE_LOCALE',
    'UPDATE_MARC_CODE',
    'UPDATE_TRANSLATION',
    'VIEW_ACQ_FUNDING_SOURCE',
    'VIEW_AUTHORITY_RECORD_NOTES',
    'CREATE_IMPORT_ITEM',
    'CREATE_IMPORT_ITEM_ATTR_DEF',
    'CREATE_IMPORT_TRASH_FIELD',
    'DELETE_IMPORT_ITEM',
    'DELETE_IMPORT_ITEM_ATTR_DEF',
    'DELETE_IMPORT_TRASH_FIELD',
    'UPDATE_IMPORT_ITEM',
    'UPDATE_IMPORT_ITEM_ATTR_DEF',
    'UPDATE_IMPORT_TRASH_FIELD',
    'UPDATE_ORG_UNIT_SETTING_ALL',
    'UPDATE_ORG_UNIT_SETTING.circ.lost_materials_processing_fee',
    'UPDATE_ORG_UNIT_SETTING.cat.default_item_price',
    'UPDATE_ORG_UNIT_SETTING.auth.opac_timeout',
    'UPDATE_ORG_UNIT_SETTING.auth.staff_timeout',
    'UPDATE_ORG_UNIT_SETTING.org.bounced_emails',
    'UPDATE_ORG_UNIT_SETTING.circ.hold_expire_alert_interval',
    'UPDATE_ORG_UNIT_SETTING.circ.hold_expire_interval',
    'UPDATE_ORG_UNIT_SETTING.global.credit.allow',
    'UPDATE_ORG_UNIT_SETTING.circ.void_overdue_on_lost',
    'UPDATE_ORG_UNIT_SETTING.circ.hold_stalling.soft',
    'UPDATE_ORG_UNIT_SETTING.circ.hold_boundary.hard',
    'UPDATE_ORG_UNIT_SETTING.circ.hold_boundary.soft',
    'UPDATE_ORG_UNIT_SETTING.opac.barcode_regex',
    'UPDATE_ORG_UNIT_SETTING.global.password_regex',
    'UPDATE_ORG_UNIT_SETTING.circ.item_checkout_history.max',
    'UPDATE_ORG_UNIT_SETTING.circ.reshelving_complete.interval',
    'UPDATE_ORG_UNIT_SETTING.circ.selfcheck.patron_login_timeout',
    'UPDATE_ORG_UNIT_SETTING.circ.selfcheck.alert_on_checkout_event',
    'UPDATE_ORG_UNIT_SETTING.circ.selfcheck.require_patron_password',
    'UPDATE_ORG_UNIT_SETTING.global.juvenile_age_threshold',
    'UPDATE_ORG_UNIT_SETTING.cat.bib.keep_on_empty',
    'UPDATE_ORG_UNIT_SETTING.cat.bib.alert_on_empty',
    'UPDATE_ORG_UNIT_SETTING.patron.password.use_phone',
    'SET_CIRC_CLAIMS_RETURNED.override'
);

SELECT SETVAL('permission.perm_list_id_seq'::TEXT, GREATEST( (SELECT MAX(id) FROM permission.perm_list), 1000 ));

UPDATE permission.grp_perm_map SET perm = perm + 1000 WHERE perm NOT IN ( SELECT id FROM permission.perm_list );
UPDATE permission.usr_perm_map SET perm = perm + 1000 WHERE perm NOT IN ( SELECT id FROM permission.perm_list );

COMMIT;

