1 ADD_FOREIGN_KEYS = """ 2 ALTER TABLE test_labels_tests MODIFY COLUMN test_id int(10) unsigned NOT NULL; 3 4 DELETE FROM test_labels_tests 5 WHERE test_id NOT IN (SELECT test_idx FROM tests); 6 7 ALTER TABLE test_labels_tests ADD CONSTRAINT tests_labels_tests_ibfk_1 8 FOREIGN KEY (testlabel_id) REFERENCES test_labels (id); 9 10 ALTER TABLE test_labels_tests ADD CONSTRAINT tests_labels_tests_ibfk_2 11 FOREIGN KEY (test_id) REFERENCES tests (test_idx); 12 """ 13 14 DROP_FOREIGN_KEYS = """ 15 ALTER TABLE test_labels_tests DROP FOREIGN KEY tests_labels_tests_ibfk_1; 16 ALTER TABLE test_labels_tests DROP FOREIGN KEY tests_labels_tests_ibfk_2; 17 ALTER TABLE test_labels_tests MODIFY COLUMN test_id int(11) NOT NULL; 18 """ 19 20 def migrate_up(mgr): 21 mgr.execute_script(ADD_FOREIGN_KEYS) 22 23 def migrate_down(mgr): 24 mgr.execute_script(DROP_FOREIGN_KEYS) 25