Add migration for cascade delete

This commit is contained in:
Andreas Kristiansen
2025-08-12 19:22:16 +02:00
parent a4b8f25af8
commit ed57f915f8
2 changed files with 26 additions and 1 deletions

View File

@@ -22,7 +22,7 @@ $config['migration_enabled'] = TRUE;
|
*/
$config['migration_version'] = 251;
$config['migration_version'] = 252;
/*
|--------------------------------------------------------------------------

View File

@@ -0,0 +1,25 @@
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Migration_add_oqrs_cascade extends CI_Migration {
public function up() {
$this->dbtry("ALTER TABLE oqrs MODIFY COLUMN qsoid BIGINT(20) UNSIGNED;");
$this->dbtry("ALTER TABLE oqrs ADD UNIQUE (qsoid)");
$this->dbtry("ALTER TABLE oqrs ADD CONSTRAINT oqrs_logbook_fk FOREIGN KEY (qsoid) REFERENCES " . $this->config->item('table_name') . " (COL_PRIMARY_KEY) ON DELETE CASCADE ON UPDATE RESTRICT;");
}
public function down(){
$this->dbtry("ALTER TABLE oqrs MODIFY COLUMN qsoid INT;");
$this->dbtry("ALTER TABLE oqrs DROP UNIQUE (qsoid);");
$this->dbtry("ALTER TABLE oqrs DROP FOREIGN KEY oqrs_logbook_fk;");
}
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());
}
}
}