--
-- Title:      Ensure that all Foreign Key indexes are present
-- Database:   Generic
-- Since:      V2.1 Schema 64
-- Author:     Derek Hulley
--
-- Please contact support@alfresco.com if you need assistance with the upgrade.
--
-- All dialects, with the exception of MySQL, will not have FK-related indexes
-- automatically generated by Hibernate.  Quite a few indexes here will be present
-- in the database already, but the list depends on the particular upgrade path
-- taken.  In order to reduce the number of scripts that need to be run to cater for
-- these diverse scenarios, this script just attempts to create each and every
-- index that would be created for a new install.  The statements are optional, apart
-- from the control statements to check for the presence of the required tables.

-- JBPM
--   JBPM tables have explicit indexes where required.  Adding FK indexes here
--   leads to duplication, and on MySQL, this is exactly what happens.

-- Repository
SELECT COUNT(*) FROM alf_access_control_entry;
CREATE INDEX FKFFF41F9960601995 ON alf_access_control_entry (permission_id);(optional)
CREATE INDEX FKFFF41F99B25A50BF ON alf_access_control_entry (authority_id);(optional)
CREATE INDEX FKFFF41F99B9553F6C ON alf_access_control_entry (acl_id);(optional)
SELECT COUNT(*) FROM alf_auth_ext_keys;
CREATE INDEX FK8A749A657B7FDE43 ON alf_auth_ext_keys (id);(optional)
SELECT COUNT(*) FROM alf_child_assoc;
CREATE INDEX FKFFC5468E74173FF4 ON alf_child_assoc (child_node_id);(optional)
CREATE INDEX FKFFC5468E8E50E582 ON alf_child_assoc (parent_node_id);(optional)
SELECT COUNT(*) FROM alf_node;
CREATE INDEX FK60EFB626B9553F6C ON alf_node (acl_id);(optional)
CREATE INDEX FK60EFB626D24ADD25 ON alf_node (protocol, identifier);(optional)
SELECT COUNT(*) FROM alf_node_properties;
CREATE INDEX FK7D4CF8EC7F2C8017 ON alf_node_properties (node_id);(optional)
CREATE INDEX FK7D4CF8EC40E780DC ON alf_node_properties (attribute_value);(optional)
SELECT COUNT(*) FROM alf_node_aspects;
CREATE INDEX FKD654E027F2C8017  ON alf_node_aspects (node_id);(optional)
SELECT COUNT(*) FROM alf_node_assoc;
CREATE INDEX FKE1A550BCB69C43F3 ON alf_node_assoc (source_node_id);(optional)
CREATE INDEX FKE1A550BCA8FC7769 ON alf_node_assoc (target_node_id);(optional)
SELECT COUNT(*) FROM alf_node_status;
CREATE INDEX FK71C2002B7F2C8017 ON alf_node_status (node_id);(optional)
SELECT COUNT(*) FROM alf_store;
CREATE INDEX FKBD4FF53D22DBA5BA ON alf_store (root_node_id);(optional)

--
-- Transaction tables
--
CREATE INDEX FK71C2002B9E57C13D ON alf_node_status (transaction_id);(optional)
SELECT COUNT(*) FROM alf_transaction;
CREATE INDEX FKB8761A3A9AE340B7 ON alf_transaction (server_id);(optional)

--
-- Audit tables
--
SELECT COUNT(*) FROM alf_audit_fact;
CREATE INDEX FKEAD1817484342E39 ON alf_audit_fact (audit_date_id);(optional)
CREATE INDEX FKEAD18174A0F9B8D9 ON alf_audit_fact (audit_source_id);(optional)
CREATE INDEX FKEAD18174F524CFD7 ON alf_audit_fact (audit_conf_id);(optional)

--
-- Attribute tables
--
SELECT COUNT(*) FROM alf_attributes;
CREATE INDEX fk_attr_n_acl      ON alf_attributes (acl_id);(optional)
SELECT COUNT(*) FROM alf_global_attributes;
CREATE INDEX FK64D0B9CF69B9F16A ON alf_global_attributes (attribute);(optional)
SELECT COUNT(*) FROM alf_list_attribute_entries;
CREATE INDEX FKC7D52FB0ACD8822C ON alf_list_attribute_entries (list_id);(optional)
CREATE INDEX FKC7D52FB02C5AB86C ON alf_list_attribute_entries (attribute_id);(optional)
SELECT COUNT(*) FROM alf_map_attribute_entries;
CREATE INDEX FK335CAE262C5AB86C ON alf_map_attribute_entries (attribute_id);(optional)
CREATE INDEX FK335CAE26AEAC208C ON alf_map_attribute_entries (map_id);(optional)

-- AVM
SELECT COUNT(*) FROM avm_aspects;
CREATE INDEX fk_avm_asp_node    ON avm_aspects (node_id);(optional)
SELECT COUNT(*) FROM avm_aspects_new;
CREATE INDEX FKD3FD9F95EDCD4A96 ON avm_aspects_new (id);(optional)
SELECT COUNT(*) FROM avm_child_entries;
CREATE INDEX fk_avm_ce_child    ON avm_child_entries (child_id);(optional)
CREATE INDEX fk_avm_ce_parent   ON avm_child_entries (parent_id);(optional)
SELECT COUNT(*) FROM avm_history_links;
CREATE INDEX fk_avm_hl_desc     ON avm_history_links (descendent);(optional)
CREATE INDEX fk_avm_hl_ancestor ON avm_history_links (ancestor);(optional)
SELECT COUNT(*) FROM avm_merge_links;
CREATE INDEX fk_avm_ml_from     ON avm_merge_links (mfrom);(optional)
CREATE INDEX fk_avm_ml_to       ON avm_merge_links (mto);(optional)
SELECT COUNT(*) FROM avm_node_properties_new;
CREATE INDEX FK44A37C8A6BD529F3 ON avm_node_properties_new (node_id);(optional)
CREATE INDEX fk_avm_np_node     ON avm_node_properties (node_id);(optional)
SELECT COUNT(*) FROM avm_nodes;
CREATE INDEX fk_avm_n_acl       ON avm_nodes (acl_id);(optional)
CREATE INDEX fk_avm_n_store     ON avm_nodes (store_new_id);(optional)
SELECT COUNT(*) FROM avm_store_properties;
CREATE INDEX fk_avm_sp_store    ON avm_store_properties (avm_store_id);(optional)
SELECT COUNT(*) FROM avm_stores;
CREATE INDEX fk_avm_s_root      ON avm_stores (current_root_id);(optional)
SELECT COUNT(*) FROM avm_version_roots;
CREATE INDEX fk_avm_vr_store    ON avm_version_roots (avm_store_id);(optional)
CREATE INDEX fk_avm_vr_root     ON avm_version_roots (root_id);(optional)


--
-- Record script finish
--
DELETE FROM alf_applied_patch WHERE id = 'patch.db-V2.1-FKIndexes';
INSERT INTO alf_applied_patch
  (id, description, fixes_from_schema, fixes_to_schema, applied_to_schema, target_schema, applied_on_date, applied_to_server, was_executed, succeeded, report)
  VALUES
  (
    'patch.db-V2.1-FKIndexes', 'Manually executed script upgrade V2.1: Ensure existence of V2.1 FK indexes',
    0, 63, -1, 64, null, 'UNKOWN', 1, 1, 'Script completed'
  );