From daf9ebb09aefc76ece59d8011ca32c78c0d33e64 Mon Sep 17 00:00:00 2001 From: Andreas Kristiansen <6977712+AndreasK79@users.noreply.github.com> Date: Sun, 8 Dec 2024 08:24:33 +0100 Subject: [PATCH] Update TLE if exists --- application/migrations/230_tle_cron.php | 11 +++++++++++ application/models/Update_model.php | 12 +++++++++--- 2 files changed, 20 insertions(+), 3 deletions(-) diff --git a/application/migrations/230_tle_cron.php b/application/migrations/230_tle_cron.php index e41dfa121..84e1c683d 100644 --- a/application/migrations/230_tle_cron.php +++ b/application/migrations/230_tle_cron.php @@ -17,12 +17,15 @@ class Migration_tle_cron extends CI_Migration { )); $this->db->insert_batch('cron', $data); } + + $this->dbtry("ALTER TABLE tle ADD CONSTRAINT tle_unique_satelliteid_FK FOREIGN KEY (satelliteid) REFERENCES satellite (id) ON DELETE CASCADE ON UPDATE RESTRICT;"); } public function down() { if ($this->chk4cron('update_tle') > 0) { $this->db->query("delete from cron where id='update_update_tle'"); } + $this->dbtry("alter table tle drop foreign key tle_unique_satelliteid_FK;"); } function chk4cron($cronkey) { @@ -30,4 +33,12 @@ class Migration_tle_cron extends CI_Migration { $row = $query->row(); return $row->cid ?? 0; } + + function dbtry($what) { + try { + $this->db->query($what); + } catch (Exception $e) { + log_message("error", "Something gone wrong while altering FKs: ".$e." // Executing: ".$this->db->last_query()); + } + } } diff --git a/application/models/Update_model.php b/application/models/Update_model.php index 9763ff65c..ad24d3335 100644 --- a/application/models/Update_model.php +++ b/application/models/Update_model.php @@ -329,7 +329,6 @@ class Update_model extends CI_Model { if ($response === false) { echo 'Error: ' . curl_error($curl); } else { - $this->db->empty_table("tle"); // Split the response into an array of lines $lines = explode("\n", $response); @@ -345,8 +344,15 @@ class Update_model extends CI_Model { $satname = $lines[$i]; $tleline1 = $lines[$i + 1]; $tleline2 = $lines[$i + 2]; - $sql = "INSERT INTO tle (satelliteid, tle) select id, ? from satellite where name = ? or displayname = ?"; - $this->db->query($sql,array($tleline1."\n".$tleline2,$satname,$satname)); + $sql = " + INSERT INTO tle (satelliteid, tle) + SELECT id, ? + FROM satellite + WHERE name = ? OR displayname = ? + ON DUPLICATE KEY UPDATE + tle = VALUES(tle), updated = now() + "; + $this->db->query($sql, array($tleline1 . "\n" . $tleline2, $satname, $satname)); } } }