1 # Adding the unique constraint will lead to the index being destroyed and a new 2 # unique index being created. 3 # To not rely on this implicit behavior, we explicitly delete the old index, 4 # and then create the new indexes. 5 # This is not really needed for the upwards migration, but if we can't be sure 6 # about the indexes names, it gets harder to do the DOWN migration later. 7 # Therefore we do this magic manually. 8 UP_SQL = """ 9 ALTER TABLE afe_shards_labels DROP FOREIGN KEY shard_label_id_fk; 10 ALTER TABLE afe_shards_labels DROP INDEX shard_label_id_fk; 11 ALTER TABLE `afe_shards_labels` ADD UNIQUE `shard_label_id_uc` (`label_id`); 12 ALTER TABLE `afe_shards_labels` ADD CONSTRAINT shard_label_id_fk 13 FOREIGN KEY (`label_id`) REFERENCES `afe_labels` (`id`); 14 """ 15 16 # Normally removing unique constraints is done just by deleting the index. 17 # This doesn't work here, as the index is also needed for the foreign key. 18 # Making an index back non-unique doesn't work in mysql. 19 # Therefore delete the foreign key, delete the index, re-add the foreign key. 20 DOWN_SQL = """ 21 ALTER TABLE afe_shards_labels DROP FOREIGN KEY shard_label_id_fk; 22 ALTER TABLE afe_shards_labels DROP INDEX shard_label_id_uc; 23 ALTER TABLE `afe_shards_labels` ADD CONSTRAINT shard_label_id_fk 24 FOREIGN KEY (`label_id`) REFERENCES `afe_labels` (`id`); 25 """ 26