Commit 186f1171a319a0e9f773819ae178f6a7860ba5ab
1 parent
84634b4d
KTS-1533
"Issues when trying to introduce db constraints during upgrades" Fixed. Deleting broken references prior to applying constraints. Reviewed by: Kevin Fourie git-svn-id: https://kt-dms.svn.sourceforge.net/svnroot/kt-dms/trunk@6204 c91229c3-7414-0410-bfa2-8a42b809f60b
Showing
1 changed file
with
164 additions
and
0 deletions
sql/mysql/upgrade/2.99.2/1-constraints.sql
| 1 | 1 | SET FOREIGN_KEY_CHECKS=0; |
| 2 | 2 | ALTER TABLE `document_type_fieldsets_link` ADD INDEX `document_type_id` (`document_type_id`); |
| 3 | 3 | ALTER TABLE `document_type_fieldsets_link` ADD INDEX `fieldset_id` (`fieldset_id`); |
| 4 | + | |
| 5 | +-- CLEAROUT ANY BROKEN RECORDS PRIOR TO ASSIGNING CONSTRAINT | |
| 6 | + | |
| 7 | +DELETE FROM `document_type_fieldsets_link` as dtfl USING `document_type_fieldsets_link` as dtfl, document_types_lookup | |
| 8 | + WHERE not exists(select 1 from `document_types_lookup` as dtl where dtfl.document_type_id = dtl.id); | |
| 9 | + | |
| 4 | 10 | ALTER TABLE `document_type_fieldsets_link` ADD CONSTRAINT `document_type_fieldsets_link_ibfk_1` FOREIGN KEY (`document_type_id`) REFERENCES `document_types_lookup` (`id`) ON DELETE CASCADE; |
| 11 | + | |
| 12 | +-- CLEAROUT ANY BROKEN RECORDS PRIOR TO ASSIGNING CONSTRAINT | |
| 13 | + | |
| 14 | +DELETE FROM `document_type_fieldsets_link` as dtfl USING `document_type_fieldsets_link` as dtfl, fieldsets | |
| 15 | + WHERE not exists(select 1 from `fieldsets` as fs where dtfl.fieldset_id = fs.id); | |
| 16 | + | |
| 5 | 17 | ALTER TABLE `document_type_fieldsets_link` ADD CONSTRAINT `document_type_fieldsets_link_ibfk_2` FOREIGN KEY (`fieldset_id`) REFERENCES `fieldsets` (`id`) ON DELETE CASCADE; |
| 18 | + | |
| 19 | +-- CLEAROUT ANY BROKEN RECORDS PRIOR TO ASSIGNING CONSTRAINT | |
| 20 | + | |
| 21 | +DELETE FROM `field_orders` as fo USING `field_orders` as fo, document_fields | |
| 22 | + WHERE not exists(select 1 from `document_fields` as df where fo.parent_field_id = df.id); | |
| 23 | + | |
| 6 | 24 | ALTER TABLE `field_orders` ADD CONSTRAINT `field_orders_ibfk_1` FOREIGN KEY (`parent_field_id`) REFERENCES `document_fields` (`id`) ON DELETE CASCADE; |
| 25 | + | |
| 26 | +-- CLEAROUT ANY BROKEN RECORDS PRIOR TO ASSIGNING CONSTRAINT | |
| 27 | + | |
| 28 | +DELETE FROM `field_orders` as fo USING `field_orders` as fo, document_fields | |
| 29 | + WHERE not exists(select 1 from `document_fields` as df where fo.child_field_id = df.id); | |
| 30 | + | |
| 7 | 31 | ALTER TABLE `field_orders` ADD CONSTRAINT `field_orders_ibfk_2` FOREIGN KEY (`child_field_id`) REFERENCES `document_fields` (`id`) ON DELETE CASCADE; |
| 32 | + | |
| 33 | +-- CLEAROUT ANY BROKEN RECORDS PRIOR TO ASSIGNING CONSTRAINT | |
| 34 | + | |
| 35 | +DELETE FROM `field_orders` as fo USING `field_orders` as fo, fieldsets | |
| 36 | + WHERE not exists(select 1 from `fieldsets` as fs where fo.fieldset_id = fs.id); | |
| 37 | + | |
| 8 | 38 | ALTER TABLE `field_orders` ADD CONSTRAINT `field_orders_ibfk_3` FOREIGN KEY (`fieldset_id`) REFERENCES `fieldsets` (`id`) ON DELETE CASCADE; |
| 39 | + | |
| 40 | +-- CLEAROUT ANY BROKEN RECORDS PRIOR TO ASSIGNING CONSTRAINT | |
| 41 | + | |
| 42 | +DELETE FROM `field_value_instances` as fvi USING `field_value_instances` as fvi, metadata_lookup | |
| 43 | + WHERE not exists(select 1 from `metadata_lookup` as ml where fvi.field_value_id = ml.id); | |
| 44 | + | |
| 9 | 45 | ALTER TABLE `field_value_instances` ADD CONSTRAINT `field_value_instances_ibfk_2` FOREIGN KEY (`field_value_id`) REFERENCES `metadata_lookup` (`id`) ON DELETE CASCADE; |
| 46 | + | |
| 47 | +-- CLEAROUT ANY BROKEN RECORDS PRIOR TO ASSIGNING CONSTRAINT | |
| 48 | + | |
| 49 | +DELETE FROM `field_value_instances` as fvi USING `field_value_instances` as fvi, field_behaviours | |
| 50 | + WHERE not exists(select 1 from `field_behaviours` as fb where fvi.behaviour_id = fb.id); | |
| 51 | + | |
| 10 | 52 | ALTER TABLE `field_value_instances` ADD CONSTRAINT `field_value_instances_ibfk_3` FOREIGN KEY (`behaviour_id`) REFERENCES `field_behaviours` (`id`) ON DELETE CASCADE; |
| 53 | + | |
| 54 | +-- CLEAROUT ANY BROKEN RECORDS PRIOR TO ASSIGNING CONSTRAINT | |
| 55 | + | |
| 56 | +DELETE FROM `field_value_instances` as fvi USING `field_value_instances` as fvi, document_fields | |
| 57 | + WHERE not exists(select 1 from `document_fields` as df where fvi.field_id = df.id); | |
| 58 | + | |
| 11 | 59 | ALTER TABLE `field_value_instances` ADD CONSTRAINT `field_value_instances_ibfk_1` FOREIGN KEY (`field_id`) REFERENCES `document_fields` (`id`) ON DELETE CASCADE; |
| 60 | + | |
| 61 | + | |
| 12 | 62 | ALTER TABLE `fieldsets` ADD INDEX `master_field` (`master_field`); |
| 63 | + | |
| 64 | +-- CLEAROUT ANY BROKEN RECORDS PRIOR TO ASSIGNING CONSTRAINT | |
| 65 | + | |
| 66 | +DELETE FROM `fieldsets` as fs USING `fieldsets` as fs, document_fields | |
| 67 | + WHERE not exists(select 1 from `document_fields` as df where fs.master_field = df.id); | |
| 68 | + | |
| 13 | 69 | ALTER TABLE `fieldsets` ADD CONSTRAINT `fieldsets_ibfk_1` FOREIGN KEY (`master_field`) REFERENCES `document_fields` (`id`) ON DELETE SET NULL; |
| 70 | + | |
| 14 | 71 | ALTER TABLE `permission_assignments` ADD INDEX `permission_descriptor_id` (`permission_descriptor_id`); |
| 72 | + | |
| 73 | +-- CLEAROUT ANY BROKEN RECORDS PRIOR TO ASSIGNING CONSTRAINT | |
| 74 | + | |
| 75 | +DELETE FROM `permission_assignments` as pa USING `permission_assignments` as pa, permission_descriptors | |
| 76 | + WHERE not exists(select 1 from `permission_descriptors` as pd where pa.permission_descriptor_id = pd.id); | |
| 77 | + | |
| 15 | 78 | ALTER TABLE `permission_assignments` ADD CONSTRAINT `permission_assignments_ibfk_3` FOREIGN KEY (`permission_descriptor_id`) REFERENCES `permission_descriptors` (`id`) ON DELETE CASCADE; |
| 79 | + | |
| 80 | +-- CLEAROUT ANY BROKEN RECORDS PRIOR TO ASSIGNING CONSTRAINT | |
| 81 | + | |
| 82 | +DELETE FROM `permission_assignments` as pa USING `permission_assignments` as pa, permissions | |
| 83 | + WHERE not exists(select 1 from `permissions` as p where pa.permission_id =p.id); | |
| 84 | + | |
| 16 | 85 | ALTER TABLE `permission_assignments` ADD CONSTRAINT `permission_assignments_ibfk_1` FOREIGN KEY (`permission_id`) REFERENCES `permissions` (`id`) ON DELETE CASCADE; |
| 86 | + | |
| 87 | +-- CLEAROUT ANY BROKEN RECORDS PRIOR TO ASSIGNING CONSTRAINT | |
| 88 | + | |
| 89 | +DELETE FROM `permission_assignments` as pa USING `permission_assignments` as pa, permission_objects | |
| 90 | + WHERE not exists(select 1 from `permission_objects` as po where pa.permission_object_id =po.id); | |
| 91 | + | |
| 17 | 92 | ALTER TABLE `permission_assignments` ADD CONSTRAINT `permission_assignments_ibfk_2` FOREIGN KEY (`permission_object_id`) REFERENCES `permission_objects` (`id`) ON DELETE CASCADE; |
| 93 | + | |
| 94 | +-- CLEAROUT ANY BROKEN RECORDS PRIOR TO ASSIGNING CONSTRAINT | |
| 95 | + | |
| 96 | +DELETE FROM `permission_descriptor_groups` as pdg USING `permission_descriptor_groups` as pdg, permission_descriptors | |
| 97 | + WHERE not exists(select 1 from `permission_descriptors` as pd where pdg.descriptor_id =pd.id); | |
| 98 | + | |
| 18 | 99 | ALTER TABLE `permission_descriptor_groups` ADD CONSTRAINT `permission_descriptor_groups_ibfk_1` FOREIGN KEY (`descriptor_id`) REFERENCES `permission_descriptors` (`id`) ON DELETE CASCADE; |
| 100 | + | |
| 101 | +-- CLEAROUT ANY BROKEN RECORDS PRIOR TO ASSIGNING CONSTRAINT | |
| 102 | + | |
| 103 | +DELETE FROM `permission_descriptor_groups` as pdg USING `permission_descriptor_groups` as pdg, groups_lookup | |
| 104 | + WHERE not exists(select 1 from `groups_lookup` as gl where pdg.group_id = gl.id); | |
| 105 | + | |
| 19 | 106 | ALTER TABLE `permission_descriptor_groups` ADD CONSTRAINT `permission_descriptor_groups_ibfk_2` FOREIGN KEY (`group_id`) REFERENCES `groups_lookup` (`id`) ON DELETE CASCADE; |
| 107 | + | |
| 20 | 108 | ALTER TABLE `permission_lookup_assignments` ADD INDEX `permission_descriptor_id` (`permission_descriptor_id`); |
| 109 | + | |
| 110 | +-- CLEAROUT ANY BROKEN RECORDS PRIOR TO ASSIGNING CONSTRAINT | |
| 111 | + | |
| 112 | +DELETE FROM `permission_lookup_assignments` as pla USING `permission_lookup_assignments` as pla, permissions | |
| 113 | + WHERE not exists(select 1 from `permissions` as p where pla.permission_id = p.id); | |
| 114 | + | |
| 21 | 115 | ALTER TABLE `permission_lookup_assignments` ADD CONSTRAINT `permission_lookup_assignments_ibfk_1` FOREIGN KEY (`permission_id`) REFERENCES `permissions` (`id`) ON DELETE CASCADE; |
| 116 | + | |
| 117 | +-- CLEAROUT ANY BROKEN RECORDS PRIOR TO ASSIGNING CONSTRAINT | |
| 118 | + | |
| 119 | +DELETE FROM `permission_lookup_assignments` as pla USING `permission_lookup_assignments` as pla, permission_descriptors | |
| 120 | + WHERE not exists(select 1 from `permission_descriptors` as pd where pla.permission_descriptor_id = pd.id); | |
| 121 | + | |
| 22 | 122 | ALTER TABLE `permission_lookup_assignments` ADD CONSTRAINT `permission_lookup_assignments_ibfk_3` FOREIGN KEY (`permission_descriptor_id`) REFERENCES `permission_descriptors` (`id`) ON DELETE CASCADE; |
| 123 | + | |
| 124 | +-- CLEAROUT ANY BROKEN RECORDS PRIOR TO ASSIGNING CONSTRAINT | |
| 125 | + | |
| 126 | +DELETE FROM `permission_lookup_assignments` as pla USING `permission_lookup_assignments` as pla, permission_lookups | |
| 127 | + WHERE not exists(select 1 from `permission_lookups` as pl where pla.permission_lookup_id = pl.id); | |
| 128 | + | |
| 23 | 129 | ALTER TABLE `permission_lookup_assignments` ADD CONSTRAINT `permission_lookup_assignments_ibfk_2` FOREIGN KEY (`permission_lookup_id`) REFERENCES `permission_lookups` (`id`) ON DELETE CASCADE; |
| 130 | + | |
| 24 | 131 | ALTER TABLE `workflow_states` ADD INDEX `inform_descriptor_id` (`inform_descriptor_id`); |
| 132 | + | |
| 133 | + | |
| 134 | +-- CLEAROUT ANY BROKEN RECORDS PRIOR TO ASSIGNING CONSTRAINT | |
| 135 | + | |
| 136 | +UPDATE workflow_states | |
| 137 | + SET inform_descriptor_id = null | |
| 138 | + WHERE not exists(select 1 from `permission_descriptors` as pd where workflow_states.inform_descriptor_id =pd.id); | |
| 139 | + | |
| 25 | 140 | ALTER TABLE `workflow_states` ADD CONSTRAINT `workflow_states_ibfk_2` FOREIGN KEY (`inform_descriptor_id`) REFERENCES `permission_descriptors` (`id`) ON DELETE SET NULL; |
| 141 | + | |
| 26 | 142 | ALTER TABLE `workflow_transitions` ADD INDEX `guard_group_id` (`guard_group_id`); |
| 27 | 143 | ALTER TABLE `workflow_transitions` ADD INDEX `guard_role_id` (`guard_role_id`); |
| 144 | + | |
| 28 | 145 | ALTER TABLE `workflow_transitions` DROP FOREIGN KEY `workflow_transitions_ibfk_2`; # was FOREIGN KEY (`target_state_id`) REFERENCES `workflow_states` (`id`) |
| 29 | 146 | ALTER TABLE `workflow_transitions` DROP FOREIGN KEY `workflow_transitions_ibfk_3`; # was FOREIGN KEY (`guard_permission_id`) REFERENCES `permissions` (`id`) |
| 30 | 147 | ALTER TABLE `workflow_transitions` DROP FOREIGN KEY `workflow_transitions_ibfk_1`; # was FOREIGN KEY (`workflow_id`) REFERENCES `workflows` (`id`) |
| 31 | 148 | ALTER TABLE `field_behaviours` DROP FOREIGN KEY `field_behaviours_ibfk_1`; |
| 149 | + | |
| 150 | +-- CLEAROUT ANY BROKEN RECORDS PRIOR TO ASSIGNING CONSTRAINT | |
| 151 | + | |
| 152 | +DELETE FROM `field_behaviours` as fb USING `field_behaviours` as fb, document_fields | |
| 153 | + WHERE not exists(select 1 from `document_fields` as df where fb.field_id = df.id); | |
| 154 | + | |
| 32 | 155 | ALTER TABLE `field_behaviours` ADD CONSTRAINT `field_behaviours_ibfk_1` FOREIGN KEY (`field_id`) REFERENCES `document_fields` (`id`) ON DELETE CASCADE; |
| 156 | + | |
| 157 | +-- CLEAROUT ANY BROKEN RECORDS PRIOR TO ASSIGNING CONSTRAINT | |
| 158 | + | |
| 159 | +UPDATE workflow_transitions | |
| 160 | + SET guard_group_id = null | |
| 161 | + WHERE not exists(select 1 from `groups_lookup` as gl where workflow_transitions.guard_group_id =gl.id); | |
| 162 | + | |
| 33 | 163 | ALTER TABLE `workflow_transitions` ADD CONSTRAINT `workflow_transitions_ibfk_48` FOREIGN KEY (`guard_group_id`) REFERENCES `groups_lookup` (`id`) ON DELETE SET NULL; |
| 164 | + | |
| 165 | +-- CLEAROUT ANY BROKEN RECORDS PRIOR TO ASSIGNING CONSTRAINT | |
| 166 | + | |
| 167 | +UPDATE workflow_transitions | |
| 168 | + SET guard_condition_id = null | |
| 169 | + WHERE not exists(select 1 from `saved_searches` as ss where workflow_transitions.guard_condition_id =ss.id); | |
| 170 | + | |
| 34 | 171 | ALTER TABLE `workflow_transitions` ADD CONSTRAINT `workflow_transitions_ibfk_50` FOREIGN KEY (`guard_condition_id`) REFERENCES `saved_searches` (`id`) ON DELETE SET NULL; |
| 172 | + | |
| 173 | +-- CLEAROUT ANY BROKEN RECORDS PRIOR TO ASSIGNING CONSTRAINT | |
| 174 | + | |
| 175 | +DELETE FROM `workflow_transitions` as wt USING `workflow_transitions` as wt, workflow_states | |
| 176 | + WHERE not exists(select 1 from `workflow_states` as ws where wt.target_state_id = ws.id); | |
| 177 | + | |
| 35 | 178 | ALTER TABLE `workflow_transitions` ADD CONSTRAINT `workflow_transitions_ibfk_46` FOREIGN KEY (`target_state_id`) REFERENCES `workflow_states` (`id`) ON DELETE CASCADE; |
| 179 | + | |
| 180 | +-- CLEAROUT ANY BROKEN RECORDS PRIOR TO ASSIGNING CONSTRAINT | |
| 181 | + | |
| 182 | +UPDATE workflow_transitions | |
| 183 | + SET guard_permission_id = null | |
| 184 | + WHERE not exists(select 1 from `permissions` as p where workflow_transitions.guard_permission_id =p.id); | |
| 185 | + | |
| 36 | 186 | ALTER TABLE `workflow_transitions` ADD CONSTRAINT `workflow_transitions_ibfk_47` FOREIGN KEY (`guard_permission_id`) REFERENCES `permissions` (`id`) ON DELETE SET NULL; |
| 187 | + | |
| 188 | +-- CLEAROUT ANY BROKEN RECORDS PRIOR TO ASSIGNING CONSTRAINT | |
| 189 | + | |
| 190 | +UPDATE workflow_transitions | |
| 191 | + SET guard_role_id = null | |
| 192 | + WHERE not exists(select 1 from `roles` as r where workflow_transitions.guard_role_id =r.id); | |
| 193 | + | |
| 37 | 194 | ALTER TABLE `workflow_transitions` ADD CONSTRAINT `workflow_transitions_ibfk_49` FOREIGN KEY (`guard_role_id`) REFERENCES `roles` (`id`) ON DELETE SET NULL; |
| 195 | + | |
| 196 | +-- CLEAROUT ANY BROKEN RECORDS PRIOR TO ASSIGNING CONSTRAINT | |
| 197 | + | |
| 198 | +DELETE FROM `workflow_transitions` as wt USING `workflow_transitions` as wt, workflows | |
| 199 | + WHERE not exists(select 1 from `workflows` as w where wt.workflow_id = w.id); | |
| 200 | + | |
| 38 | 201 | ALTER TABLE `workflow_transitions` ADD CONSTRAINT `workflow_transitions_ibfk_45` FOREIGN KEY (`workflow_id`) REFERENCES `workflows` (`id`) ON DELETE CASCADE; |
| 202 | + | |
| 39 | 203 | SET FOREIGN_KEY_CHECKS=1; | ... | ... |