1 ADD_FOREIGN_KEYS = """ 2 ALTER TABLE tests MODIFY COLUMN job_idx int(10) unsigned NOT NULL; 3 4 DELETE FROM tests WHERE job_idx NOT IN (SELECT job_idx FROM jobs); 5 6 ALTER TABLE tests ADD CONSTRAINT tests_to_jobs_ibfk 7 FOREIGN KEY (job_idx) REFERENCES jobs (job_idx); 8 """ 9 10 DROP_FOREIGN_KEYS = """ 11 ALTER TABLE tests DROP FOREIGN KEY tests_to_jobs_ibfk; 12 ALTER TABLE tests MODIFY COLUMN job_idx int(11) NOT NULL; 13 """ 14 15 def migrate_up(mgr): 16 mgr.execute_script(ADD_FOREIGN_KEYS) 17 18 def migrate_down(mgr): 19 mgr.execute_script(DROP_FOREIGN_KEYS) 20