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