Update TLE if exists

This commit is contained in:
Andreas Kristiansen
2024-12-08 08:24:33 +01:00
parent d5f238f6d4
commit daf9ebb09a
2 changed files with 20 additions and 3 deletions

View File

@@ -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());
}
}
}

View File

@@ -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));
}
}
}