From 58891bf50a6d766e20c336be69dde2552a6b3ad5 Mon Sep 17 00:00:00 2001
From: Alejandro Gallardo Escobar <alejandro.gallardo@artica.es>
Date: Thu, 2 Jul 2015 12:03:20 +0200
Subject: [PATCH] Moved a table to fix an error and added the change to the
 migrate and the oracle and postgres files

---
 .../pandoradb_migrate_5.1_to_6.0.mysql.sql    |  3 +
 pandora_console/pandoradb.oracle.sql          | 73 ++++++++++---------
 pandora_console/pandoradb.postgreSQL.sql      | 68 +++++++++--------
 pandora_console/pandoradb.sql                 | 68 ++++++++---------
 4 files changed, 112 insertions(+), 100 deletions(-)

diff --git a/pandora_console/extras/pandoradb_migrate_5.1_to_6.0.mysql.sql b/pandora_console/extras/pandoradb_migrate_5.1_to_6.0.mysql.sql
index f572e57ea2..ca5325b41a 100755
--- a/pandora_console/extras/pandoradb_migrate_5.1_to_6.0.mysql.sql
+++ b/pandora_console/extras/pandoradb_migrate_5.1_to_6.0.mysql.sql
@@ -143,3 +143,6 @@ CREATE  TABLE IF NOT EXISTS `tgis_map_has_tgis_map_con` (
 INSERT INTO `tgis_map_has_tgis_map_con` SELECT * FROM `tgis_map_has_tgis_map_connection`;
 DROP TABLE `tgis_map_has_tgis_map_connection`;
 
+ALTER TABLE `tmodule_relationship`
+	ADD COLUMN `id_rt` int(10) unsigned NOT NULL DEFAULT 0,
+	ADD FOREIGN KEY (`id_rt`) REFERENCES trecon_task(`id_rt`) ON DELETE CASCADE;
diff --git a/pandora_console/pandoradb.oracle.sql b/pandora_console/pandoradb.oracle.sql
index 4948c22ffe..ed29f8b3cb 100755
--- a/pandora_console/pandoradb.oracle.sql
+++ b/pandora_console/pandoradb.oracle.sql
@@ -747,11 +747,49 @@ CREATE TABLE tmodule_group (
 CREATE SEQUENCE tmodule_group_s INCREMENT BY 1 START WITH 1;
 CREATE OR REPLACE TRIGGER tmodule_group_inc BEFORE INSERT ON tmodule_group REFERENCING NEW AS NEW FOR EACH ROW BEGIN SELECT tmodule_group_s.nextval INTO :NEW.id_mg FROM dual; END;;
 
+-- This table was moved cause the `tmodule_relationship` will add
+-- a foreign key for the trecon_task(id_rt)
+-- ----------------------------------------------------------------------
+-- Table `trecon_task`
+-- ----------------------------------------------------------------------
+CREATE TABLE trecon_task (
+	id_rt NUMBER(10, 0) PRIMARY KEY,
+	name VARCHAR2(100) DEFAULT '',
+	description VARCHAR2(250) DEFAULT '',
+	subnet CLOB DEFAULT NULL,
+	id_network_profile NUMBER(10, 0) DEFAULT 0,
+	create_incident NUMBER(10, 0) DEFAULT 0,
+	id_group NUMBER(10, 0) DEFAULT 1,
+	utimestamp NUMBER(19, 0) DEFAULT 0,
+	status NUMBER(10, 0) DEFAULT 0,
+	interval_sweep NUMBER(10, 0) DEFAULT 0,
+	id_recon_server NUMBER(10, 0) DEFAULT 0,
+	id_os NUMBER(10, 0) DEFAULT 0,
+	recon_ports VARCHAR2(250) DEFAULT '',
+	snmp_community VARCHAR2(64) DEFAULT 'public',
+	id_recon_script NUMBER(10, 0),
+	field1 CLOB DEFAULT NULL,
+	field2 VARCHAR2(250) DEFAULT '',
+	field3 VARCHAR2(250) DEFAULT '',
+	field4 VARCHAR2(250) DEFAULT '',
+	os_detect NUMBER(5, 0) DEFAULT 1,
+	resolve_names NUMBER(5, 0) DEFAULT 1,
+	parent_detection NUMBER(5, 0) DEFAULT 1,
+	parent_recursion NUMBER(5, 0) DEFAULT 1,
+	disabled NUMBER(5, 0) DEFAULT 1,
+	macros CLOB DEFAULT ''
+);
+CREATE INDEX trecon_task_id_rec_serv_idx ON trecon_task(id_recon_server);
+
+CREATE SEQUENCE trecon_task_s INCREMENT BY 1 START WITH 1;
+CREATE OR REPLACE TRIGGER trecon_task_inc BEFORE INSERT ON trecon_task REFERENCING NEW AS NEW FOR EACH ROW BEGIN SELECT trecon_task_s.nextval INTO :NEW.id_rt FROM dual; END trecon_task_inc;;
+
 -- ----------------------------------------------------------------------
 -- Table `tmodule_relationship`
 -- ----------------------------------------------------------------------
 CREATE TABLE tmodule_relationship (
 	id NUMBER(10, 0) PRIMARY KEY,
+	id_rt NUMBER(10, 0) DEFAULT 0 REFERENCES trecon_task(id_rt) ON DELETE CASCADE,
 	module_a NUMBER(10, 0) REFERENCES tagente_modulo(id_agente_modulo) ON DELETE CASCADE,
 	module_b NUMBER(10, 0) REFERENCES tagente_modulo(id_agente_modulo) ON DELETE CASCADE,
 	disable_update NUMBER(1, 0) DEFAULT 0
@@ -927,41 +965,6 @@ CREATE TABLE trecon_script (
 CREATE SEQUENCE trecon_script_s INCREMENT BY 1 START WITH 1;
 CREATE OR REPLACE TRIGGER trecon_script_inc BEFORE INSERT ON trecon_script REFERENCING NEW AS NEW FOR EACH ROW BEGIN SELECT trecon_script_s.nextval INTO :NEW.id_recon_script FROM dual; END;;
 
--- ----------------------------------------------------------------------
--- Table `trecon_task`
--- ----------------------------------------------------------------------
-CREATE TABLE trecon_task (
-	id_rt NUMBER(10, 0) PRIMARY KEY,
-	name VARCHAR2(100) DEFAULT '',
-	description VARCHAR2(250) DEFAULT '',
-	subnet CLOB DEFAULT NULL,
-	id_network_profile NUMBER(10, 0) DEFAULT 0,
-	create_incident NUMBER(10, 0) DEFAULT 0,
-	id_group NUMBER(10, 0) DEFAULT 1,
-	utimestamp NUMBER(19, 0) DEFAULT 0,
-	status NUMBER(10, 0) DEFAULT 0,
-	interval_sweep NUMBER(10, 0) DEFAULT 0,
-	id_recon_server NUMBER(10, 0) DEFAULT 0,
-	id_os NUMBER(10, 0) DEFAULT 0,
-	recon_ports VARCHAR2(250) DEFAULT '',
-	snmp_community VARCHAR2(64) DEFAULT 'public',
-	id_recon_script NUMBER(10, 0),
-	field1 CLOB DEFAULT NULL,
-	field2 VARCHAR2(250) DEFAULT '',
-	field3 VARCHAR2(250) DEFAULT '',
-	field4 VARCHAR2(250) DEFAULT '',
-	os_detect NUMBER(5, 0) DEFAULT 1,
-	resolve_names NUMBER(5, 0) DEFAULT 1,
-	parent_detection NUMBER(5, 0) DEFAULT 1,
-	parent_recursion NUMBER(5, 0) DEFAULT 1,
-	disabled NUMBER(5, 0) DEFAULT 1,
-	macros CLOB DEFAULT ''
-);
-CREATE INDEX trecon_task_id_rec_serv_idx ON trecon_task(id_recon_server);
-
-CREATE SEQUENCE trecon_task_s INCREMENT BY 1 START WITH 1;
-CREATE OR REPLACE TRIGGER trecon_task_inc BEFORE INSERT ON trecon_task REFERENCING NEW AS NEW FOR EACH ROW BEGIN SELECT trecon_task_s.nextval INTO :NEW.id_rt FROM dual; END trecon_task_inc;;
-
 -- ----------------------------------------------------------------------
 -- Table `tserver`
 -- ----------------------------------------------------------------------
diff --git a/pandora_console/pandoradb.postgreSQL.sql b/pandora_console/pandoradb.postgreSQL.sql
index 95872fe493..269469d442 100755
--- a/pandora_console/pandoradb.postgreSQL.sql
+++ b/pandora_console/pandoradb.postgreSQL.sql
@@ -600,11 +600,47 @@ CREATE TABLE "tmodule_group" (
 	"name" varchar(150) NOT NULL default ''
 );
 
+-- This table was moved cause the "tmodule_relationship" will add
+-- a foreign key for the trecon_task(id_rt)
+-- ---------------------------------------------------------------------
+-- Table `trecon_task`
+-- ---------------------------------------------------------------------
+CREATE TABLE "trecon_task" (
+	"id_rt" SERIAL NOT NULL PRIMARY KEY,
+	"name" varchar(100) NOT NULL default '',
+	"description" varchar(250) NOT NULL default '',
+	"subnet" TEXT default NULL,
+	"id_network_profile" INTEGER NOT NULL default 0,
+	"create_incident" INTEGER NOT NULL default 0,
+	"id_group" INTEGER NOT NULL default 1,
+	"utimestamp" BIGINT NOT NULL default 0,
+	"status" INTEGER NOT NULL default 0,
+	"interval_sweep" INTEGER NOT NULL default 0,
+	"id_recon_server" INTEGER NOT NULL default 0,
+	"id_os" INTEGER NOT NULL default 0,
+	"recon_ports" varchar(250) NOT NULL default '',
+	"snmp_community" varchar(64) NOT NULL default 'public',
+	"id_recon_script" INTEGER,
+	"field1" TEXT default NULL,
+	"field2" varchar(250) NOT NULL default '',
+	"field3" varchar(250) NOT NULL default '',
+	"field4" varchar(250) NOT NULL default '',
+	"os_detect" SMALLINT NOT NULL default 1,
+	"resolve_names" SMALLINT NOT NULL default 1,
+	"parent_detection" SMALLINT NOT NULL default 1,
+	"parent_recursion" SMALLINT NOT NULL default 1,
+	"disabled" SMALLINT NOT NULL default 1,
+	"macros" TEXT NOT NULL default ''
+);
+CREATE INDEX "trecon_task_id_recon_server_idx" ON "trecon_task"("id_recon_server");
+
 -- ----------------------------------------------------------------------
 -- Table `tmodule_relationship`
 -- ----------------------------------------------------------------------
 CREATE TABLE "tmodule_relationship" (
 	"id" SERIAL NOT NULL PRIMARY KEY,
+	"id_rt" INTEGER NOT NULL REFERENCES trecon_task("id_rt")
+		ON DELETE CASCADE,
 	"module_a" INTEGER NOT NULL REFERENCES tagente_modulo("id_agente_modulo")
 		ON DELETE CASCADE,
 	"module_b" INTEGER NOT NULL REFERENCES tagente_modulo("id_agente_modulo")
@@ -758,38 +794,6 @@ CREATE TABLE "trecon_script" (
 	"macros" TEXT NOT NULL default ''
 );
 
--- ---------------------------------------------------------------------
--- Table `trecon_task`
--- ---------------------------------------------------------------------
-CREATE TABLE "trecon_task" (
-	"id_rt" SERIAL NOT NULL PRIMARY KEY,
-	"name" varchar(100) NOT NULL default '',
-	"description" varchar(250) NOT NULL default '',
-	"subnet" TEXT default NULL,
-	"id_network_profile" INTEGER NOT NULL default 0,
-	"create_incident" INTEGER NOT NULL default 0,
-	"id_group" INTEGER NOT NULL default 1,
-	"utimestamp" BIGINT NOT NULL default 0,
-	"status" INTEGER NOT NULL default 0,
-	"interval_sweep" INTEGER NOT NULL default 0,
-	"id_recon_server" INTEGER NOT NULL default 0,
-	"id_os" INTEGER NOT NULL default 0,
-	"recon_ports" varchar(250) NOT NULL default '',
-	"snmp_community" varchar(64) NOT NULL default 'public',
-	"id_recon_script" INTEGER,
-	"field1" TEXT default NULL,
-	"field2" varchar(250) NOT NULL default '',
-	"field3" varchar(250) NOT NULL default '',
-	"field4" varchar(250) NOT NULL default '',
-	"os_detect" SMALLINT NOT NULL default 1,
-	"resolve_names" SMALLINT NOT NULL default 1,
-	"parent_detection" SMALLINT NOT NULL default 1,
-	"parent_recursion" SMALLINT NOT NULL default 1,
-	"disabled" SMALLINT NOT NULL default 1,
-	"macros" TEXT NOT NULL default ''
-);
-CREATE INDEX "trecon_task_id_recon_server_idx" ON "trecon_task"("id_recon_server");
-
 CREATE TABLE "tserver" (
 	"id_server" SERIAL NOT NULL PRIMARY KEY,
 	"name" varchar(100) NOT NULL default '',
diff --git a/pandora_console/pandoradb.sql b/pandora_console/pandoradb.sql
index 761f277e25..b53120bbbf 100755
--- a/pandora_console/pandoradb.sql
+++ b/pandora_console/pandoradb.sql
@@ -653,6 +653,41 @@ CREATE TABLE IF NOT EXISTS `tmodule_group` (
 	PRIMARY KEY  (`id_mg`)
 ) ENGINE=InnoDB DEFAULT CHARSET=utf8;
 
+-- This table was moved cause the `tmodule_relationship` will add
+-- a foreign key for the trecon_task(id_rt)
+-- ----------------------------------------------------------------------
+-- Table `trecon_task`
+-- ----------------------------------------------------------------------
+CREATE TABLE IF NOT EXISTS `trecon_task` (
+	`id_rt` int(10) unsigned NOT NULL auto_increment,
+	`name` varchar(100) NOT NULL default '',
+	`description` varchar(250) NOT NULL default '',
+	`subnet` text NOT NULL,
+	`id_network_profile` int(10) unsigned NOT NULL default '0',
+	`create_incident` tinyint(3) unsigned NOT NULL default '0',
+	`id_group` int(10) unsigned NOT NULL default '1',
+	`utimestamp` bigint(20) unsigned NOT NULL default '0',
+	`status` tinyint(4) NOT NULL default '0',
+	`interval_sweep` int(10) unsigned NOT NULL default '0',
+	`id_recon_server` int(10) unsigned NOT NULL default '0',
+	`id_os` tinyint(4) NOT NULL default '0',
+	`recon_ports` varchar(250) NOT NULL default '',
+	`snmp_community` varchar(64) NOT NULL default 'public',
+	`id_recon_script` int(10),
+	`field1` text NOT NULL,
+	`field2` varchar(250) NOT NULL default '',
+	`field3` varchar(250) NOT NULL default '',
+	`field4` varchar(250) NOT NULL default '',
+	`os_detect` tinyint(1) unsigned default '0',
+	`resolve_names` tinyint(1) unsigned default '0',
+	`parent_detection` tinyint(1) unsigned default '0',
+	`parent_recursion` tinyint(1) unsigned default '0',
+	`disabled` tinyint(1) unsigned NOT NULL DEFAULT '0',
+	`macros` TEXT,
+	PRIMARY KEY  (`id_rt`),
+	KEY `recon_task_daemon` (`id_recon_server`)
+) ENGINE=InnoDB  DEFAULT CHARSET=utf8;
+
 -- ----------------------------------------------------------------------
 -- Table `tmodule_relationship`
 -- ----------------------------------------------------------------------
@@ -822,39 +857,6 @@ CREATE TABLE IF NOT EXISTS `trecon_script` (
 	PRIMARY KEY  (`id_recon_script`)
 ) ENGINE=InnoDB DEFAULT CHARSET=utf8;
 
--- ----------------------------------------------------------------------
--- Table `trecon_task`
--- ----------------------------------------------------------------------
-CREATE TABLE IF NOT EXISTS `trecon_task` (
-	`id_rt` int(10) unsigned NOT NULL auto_increment,
-	`name` varchar(100) NOT NULL default '',
-	`description` varchar(250) NOT NULL default '',
-	`subnet` text NOT NULL,
-	`id_network_profile` int(10) unsigned NOT NULL default '0',
-	`create_incident` tinyint(3) unsigned NOT NULL default '0',
-	`id_group` int(10) unsigned NOT NULL default '1',
-	`utimestamp` bigint(20) unsigned NOT NULL default '0',
-	`status` tinyint(4) NOT NULL default '0',
-	`interval_sweep` int(10) unsigned NOT NULL default '0',
-	`id_recon_server` int(10) unsigned NOT NULL default '0',
-	`id_os` tinyint(4) NOT NULL default '0',
-	`recon_ports` varchar(250) NOT NULL default '',
-	`snmp_community` varchar(64) NOT NULL default 'public',
-	`id_recon_script` int(10),
-	`field1` text NOT NULL,
-	`field2` varchar(250) NOT NULL default '',
-	`field3` varchar(250) NOT NULL default '',
-	`field4` varchar(250) NOT NULL default '',
-	`os_detect` tinyint(1) unsigned default '0',
-	`resolve_names` tinyint(1) unsigned default '0',
-	`parent_detection` tinyint(1) unsigned default '0',
-	`parent_recursion` tinyint(1) unsigned default '0',
-	`disabled` tinyint(1) unsigned NOT NULL DEFAULT '0',
-	`macros` TEXT,
-	PRIMARY KEY  (`id_rt`),
-	KEY `recon_task_daemon` (`id_recon_server`)
-) ENGINE=InnoDB  DEFAULT CHARSET=utf8;
-
 -- ----------------------------------------------------------------------
 -- Table `tserver`
 -- ----------------------------------------------------------------------