Home | History | Annotate | Download | only in migrations
      1 UP_SQL = """
      2 ALTER TABLE hosts
      3 ADD CONSTRAINT hosts_locked_by_fk FOREIGN KEY
      4 (locked_by_id) REFERENCES users(id)
      5 ON DELETE NO ACTION;
      6 
      7 ALTER TABLE host_queue_entries
      8 ADD CONSTRAINT host_queue_entries_job_id_fk FOREIGN KEY
      9 (job_id) REFERENCES jobs(id)
     10 ON DELETE NO ACTION;
     11 
     12 INSERT INTO hosts (hostname, invalid, protection, dirty)
     13 VALUES ('__missing_host__', 1, 0, 1);
     14 
     15 UPDATE host_queue_entries AS hqe
     16     LEFT OUTER JOIN hosts ON (hqe.host_id = hosts.id)
     17 SET hqe.host_id = (SELECT id FROM hosts WHERE hostname = '__missing_host__')
     18 WHERE hqe.host_id IS NOT NULL AND hosts.id IS NULL;
     19 
     20 ALTER TABLE host_queue_entries
     21 ADD CONSTRAINT host_queue_entries_host_id_fk FOREIGN KEY
     22 (host_id) REFERENCES hosts(id)
     23 ON DELETE NO ACTION;
     24 
     25 ALTER TABLE host_queue_entries
     26 ADD CONSTRAINT host_queue_entries_meta_host_fk FOREIGN KEY
     27 (meta_host) REFERENCES labels(id)
     28 ON DELETE NO ACTION;
     29 
     30 ALTER TABLE aborted_host_queue_entries
     31 ADD CONSTRAINT aborted_host_queue_entries_queue_entry_id_fk FOREIGN KEY
     32 (queue_entry_id) REFERENCES host_queue_entries(id)
     33 ON DELETE NO ACTION;
     34 
     35 ALTER TABLE aborted_host_queue_entries
     36 ADD CONSTRAINT aborted_host_queue_entries_aborted_by_id_fk FOREIGN KEY
     37 (aborted_by_id) REFERENCES users(id)
     38 ON DELETE NO ACTION;
     39 
     40 ALTER TABLE recurring_run
     41 ADD CONSTRAINT recurring_run_job_id_fk FOREIGN KEY
     42 (job_id) REFERENCES jobs(id)
     43 ON DELETE NO ACTION;
     44 
     45 ALTER TABLE recurring_run
     46 ADD CONSTRAINT recurring_run_owner_id_fk FOREIGN KEY
     47 (owner_id) REFERENCES users(id)
     48 ON DELETE NO ACTION;
     49 """
     50 
     51 DOWN_SQL = """
     52 ALTER TABLE hosts
     53 DROP FOREIGN KEY hosts_locked_by_fk;
     54 
     55 ALTER TABLE host_queue_entries
     56 DROP FOREIGN KEY host_queue_entries_job_id_fk;
     57 
     58 ALTER TABLE host_queue_entries
     59 DROP FOREIGN KEY host_queue_entries_host_id_fk;
     60 
     61 ALTER TABLE host_queue_entries
     62 DROP FOREIGN KEY host_queue_entries_meta_host_fk;
     63 
     64 ALTER TABLE aborted_host_queue_entries
     65 DROP FOREIGN KEY aborted_host_queue_entries_queue_entry_id_fk;
     66 
     67 ALTER TABLE aborted_host_queue_entries
     68 DROP FOREIGN KEY aborted_host_queue_entries_aborted_by_id_fk;
     69 
     70 ALTER TABLE recurring_run
     71 DROP FOREIGN KEY recurring_run_job_id_fk;
     72 
     73 ALTER TABLE recurring_run
     74 DROP FOREIGN KEY recurring_run_owner_id_fk;
     75 """
     76