<?xml version="1.0" encoding="UTF-8"?>

<!-- Differential patch for M/Monit database schema version 19 to 20 -->

<patch>

<!-- sqlite -->

<statements type="sqlite">

<comment>Check database integrity before upgrade</comment>
<statement>
PRAGMA integrity_check;
</statement>
<statement>
PRAGMA foreign_key_check;
</statement>

<comment>Disable foreign keys</comment>
<statement>
PRAGMA foreign_keys=OFF;
</statement>

<comment>Fix rolerowpermission foreign key name</comment>
<statement>
CREATE TABLE rolerowpermission_backup (
  rolerowid INTEGER,
  permission INTEGER
);
</statement>
<statement>
INSERT INTO rolerowpermission_backup (rolerowid, permission) SELECT rolerowid, permission FROM rolerowpermission;
</statement>
<statement>
DROP TABLE rolerowpermission;
</statement>
<statement>
CREATE TABLE rolerowpermission (
  rolerowid INTEGER,
  permission INTEGER,
  CONSTRAINT rolerowpermission_rolerowid_fk FOREIGN KEY(rolerowid) REFERENCES rolerow(id) ON DELETE CASCADE
);
</statement>
<statement>
INSERT INTO rolerowpermission SELECT * FROM rolerowpermission_backup;
</statement>
<statement>
DROP TABLE rolerowpermission_backup;
</statement>
<statement>
CREATE INDEX rolerowpermission_roleid_index ON rolerowpermission(rolerowid);
</statement>

<comment>Trigger synchronization of service layout on next start </comment>
<statement>
UPDATE host SET incarnation=0;
</statement>

<comment>Enable foreign keys</comment>
<statement>
PRAGMA foreign_keys=ON;
</statement>

</statements>


<!-- mysql -->

<statements type="mysql">

<comment>Drop all foreign keys</comment>
<dropforeignkeys />

<comment>---</comment>

<comment>Convert name IDs to BIGINT</comment>
<statement>
ALTER TABLE name MODIFY COLUMN id BIGINT;
</statement>

<comment>Convert host IDs and timestamps to BIGINT</comment>
<statement>
ALTER TABLE host MODIFY COLUMN id BIGINT;
</statement>
<statement>
ALTER TABLE host MODIFY COLUMN created_at BIGINT;
</statement>
<statement>
ALTER TABLE host MODIFY COLUMN updated_at BIGINT;
</statement>
<statement>
ALTER TABLE host MODIFY COLUMN nameid BIGINT;
</statement>
<statement>
ALTER TABLE host MODIFY COLUMN controlfilenameid BIGINT;
</statement>
<statement>
ALTER TABLE host MODIFY COLUMN statusmodified BIGINT;
</statement>
<statement>
ALTER TABLE host MODIFY COLUMN servicemodified BIGINT;
</statement>

<comment>Convert hostgroup IDs to BIGINT</comment>
<statement>
ALTER TABLE hostgroup MODIFY COLUMN id BIGINT;
</statement>
<statement>
ALTER TABLE hostgroup MODIFY COLUMN nameid BIGINT;
</statement>

<comment>Convert groupedhost IDs to BIGINT</comment>
<statement>
ALTER TABLE groupedhost MODIFY COLUMN groupid BIGINT;
</statement>
<statement>
ALTER TABLE groupedhost MODIFY COLUMN hostid BIGINT;
</statement>

<comment>Convert service IDs and timestamps to BIGINT</comment>
<statement>
ALTER TABLE service MODIFY COLUMN id BIGINT;
</statement>
<statement>
ALTER TABLE service MODIFY COLUMN created_at BIGINT;
</statement>
<statement>
ALTER TABLE service MODIFY COLUMN updated_at BIGINT;
</statement>
<statement>
ALTER TABLE service MODIFY COLUMN nameid BIGINT;
</statement>
<statement>
ALTER TABLE service MODIFY COLUMN hostid BIGINT;
</statement>
<statement>
ALTER TABLE service MODIFY COLUMN statusmodified BIGINT;
</statement>

<comment>Convert servicegroup IDs to BIGINT</comment>
<statement>
ALTER TABLE servicegroup MODIFY COLUMN id BIGINT;
</statement>
<statement>
ALTER TABLE servicegroup MODIFY COLUMN nameid BIGINT;
</statement>
<statement>
ALTER TABLE servicegroup MODIFY COLUMN hostid BIGINT;
</statement>

<comment>Convert groupedservices IDs to BIGINT</comment>
<statement>
ALTER TABLE groupedservices MODIFY COLUMN servicegroupid BIGINT;
</statement>
<statement>
ALTER TABLE groupedservices MODIFY COLUMN service_nameid BIGINT;
</statement>

<comment>Convert users IDs to BIGINT</comment>
<statement>
ALTER TABLE users MODIFY COLUMN id BIGINT;
</statement>

<comment>Convert roles IDs to BIGINT</comment>
<statement>
ALTER TABLE roles MODIFY COLUMN id BIGINT;
</statement>

<comment>Convert rolerow IDs to BIGINT</comment>
<statement>
ALTER TABLE rolerow MODIFY COLUMN id BIGINT;
</statement>
<statement>
ALTER TABLE rolerow MODIFY COLUMN roleid BIGINT;
</statement>

<comment>Convert rolerowhost IDs to BIGINT</comment>
<statement>
ALTER TABLE rolerowhost MODIFY COLUMN rolerowid BIGINT;
</statement>
<statement>
ALTER TABLE rolerowhost MODIFY COLUMN hostid BIGINT;
</statement>

<comment>Convert rolerowhostgroup IDs to BIGINT</comment>
<statement>
ALTER TABLE rolerowhostgroup MODIFY COLUMN rolerowid BIGINT;
</statement>
<statement>
ALTER TABLE rolerowhostgroup MODIFY COLUMN hostgroupid BIGINT;
</statement>

<comment>Convert rolerowpermission IDs to BIGINT</comment>
<statement>
ALTER TABLE rolerowpermission MODIFY COLUMN rolerowid BIGINT;
</statement>

<comment>Convert userroles IDs to BIGINT</comment>
<statement>
ALTER TABLE userroles MODIFY COLUMN userid BIGINT;
</statement>
<statement>
ALTER TABLE userroles MODIFY COLUMN roleid BIGINT;
</statement>
<statement>
ALTER TABLE userroles MODIFY COLUMN userid BIGINT;
</statement>
<statement>
ALTER TABLE userroles MODIFY COLUMN roleid BIGINT;
</statement>

<comment>Convert statistics IDs to BIGINT</comment>
<statement>
ALTER TABLE statistics MODIFY COLUMN id BIGINT;
</statement>
<statement>
ALTER TABLE statistics MODIFY COLUMN serviceid BIGINT;
</statement>

<comment>Convert statistics_string IDs and timestamps to BIGINT</comment>
<statement>
ALTER TABLE statistics_string MODIFY COLUMN statisticsid BIGINT;
</statement>
<statement>
ALTER TABLE statistics_string MODIFY COLUMN collectedsec BIGINT;
</statement>

<comment>Convert statistics_int IDs and timestamps to BIGINT</comment>
<statement>
ALTER TABLE statistics_int MODIFY COLUMN statisticsid BIGINT;
</statement>
<statement>
ALTER TABLE statistics_int MODIFY COLUMN collectedsec BIGINT;
</statement>

<comment>Convert statistics_llong IDs and timestamps to BIGINT</comment>
<statement>
ALTER TABLE statistics_llong MODIFY COLUMN statisticsid BIGINT;
</statement>
<statement>
ALTER TABLE statistics_llong MODIFY COLUMN collectedsec BIGINT;
</statement>

<comment>Convert statistics_float IDs and timestamps to BIGINT</comment>
<statement>
ALTER TABLE statistics_float MODIFY COLUMN statisticsid BIGINT;
</statement>
<statement>
ALTER TABLE statistics_float MODIFY COLUMN collectedsec BIGINT;
</statement>

<comment>Convert statistics_double IDs and timestamps to BIGINT</comment>
<statement>
ALTER TABLE statistics_double MODIFY COLUMN statisticsid BIGINT;
</statement>
<statement>
ALTER TABLE statistics_double MODIFY COLUMN collectedsec BIGINT;
</statement>

<comment>Convert statistics_aggregate_1m IDs and timestamps to BIGINT</comment>
<statement>
ALTER TABLE statistics_aggregate_1m MODIFY COLUMN statisticsid BIGINT;
</statement>
<statement>
ALTER TABLE statistics_aggregate_1m MODIFY COLUMN collectedsec BIGINT;
</statement>

<comment>Convert statistics_aggregate_15m IDs and timestamps to BIGINT</comment>
<statement>
ALTER TABLE statistics_aggregate_15m MODIFY COLUMN statisticsid BIGINT;
</statement>
<statement>
ALTER TABLE statistics_aggregate_15m MODIFY COLUMN collectedsec BIGINT;
</statement>

<comment>Convert statistics_aggregate_2h IDs and timestamps to BIGINT</comment>
<statement>
ALTER TABLE statistics_aggregate_2h MODIFY COLUMN statisticsid BIGINT;
</statement>
<statement>
ALTER TABLE statistics_aggregate_2h MODIFY COLUMN collectedsec BIGINT;
</statement>

<comment>Convert statistics_aggregate_12h IDs and timestamps to BIGINT</comment>
<statement>
ALTER TABLE statistics_aggregate_12h MODIFY COLUMN statisticsid BIGINT;
</statement>
<statement>
ALTER TABLE statistics_aggregate_12h MODIFY COLUMN collectedsec BIGINT;
</statement>

<comment>Convert statistics_aggregate_1d IDs and timestamps to BIGINT</comment>
<statement>
ALTER TABLE statistics_aggregate_1d MODIFY COLUMN statisticsid BIGINT;
</statement>
<statement>
ALTER TABLE statistics_aggregate_1d MODIFY COLUMN collectedsec BIGINT;
</statement>

<comment>Convert statistics_aggregate_7d IDs and timestamps to BIGINT</comment>
<statement>
ALTER TABLE statistics_aggregate_7d MODIFY COLUMN statisticsid BIGINT;
</statement>
<statement>
ALTER TABLE statistics_aggregate_7d MODIFY COLUMN collectedsec BIGINT;
</statement>

<comment>Convert statistics_aggregate_14d IDs and timestamps to BIGINT</comment>
<statement>
ALTER TABLE statistics_aggregate_14d MODIFY COLUMN statisticsid BIGINT;
</statement>
<statement>
ALTER TABLE statistics_aggregate_14d MODIFY COLUMN collectedsec BIGINT;
</statement>

<comment>Convert statistics_events IDs to BIGINT</comment>
<statement>
ALTER TABLE statistics_events MODIFY COLUMN hostid BIGINT;
</statement>
<statement>
ALTER TABLE statistics_events MODIFY COLUMN count BIGINT;
</statement>

<comment>Convert event IDs and timestamps to BIGINT</comment>
<statement>
ALTER TABLE event MODIFY COLUMN id BIGINT;
</statement>
<statement>
ALTER TABLE event MODIFY COLUMN hostid BIGINT;
</statement>
<statement>
ALTER TABLE event MODIFY COLUMN serviceid BIGINT;
</statement>
<statement>
ALTER TABLE event MODIFY COLUMN collectedsec BIGINT;
</statement>
<statement>
ALTER TABLE event MODIFY COLUMN collectedusec BIGINT;
</statement>
<statement>
ALTER TABLE event MODIFY COLUMN service_nameid BIGINT;
</statement>

<comment>Convert eventnotice IDs to BIGINT</comment>
<statement>
ALTER TABLE eventnotice MODIFY COLUMN eventid BIGINT;
</statement>

<comment>Convert eventfilter IDs to BIGINT</comment>
<statement>
ALTER TABLE eventfilter MODIFY COLUMN eventid BIGINT;
</statement>
<statement>
ALTER TABLE eventfilter MODIFY COLUMN userid BIGINT;
</statement>

<comment>Convert rule IDs and timestamps to BIGINT</comment>
<statement>
ALTER TABLE rule MODIFY COLUMN id BIGINT;
</statement>
<statement>
ALTER TABLE rule MODIFY COLUMN created_at BIGINT;
</statement>
<statement>
ALTER TABLE rule MODIFY COLUMN updated_at BIGINT;
</statement>

<comment>Convert rulerow IDs to BIGINT</comment>
<statement>
ALTER TABLE rulerow MODIFY COLUMN id BIGINT;
</statement>
<statement>
ALTER TABLE rulerow MODIFY COLUMN ruleid BIGINT;
</statement>

<comment>Convert rulerowhost IDs to BIGINT</comment>
<statement>
ALTER TABLE rulerowhost MODIFY COLUMN rulerowid BIGINT;
</statement>
<statement>
ALTER TABLE rulerowhost MODIFY COLUMN hostid BIGINT;
</statement>

<comment>Convert rulerowhostgroup IDs to BIGINT</comment>
<statement>
ALTER TABLE rulerowhostgroup MODIFY COLUMN rulerowid BIGINT;
</statement>
<statement>
ALTER TABLE rulerowhostgroup MODIFY COLUMN hostgroupid BIGINT;
</statement>

<comment>Convert rulerowservice IDs to BIGINT</comment>
<statement>
ALTER TABLE rulerowservice MODIFY COLUMN rulerowid BIGINT;
</statement>
<statement>
ALTER TABLE rulerowservice MODIFY COLUMN service_nameid BIGINT;
</statement>

<comment>Convert rulerowservicegroup IDs to BIGINT</comment>
<statement>
ALTER TABLE rulerowservicegroup MODIFY COLUMN rulerowid BIGINT;
</statement>
<statement>
ALTER TABLE rulerowservicegroup MODIFY COLUMN servicegroup_nameid BIGINT;
</statement>

<comment>Convert rulerowstate IDs to BIGINT</comment>
<statement>
ALTER TABLE rulerowstate MODIFY COLUMN rulerowid BIGINT;
</statement>

<comment>Convert rulerowevent IDs to BIGINT</comment>
<statement>
ALTER TABLE rulerowevent MODIFY COLUMN rulerowid BIGINT;
</statement>

<comment>Convert actionrow IDs to BIGINT</comment>
<statement>
ALTER TABLE actionrow MODIFY COLUMN id BIGINT;
</statement>
<statement>
ALTER TABLE actionrow MODIFY COLUMN ruleid BIGINT;
</statement>

<comment>Convert actionrowtarget IDs to BIGINT</comment>
<statement>
ALTER TABLE actionrowtarget MODIFY COLUMN actionrowid BIGINT;
</statement>

<comment>Convert actionrowoption IDs to BIGINT</comment>
<statement>
ALTER TABLE actionrowoption MODIFY COLUMN actionrowid BIGINT;
</statement>

<comment>Convert actionqueue IDs and timestamps to BIGINT</comment>
<statement>
ALTER TABLE actionqueue MODIFY COLUMN actionrowid BIGINT;
</statement>
<statement>
ALTER TABLE actionqueue MODIFY COLUMN eventid BIGINT;
</statement>
<statement>
ALTER TABLE actionqueue MODIFY COLUMN created_at BIGINT;
</statement>
<statement>
ALTER TABLE actionqueue MODIFY COLUMN process_at BIGINT;
</statement>

<comment>Convert mailserver IDs to BIGINT</comment>
<statement>
ALTER TABLE mailserver MODIFY COLUMN id BIGINT;
</statement>

<comment>Convert jabberserver IDs to BIGINT</comment>
<statement>
ALTER TABLE jabberserver MODIFY COLUMN id BIGINT;
</statement>

<comment>Convert messageformat IDs to BIGINT</comment>
<statement>
ALTER TABLE messageformat MODIFY COLUMN id BIGINT;
</statement>

<comment>Convert message IDs to BIGINT</comment>
<statement>
ALTER TABLE message MODIFY COLUMN id BIGINT;
</statement>
<statement>
ALTER TABLE message MODIFY COLUMN created BIGINT;
</statement>

<comment>Convert messagerecipients IDs to BIGINT</comment>
<statement>
ALTER TABLE messagerecipients MODIFY COLUMN messageid BIGINT;
</statement>

<comment>---</comment>

<comment>Add foreign keys to host</comment>
<statement>
ALTER TABLE host ADD CONSTRAINT host_nameid_fk FOREIGN KEY (nameid) REFERENCES name (id) ON DELETE CASCADE;
</statement>
<statement>
ALTER TABLE host ADD CONSTRAINT host_controlfilenameid_fk FOREIGN KEY (controlfilenameid) REFERENCES name(id) ON DELETE CASCADE;
</statement>

<comment>Add foreign keys to hostgroup</comment>
<statement>
ALTER TABLE hostgroup ADD CONSTRAINT hostgroup_nameid_fk FOREIGN KEY (nameid) REFERENCES name (id) ON DELETE CASCADE;
</statement>

<comment>Add foreign keys to groupedhost</comment>
<statement>
ALTER TABLE groupedhost ADD CONSTRAINT groupedhost_groupid_fk FOREIGN KEY (groupid) REFERENCES hostgroup (id) ON DELETE CASCADE;
</statement>
<statement>
ALTER TABLE groupedhost ADD CONSTRAINT groupedhost_hostid_fk FOREIGN KEY (hostid) REFERENCES host (id) ON DELETE CASCADE;
</statement>

<comment>Add foreign keys to service</comment>
<statement>
ALTER TABLE service ADD CONSTRAINT service_hostid_fk FOREIGN KEY (hostid) REFERENCES host (id) ON DELETE CASCADE;
</statement>
<statement>
ALTER TABLE service ADD CONSTRAINT service_nameid_fk FOREIGN KEY (nameid) REFERENCES name (id) ON DELETE CASCADE;
</statement>

<comment>Add foreign keys to servicegroup</comment>
<statement>
ALTER TABLE servicegroup ADD CONSTRAINT servicegroup_hostid_fk FOREIGN KEY (hostid) REFERENCES host (id) ON DELETE CASCADE;
</statement>
<statement>
ALTER TABLE servicegroup ADD CONSTRAINT servicegroup_nameid_fk FOREIGN KEY (nameid) REFERENCES name (id) ON DELETE CASCADE;
</statement>

<comment>Add foreign keys to groupedservices</comment>
<statement>
ALTER TABLE groupedservices ADD CONSTRAINT groupedservices_servicegroupid_fk FOREIGN KEY (servicegroupid) REFERENCES servicegroup (id) ON DELETE CASCADE;
</statement>
<statement>
ALTER TABLE groupedservices ADD CONSTRAINT groupedservices_service_nameid_fk FOREIGN KEY (service_nameid) REFERENCES name (id) ON DELETE CASCADE;
</statement>

<comment>Add foreign keys to rolerow</comment>
<statement>
ALTER TABLE rolerow ADD CONSTRAINT rolerow_roleid_fk FOREIGN KEY(roleid) REFERENCES roles(id) ON DELETE CASCADE;
</statement>

<comment>Add foreign keys to rolerowhost</comment>
<statement>
ALTER TABLE rolerowhost ADD CONSTRAINT rolerowhost_rolerowid_fk FOREIGN KEY(rolerowid) REFERENCES rolerow(id) ON DELETE CASCADE;
</statement>
<statement>
ALTER TABLE rolerowhost ADD CONSTRAINT rolerowhost_hostid_fk FOREIGN KEY(hostid) REFERENCES host(id) ON DELETE CASCADE;
</statement>

<comment>Add foreign keys to rolerowhostgroup</comment>
<statement>
ALTER TABLE rolerowhostgroup ADD CONSTRAINT rolerowhostgroup_rolerowid_fk FOREIGN KEY(rolerowid) REFERENCES rolerow(id) ON DELETE CASCADE;
</statement>
<statement>
ALTER TABLE rolerowhostgroup ADD CONSTRAINT rolerowhostgroup_hostgroupid_fk FOREIGN KEY(hostgroupid) REFERENCES hostgroup(id) ON DELETE CASCADE;
</statement>

<comment>Add foreign keys to rolerowpermission</comment>
<statement>
ALTER TABLE rolerowpermission ADD CONSTRAINT rolerowpermission_rolerowid_fk FOREIGN KEY(rolerowid) REFERENCES rolerow(id) ON DELETE CASCADE;
</statement>

<comment>Add foreign keys to userroles</comment>
<statement>
ALTER TABLE userroles ADD CONSTRAINT userroles_roleid_fk FOREIGN KEY(roleid) REFERENCES roles(id) ON DELETE CASCADE;
</statement>
<statement>
ALTER TABLE userroles ADD CONSTRAINT userroles_userid_fk FOREIGN KEY(userid) REFERENCES users(id) ON DELETE CASCADE;
</statement>

<comment>Add foreign keys to statistics</comment>
<statement>
ALTER TABLE statistics ADD CONSTRAINT statistics_serviceid_fk FOREIGN KEY (serviceid) REFERENCES service(id) ON DELETE CASCADE;
</statement>

<comment>Add foreign keys to statistics_string</comment>
<statement>
ALTER TABLE statistics_string ADD CONSTRAINT statistics_string_statisticsid_fk FOREIGN KEY (statisticsid) REFERENCES statistics(id) ON DELETE CASCADE;
</statement>

<comment>Add foreign keys to statistics_int</comment>
<statement>
ALTER TABLE statistics_int ADD CONSTRAINT statistics_int_statisticsid_fk FOREIGN KEY (statisticsid) REFERENCES statistics(id) ON DELETE CASCADE;
</statement>

<comment>Add foreign keys to statistics_llong</comment>
<statement>
ALTER TABLE statistics_llong ADD CONSTRAINT statistics_llong_statisticsid_fk FOREIGN KEY (statisticsid) REFERENCES statistics(id) ON DELETE CASCADE;
</statement>

<comment>Add foreign keys to statistics_float</comment>
<statement>
ALTER TABLE statistics_float ADD CONSTRAINT statistics_float_statisticsid_fk FOREIGN KEY (statisticsid) REFERENCES statistics(id) ON DELETE CASCADE;
</statement>

<comment>Add foreign keys to statistics_double</comment>
<statement>
ALTER TABLE statistics_double ADD CONSTRAINT statistics_double_statisticsid_fk FOREIGN KEY (statisticsid) REFERENCES statistics(id) ON DELETE CASCADE;
</statement>

<comment>Add foreign keys to statistics_aggregate_1m</comment>
<statement>
ALTER TABLE statistics_aggregate_1m ADD CONSTRAINT statistics_aggregate_1m_statisticsid_fk FOREIGN KEY (statisticsid) REFERENCES statistics(id) ON DELETE CASCADE;
</statement>

<comment>Add foreign keys to statistics_aggregate_15m</comment>
<statement>
ALTER TABLE statistics_aggregate_15m ADD CONSTRAINT statistics_aggregate_15m_statisticsid_fk FOREIGN KEY (statisticsid) REFERENCES statistics(id) ON DELETE CASCADE;
</statement>

<comment>Add foreign keys to statistics_aggregate_2h</comment>
<statement>
ALTER TABLE statistics_aggregate_2h ADD CONSTRAINT statistics_aggregate_2h_statisticsid_fk FOREIGN KEY (statisticsid) REFERENCES statistics(id) ON DELETE CASCADE;
</statement>

<comment>Add foreign keys to statistics_aggregate_12h</comment>
<statement>
ALTER TABLE statistics_aggregate_12h ADD CONSTRAINT statistics_aggregate_12h_statisticsid_fk FOREIGN KEY (statisticsid) REFERENCES statistics(id) ON DELETE CASCADE;
</statement>

<comment>Add foreign keys to statistics_aggregate_1d</comment>
<statement>
ALTER TABLE statistics_aggregate_1d ADD CONSTRAINT statistics_aggregate_1d_statisticsid_fk FOREIGN KEY (statisticsid) REFERENCES statistics(id) ON DELETE CASCADE;
</statement>

<comment>Add foreign keys to statistics_aggregate_7d</comment>
<statement>
ALTER TABLE statistics_aggregate_7d ADD CONSTRAINT statistics_aggregate_7d_statisticsid_fk FOREIGN KEY (statisticsid) REFERENCES statistics(id) ON DELETE CASCADE;
</statement>

<comment>Add foreign keys to statistics_aggregate_14d</comment>
<statement>
ALTER TABLE statistics_aggregate_14d ADD CONSTRAINT statistics_aggregate_14d_statisticsid_fk FOREIGN KEY (statisticsid) REFERENCES statistics(id) ON DELETE CASCADE;
</statement>

<comment>Add foreign keys to statistics_events</comment>
<statement>
ALTER TABLE statistics_events ADD CONSTRAINT statistics_events_hostid_fk FOREIGN KEY (hostid) REFERENCES host (id) ON DELETE CASCADE;
</statement>

<comment>Add foreign keys to event</comment>
<statement>
ALTER TABLE event ADD CONSTRAINT event_hostid_fk FOREIGN KEY (hostid) REFERENCES host (id) ON DELETE CASCADE;
</statement>
<statement>
ALTER TABLE event ADD CONSTRAINT event_serviceid_fk FOREIGN KEY (serviceid) REFERENCES service (id) ON DELETE CASCADE;
</statement>
<statement>
ALTER TABLE event ADD CONSTRAINT event_service_nameid_fk FOREIGN KEY (service_nameid) REFERENCES name (id) ON DELETE CASCADE;
</statement>

<comment>Add foreign keys to eventnotice</comment>
<statement>
ALTER TABLE eventnotice ADD CONSTRAINT eventnotice_eventid_fk FOREIGN KEY (eventid) REFERENCES event (id) ON DELETE CASCADE;
</statement>

<comment>Add foreign keys to eventfilter</comment>
<statement>
ALTER TABLE eventfilter ADD CONSTRAINT eventfilter_eventid_fk FOREIGN KEY (eventid) REFERENCES event (id) ON DELETE CASCADE;
</statement>
<statement>
ALTER TABLE eventfilter ADD CONSTRAINT eventfilter_userid_fk FOREIGN KEY (userid) REFERENCES users (id) ON DELETE CASCADE;
</statement>

<comment>Add foreign keys to rulerow</comment>
<statement>
ALTER TABLE rulerow ADD CONSTRAINT rulerow_ruleid_fk FOREIGN KEY (ruleid) REFERENCES rule (id) ON DELETE CASCADE;
</statement>

<comment>Add foreign keys to rulerowhost</comment>
<statement>
ALTER TABLE rulerowhost ADD CONSTRAINT rulerowhost_rulerowid_fk FOREIGN KEY (rulerowid) REFERENCES rulerow (id) ON DELETE CASCADE;
</statement>
<statement>
ALTER TABLE rulerowhost ADD CONSTRAINT rulerowhost_hostid_fk FOREIGN KEY (hostid) REFERENCES host (id) ON DELETE CASCADE;
</statement>

<comment>Add foreign keys to rulerowhostgroup</comment>
<statement>
ALTER TABLE rulerowhostgroup ADD CONSTRAINT rulerowhostgroup_rulerowid_fk FOREIGN KEY (rulerowid) REFERENCES rulerow (id) ON DELETE CASCADE;
</statement>
<statement>
ALTER TABLE rulerowhostgroup ADD CONSTRAINT rulerowhostgroup_hostgroupid_fk FOREIGN KEY (hostgroupid) REFERENCES hostgroup (id) ON DELETE CASCADE;
</statement>

<comment>Add foreign keys to rulerowservice</comment>
<statement>
ALTER TABLE rulerowservice ADD CONSTRAINT rulerowservice_rulerowid_fk FOREIGN KEY (rulerowid) REFERENCES rulerow (id) ON DELETE CASCADE;
</statement>
<statement>
ALTER TABLE rulerowservice ADD CONSTRAINT rulerowservice_service_nameid_fk FOREIGN KEY (service_nameid) REFERENCES name (id) ON DELETE CASCADE;
</statement>

<comment>Add foreign keys to rulerowservicegroup</comment>
<statement>
ALTER TABLE rulerowservicegroup ADD CONSTRAINT rulerowservicegroup_rulerowid_fk FOREIGN KEY (rulerowid) REFERENCES rulerow (id) ON DELETE CASCADE;
</statement>
<statement>
ALTER TABLE rulerowservicegroup ADD CONSTRAINT rulerowservicegroup_servicegroup_nameid_fk FOREIGN KEY (servicegroup_nameid) REFERENCES name (id) ON DELETE CASCADE;
</statement>

<comment>Add foreign keys to rulerowstate</comment>
<statement>
ALTER TABLE rulerowstate ADD CONSTRAINT rulerowstate_rulerowid_fk FOREIGN KEY (rulerowid) REFERENCES rulerow (id) ON DELETE CASCADE;
</statement>

<comment>Add foreign keys to rulerowevent</comment>
<statement>
ALTER TABLE rulerowevent ADD CONSTRAINT rulerowevent_rulerowid_fk FOREIGN KEY (rulerowid) REFERENCES rulerow (id) ON DELETE CASCADE;
</statement>

<comment>Add foreign keys to actionrow</comment>
<statement>
ALTER TABLE actionrow ADD CONSTRAINT actionrow_ruleid_fk FOREIGN KEY (ruleid) REFERENCES rule (id) ON DELETE CASCADE;
</statement>

<comment>Add foreign keys to actionrowtarget</comment>
<statement>
ALTER TABLE actionrowtarget ADD CONSTRAINT actionrowtarget_actionrowid_fk FOREIGN KEY (actionrowid) REFERENCES actionrow(id) ON DELETE CASCADE;
</statement>

<comment>Add foreign keys to actionrowoption</comment>
<statement>
ALTER TABLE actionrowoption ADD CONSTRAINT actionrowoption_actionrowid_fk FOREIGN KEY (actionrowid) REFERENCES actionrow(id) ON DELETE CASCADE;
</statement>

<comment>Add foreign keys to actionqueue</comment>
<statement>
ALTER TABLE actionqueue ADD CONSTRAINT actionqueue_actionrowid_fk FOREIGN KEY (actionrowid) REFERENCES actionrow(id) ON DELETE CASCADE;
</statement>
<statement>
ALTER TABLE actionqueue ADD CONSTRAINT actionqueue_eventid_fk FOREIGN KEY (eventid) REFERENCES event(id) ON DELETE CASCADE;
</statement>

<comment>Add foreign keys to messagerecipients</comment>
<statement>
ALTER TABLE messagerecipients ADD CONSTRAINT messagerecipients_message_fk FOREIGN KEY (messageid) REFERENCES message (id) ON DELETE CASCADE;
</statement>

<comment>Trigger synchronization of service layout on next start </comment>
<statement>
UPDATE host SET incarnation=0;
</statement>

</statements>


<!-- postgresql -->

<statements type="postgresql">

<comment>Drop all foreign keys</comment>
<dropforeignkeys />

<comment>---</comment>

<comment>Convert name IDs to BIGINT</comment>
<statement>
ALTER TABLE name ALTER COLUMN id TYPE BIGINT;
</statement>

<comment>Convert host IDs and timestamps to BIGINT</comment>
<statement>
ALTER TABLE host ALTER COLUMN id TYPE BIGINT;
</statement>
<statement>
ALTER TABLE host ALTER COLUMN created_at TYPE BIGINT;
</statement>
<statement>
ALTER TABLE host ALTER COLUMN updated_at TYPE BIGINT;
</statement>
<statement>
ALTER TABLE host ALTER COLUMN nameid TYPE BIGINT;
</statement>
<statement>
ALTER TABLE host ALTER COLUMN controlfilenameid TYPE BIGINT;
</statement>
<statement>
ALTER TABLE host ALTER COLUMN statusmodified TYPE BIGINT;
</statement>
<statement>
ALTER TABLE host ALTER COLUMN servicemodified TYPE BIGINT;
</statement>

<comment>Convert hostgroup IDs to BIGINT</comment>
<statement>
ALTER TABLE hostgroup ALTER COLUMN id TYPE BIGINT;
</statement>
<statement>
ALTER TABLE hostgroup ALTER COLUMN nameid TYPE BIGINT;
</statement>

<comment>Convert groupedhost IDs to BIGINT</comment>
<statement>
ALTER TABLE groupedhost ALTER COLUMN groupid TYPE BIGINT;
</statement>
<statement>
ALTER TABLE groupedhost ALTER COLUMN hostid TYPE BIGINT;
</statement>

<comment>Convert service IDs and timestamps to BIGINT</comment>
<statement>
ALTER TABLE service ALTER COLUMN id TYPE BIGINT;
</statement>
<statement>
ALTER TABLE service ALTER COLUMN created_at TYPE BIGINT;
</statement>
<statement>
ALTER TABLE service ALTER COLUMN updated_at TYPE BIGINT;
</statement>
<statement>
ALTER TABLE service ALTER COLUMN nameid TYPE BIGINT;
</statement>
<statement>
ALTER TABLE service ALTER COLUMN hostid TYPE BIGINT;
</statement>
<statement>
ALTER TABLE service ALTER COLUMN statusmodified TYPE BIGINT;
</statement>

<comment>Convert servicegroup IDs to BIGINT</comment>
<statement>
ALTER TABLE servicegroup ALTER COLUMN id TYPE BIGINT;
</statement>
<statement>
ALTER TABLE servicegroup ALTER COLUMN nameid TYPE BIGINT;
</statement>
<statement>
ALTER TABLE servicegroup ALTER COLUMN hostid TYPE BIGINT;
</statement>

<comment>Convert groupedservices IDs to BIGINT</comment>
<statement>
ALTER TABLE groupedservices ALTER COLUMN servicegroupid TYPE BIGINT;
</statement>
<statement>
ALTER TABLE groupedservices ALTER COLUMN service_nameid TYPE BIGINT;
</statement>

<comment>Convert users IDs to BIGINT</comment>
<statement>
ALTER TABLE users ALTER COLUMN id TYPE BIGINT;
</statement>

<comment>Convert roles IDs to BIGINT</comment>
<statement>
ALTER TABLE roles ALTER COLUMN id TYPE BIGINT;
</statement>

<comment>Convert rolerow IDs to BIGINT</comment>
<statement>
ALTER TABLE rolerow ALTER COLUMN id TYPE BIGINT;
</statement>
<statement>
ALTER TABLE rolerow ALTER COLUMN roleid TYPE BIGINT;
</statement>

<comment>Convert rolerowhost IDs to BIGINT</comment>
<statement>
ALTER TABLE rolerowhost ALTER COLUMN rolerowid TYPE BIGINT;
</statement>
<statement>
ALTER TABLE rolerowhost ALTER COLUMN hostid TYPE BIGINT;
</statement>

<comment>Convert rolerowhostgroup IDs to BIGINT</comment>
<statement>
ALTER TABLE rolerowhostgroup ALTER COLUMN rolerowid TYPE BIGINT;
</statement>
<statement>
ALTER TABLE rolerowhostgroup ALTER COLUMN hostgroupid TYPE BIGINT;
</statement>

<comment>Convert rolerowpermission IDs to BIGINT</comment>
<statement>
ALTER TABLE rolerowpermission ALTER COLUMN rolerowid TYPE BIGINT;
</statement>

<comment>Convert userroles IDs to BIGINT</comment>
<statement>
ALTER TABLE userroles ALTER COLUMN userid TYPE BIGINT;
</statement>
<statement>
ALTER TABLE userroles ALTER COLUMN roleid TYPE BIGINT;
</statement>
<statement>
ALTER TABLE userroles ALTER COLUMN userid TYPE BIGINT;
</statement>
<statement>
ALTER TABLE userroles ALTER COLUMN roleid TYPE BIGINT;
</statement>

<comment>Convert statistics IDs to BIGINT</comment>
<statement>
ALTER TABLE statistics ALTER COLUMN id TYPE BIGINT;
</statement>
<statement>
ALTER TABLE statistics ALTER COLUMN serviceid TYPE BIGINT;
</statement>

<comment>Convert statistics_string IDs and timestamps to BIGINT</comment>
<statement>
ALTER TABLE statistics_string ALTER COLUMN statisticsid TYPE BIGINT;
</statement>
<statement>
ALTER TABLE statistics_string ALTER COLUMN collectedsec TYPE BIGINT;
</statement>

<comment>Convert statistics_int IDs and timestamps to BIGINT</comment>
<statement>
ALTER TABLE statistics_int ALTER COLUMN statisticsid TYPE BIGINT;
</statement>
<statement>
ALTER TABLE statistics_int ALTER COLUMN collectedsec TYPE BIGINT;
</statement>

<comment>Convert statistics_llong IDs and timestamps to BIGINT</comment>
<statement>
ALTER TABLE statistics_llong ALTER COLUMN statisticsid TYPE BIGINT;
</statement>
<statement>
ALTER TABLE statistics_llong ALTER COLUMN collectedsec TYPE BIGINT;
</statement>

<comment>Convert statistics_float IDs and timestamps to BIGINT</comment>
<statement>
ALTER TABLE statistics_float ALTER COLUMN statisticsid TYPE BIGINT;
</statement>
<statement>
ALTER TABLE statistics_float ALTER COLUMN collectedsec TYPE BIGINT;
</statement>

<comment>Convert statistics_double IDs and timestamps to BIGINT</comment>
<statement>
ALTER TABLE statistics_double ALTER COLUMN statisticsid TYPE BIGINT;
</statement>
<statement>
ALTER TABLE statistics_double ALTER COLUMN collectedsec TYPE BIGINT;
</statement>

<comment>Convert statistics_aggregate_1m IDs and timestamps to BIGINT</comment>
<statement>
ALTER TABLE statistics_aggregate_1m ALTER COLUMN statisticsid TYPE BIGINT;
</statement>
<statement>
ALTER TABLE statistics_aggregate_1m ALTER COLUMN collectedsec TYPE BIGINT;
</statement>

<comment>Convert statistics_aggregate_15m IDs and timestamps to BIGINT</comment>
<statement>
ALTER TABLE statistics_aggregate_15m ALTER COLUMN statisticsid TYPE BIGINT;
</statement>
<statement>
ALTER TABLE statistics_aggregate_15m ALTER COLUMN collectedsec TYPE BIGINT;
</statement>

<comment>Convert statistics_aggregate_2h IDs and timestamps to BIGINT</comment>
<statement>
ALTER TABLE statistics_aggregate_2h ALTER COLUMN statisticsid TYPE BIGINT;
</statement>
<statement>
ALTER TABLE statistics_aggregate_2h ALTER COLUMN collectedsec TYPE BIGINT;
</statement>

<comment>Convert statistics_aggregate_12h IDs and timestamps to BIGINT</comment>
<statement>
ALTER TABLE statistics_aggregate_12h ALTER COLUMN statisticsid TYPE BIGINT;
</statement>
<statement>
ALTER TABLE statistics_aggregate_12h ALTER COLUMN collectedsec TYPE BIGINT;
</statement>

<comment>Convert statistics_aggregate_1d IDs and timestamps to BIGINT</comment>
<statement>
ALTER TABLE statistics_aggregate_1d ALTER COLUMN statisticsid TYPE BIGINT;
</statement>
<statement>
ALTER TABLE statistics_aggregate_1d ALTER COLUMN collectedsec TYPE BIGINT;
</statement>

<comment>Convert statistics_aggregate_7d IDs and timestamps to BIGINT</comment>
<statement>
ALTER TABLE statistics_aggregate_7d ALTER COLUMN statisticsid TYPE BIGINT;
</statement>
<statement>
ALTER TABLE statistics_aggregate_7d ALTER COLUMN collectedsec TYPE BIGINT;
</statement>

<comment>Convert statistics_aggregate_14d IDs and timestamps to BIGINT</comment>
<statement>
ALTER TABLE statistics_aggregate_14d ALTER COLUMN statisticsid TYPE BIGINT;
</statement>
<statement>
ALTER TABLE statistics_aggregate_14d ALTER COLUMN collectedsec TYPE BIGINT;
</statement>

<comment>Convert statistics_events IDs to BIGINT</comment>
<statement>
ALTER TABLE statistics_events ALTER COLUMN hostid TYPE BIGINT;
</statement>
<statement>
ALTER TABLE statistics_events ALTER COLUMN count TYPE BIGINT;
</statement>

<comment>Convert event IDs and timestamps to BIGINT</comment>
<statement>
ALTER TABLE event ALTER COLUMN id TYPE BIGINT;
</statement>
<statement>
ALTER TABLE event ALTER COLUMN hostid TYPE BIGINT;
</statement>
<statement>
ALTER TABLE event ALTER COLUMN serviceid TYPE BIGINT;
</statement>
<statement>
ALTER TABLE event ALTER COLUMN collectedsec TYPE BIGINT;
</statement>
<statement>
ALTER TABLE event ALTER COLUMN collectedusec TYPE BIGINT;
</statement>
<statement>
ALTER TABLE event ALTER COLUMN service_nameid TYPE BIGINT;
</statement>

<comment>Convert eventnotice IDs to BIGINT</comment>
<statement>
ALTER TABLE eventnotice ALTER COLUMN eventid TYPE BIGINT;
</statement>

<comment>Convert eventfilter IDs to BIGINT</comment>
<statement>
ALTER TABLE eventfilter ALTER COLUMN eventid TYPE BIGINT;
</statement>
<statement>
ALTER TABLE eventfilter ALTER COLUMN userid TYPE BIGINT;
</statement>

<comment>Convert rule IDs and timestamps to BIGINT</comment>
<statement>
ALTER TABLE rule ALTER COLUMN id TYPE BIGINT;
</statement>
<statement>
ALTER TABLE rule ALTER COLUMN created_at TYPE BIGINT;
</statement>
<statement>
ALTER TABLE rule ALTER COLUMN updated_at TYPE BIGINT;
</statement>

<comment>Convert rulerow IDs to BIGINT</comment>
<statement>
ALTER TABLE rulerow ALTER COLUMN id TYPE BIGINT;
</statement>
<statement>
ALTER TABLE rulerow ALTER COLUMN ruleid TYPE BIGINT;
</statement>

<comment>Convert rulerowhost IDs to BIGINT</comment>
<statement>
ALTER TABLE rulerowhost ALTER COLUMN rulerowid TYPE BIGINT;
</statement>
<statement>
ALTER TABLE rulerowhost ALTER COLUMN hostid TYPE BIGINT;
</statement>

<comment>Convert rulerowhostgroup IDs to BIGINT</comment>
<statement>
ALTER TABLE rulerowhostgroup ALTER COLUMN rulerowid TYPE BIGINT;
</statement>
<statement>
ALTER TABLE rulerowhostgroup ALTER COLUMN hostgroupid TYPE BIGINT;
</statement>

<comment>Convert rulerowservice IDs to BIGINT</comment>
<statement>
ALTER TABLE rulerowservice ALTER COLUMN rulerowid TYPE BIGINT;
</statement>
<statement>
ALTER TABLE rulerowservice ALTER COLUMN service_nameid TYPE BIGINT;
</statement>

<comment>Convert rulerowservicegroup IDs to BIGINT</comment>
<statement>
ALTER TABLE rulerowservicegroup ALTER COLUMN rulerowid TYPE BIGINT;
</statement>
<statement>
ALTER TABLE rulerowservicegroup ALTER COLUMN servicegroup_nameid TYPE BIGINT;
</statement>

<comment>Convert rulerowstate IDs to BIGINT</comment>
<statement>
ALTER TABLE rulerowstate ALTER COLUMN rulerowid TYPE BIGINT;
</statement>

<comment>Convert rulerowevent IDs to BIGINT</comment>
<statement>
ALTER TABLE rulerowevent ALTER COLUMN rulerowid TYPE BIGINT;
</statement>

<comment>Convert actionrow IDs to BIGINT</comment>
<statement>
ALTER TABLE actionrow ALTER COLUMN id TYPE BIGINT;
</statement>
<statement>
ALTER TABLE actionrow ALTER COLUMN ruleid TYPE BIGINT;
</statement>

<comment>Convert actionrowtarget IDs to BIGINT</comment>
<statement>
ALTER TABLE actionrowtarget ALTER COLUMN actionrowid TYPE BIGINT;
</statement>

<comment>Convert actionrowoption IDs to BIGINT</comment>
<statement>
ALTER TABLE actionrowoption ALTER COLUMN actionrowid TYPE BIGINT;
</statement>

<comment>Convert actionqueue IDs and timestamps to BIGINT</comment>
<statement>
ALTER TABLE actionqueue ALTER COLUMN actionrowid TYPE BIGINT;
</statement>
<statement>
ALTER TABLE actionqueue ALTER COLUMN eventid TYPE BIGINT;
</statement>
<statement>
ALTER TABLE actionqueue ALTER COLUMN created_at TYPE BIGINT;
</statement>
<statement>
ALTER TABLE actionqueue ALTER COLUMN process_at TYPE BIGINT;
</statement>

<comment>Convert mailserver IDs to BIGINT</comment>
<statement>
ALTER TABLE mailserver ALTER COLUMN id TYPE BIGINT;
</statement>

<comment>Convert jabberserver IDs to BIGINT</comment>
<statement>
ALTER TABLE jabberserver ALTER COLUMN id TYPE BIGINT;
</statement>

<comment>Convert messageformat IDs to BIGINT</comment>
<statement>
ALTER TABLE messageformat ALTER COLUMN id TYPE BIGINT;
</statement>

<comment>Convert message IDs to BIGINT</comment>
<statement>
ALTER TABLE message ALTER COLUMN id TYPE BIGINT;
</statement>
<statement>
ALTER TABLE message ALTER COLUMN created TYPE BIGINT;
</statement>

<comment>Convert messagerecipients IDs to BIGINT</comment>
<statement>
ALTER TABLE messagerecipients ALTER COLUMN messageid TYPE BIGINT;
</statement>

<comment>---</comment>

<comment>Add foreign keys to host</comment>
<statement>
ALTER TABLE host ADD CONSTRAINT host_nameid_fk FOREIGN KEY (nameid) REFERENCES name (id) ON DELETE CASCADE;
</statement>
<statement>
ALTER TABLE host ADD CONSTRAINT host_controlfilenameid_fk FOREIGN KEY (controlfilenameid) REFERENCES name(id) ON DELETE CASCADE;
</statement>

<comment>Add foreign keys to hostgroup</comment>
<statement>
ALTER TABLE hostgroup ADD CONSTRAINT hostgroup_nameid_fk FOREIGN KEY (nameid) REFERENCES name (id) ON DELETE CASCADE;
</statement>

<comment>Add foreign keys to groupedhost</comment>
<statement>
ALTER TABLE groupedhost ADD CONSTRAINT groupedhost_groupid_fk FOREIGN KEY (groupid) REFERENCES hostgroup (id) ON DELETE CASCADE;
</statement>
<statement>
ALTER TABLE groupedhost ADD CONSTRAINT groupedhost_hostid_fk FOREIGN KEY (hostid) REFERENCES host (id) ON DELETE CASCADE;
</statement>

<comment>Add foreign keys to service</comment>
<statement>
ALTER TABLE service ADD CONSTRAINT service_hostid_fk FOREIGN KEY (hostid) REFERENCES host (id) ON DELETE CASCADE;
</statement>
<statement>
ALTER TABLE service ADD CONSTRAINT service_nameid_fk FOREIGN KEY (nameid) REFERENCES name (id) ON DELETE CASCADE;
</statement>

<comment>Add foreign keys to servicegroup</comment>
<statement>
ALTER TABLE servicegroup ADD CONSTRAINT servicegroup_hostid_fk FOREIGN KEY (hostid) REFERENCES host (id) ON DELETE CASCADE;
</statement>
<statement>
ALTER TABLE servicegroup ADD CONSTRAINT servicegroup_nameid_fk FOREIGN KEY (nameid) REFERENCES name (id) ON DELETE CASCADE;
</statement>

<comment>Add foreign keys to groupedservices</comment>
<statement>
ALTER TABLE groupedservices ADD CONSTRAINT groupedservices_servicegroupid_fk FOREIGN KEY (servicegroupid) REFERENCES servicegroup (id) ON DELETE CASCADE;
</statement>
<statement>
ALTER TABLE groupedservices ADD CONSTRAINT groupedservices_service_nameid_fk FOREIGN KEY (service_nameid) REFERENCES name (id) ON DELETE CASCADE;
</statement>

<comment>Add foreign keys to rolerow</comment>
<statement>
ALTER TABLE rolerow ADD CONSTRAINT rolerow_roleid_fk FOREIGN KEY(roleid) REFERENCES roles(id) ON DELETE CASCADE;
</statement>

<comment>Add foreign keys to rolerowhost</comment>
<statement>
ALTER TABLE rolerowhost ADD CONSTRAINT rolerowhost_rolerowid_fk FOREIGN KEY(rolerowid) REFERENCES rolerow(id) ON DELETE CASCADE;
</statement>
<statement>
ALTER TABLE rolerowhost ADD CONSTRAINT rolerowhost_hostid_fk FOREIGN KEY(hostid) REFERENCES host(id) ON DELETE CASCADE;
</statement>

<comment>Add foreign keys to rolerowhostgroup</comment>
<statement>
ALTER TABLE rolerowhostgroup ADD CONSTRAINT rolerowhostgroup_rolerowid_fk FOREIGN KEY(rolerowid) REFERENCES rolerow(id) ON DELETE CASCADE;
</statement>
<statement>
ALTER TABLE rolerowhostgroup ADD CONSTRAINT rolerowhostgroup_hostgroupid_fk FOREIGN KEY(hostgroupid) REFERENCES hostgroup(id) ON DELETE CASCADE;
</statement>

<comment>Add foreign keys to rolerowpermission</comment>
<statement>
ALTER TABLE rolerowpermission ADD CONSTRAINT rolerowpermission_rolerowid_fk FOREIGN KEY(rolerowid) REFERENCES rolerow(id) ON DELETE CASCADE;
</statement>

<comment>Add foreign keys to userroles</comment>
<statement>
ALTER TABLE userroles ADD CONSTRAINT userroles_roleid_fk FOREIGN KEY(roleid) REFERENCES roles(id) ON DELETE CASCADE;
</statement>
<statement>
ALTER TABLE userroles ADD CONSTRAINT userroles_userid_fk FOREIGN KEY(userid) REFERENCES users(id) ON DELETE CASCADE;
</statement>

<comment>Add foreign keys to statistics</comment>
<statement>
ALTER TABLE statistics ADD CONSTRAINT statistics_serviceid_fk FOREIGN KEY (serviceid) REFERENCES service(id) ON DELETE CASCADE;
</statement>

<comment>Add foreign keys to statistics_string</comment>
<statement>
ALTER TABLE statistics_string ADD CONSTRAINT statistics_string_statisticsid_fk FOREIGN KEY (statisticsid) REFERENCES statistics(id) ON DELETE CASCADE;
</statement>

<comment>Add foreign keys to statistics_int</comment>
<statement>
ALTER TABLE statistics_int ADD CONSTRAINT statistics_int_statisticsid_fk FOREIGN KEY (statisticsid) REFERENCES statistics(id) ON DELETE CASCADE;
</statement>

<comment>Add foreign keys to statistics_llong</comment>
<statement>
ALTER TABLE statistics_llong ADD CONSTRAINT statistics_llong_statisticsid_fk FOREIGN KEY (statisticsid) REFERENCES statistics(id) ON DELETE CASCADE;
</statement>

<comment>Add foreign keys to statistics_float</comment>
<statement>
ALTER TABLE statistics_float ADD CONSTRAINT statistics_float_statisticsid_fk FOREIGN KEY (statisticsid) REFERENCES statistics(id) ON DELETE CASCADE;
</statement>

<comment>Add foreign keys to statistics_double</comment>
<statement>
ALTER TABLE statistics_double ADD CONSTRAINT statistics_double_statisticsid_fk FOREIGN KEY (statisticsid) REFERENCES statistics(id) ON DELETE CASCADE;
</statement>

<comment>Add foreign keys to statistics_aggregate_1m</comment>
<statement>
ALTER TABLE statistics_aggregate_1m ADD CONSTRAINT statistics_aggregate_1m_statisticsid_fk FOREIGN KEY (statisticsid) REFERENCES statistics(id) ON DELETE CASCADE;
</statement>

<comment>Add foreign keys to statistics_aggregate_15m</comment>
<statement>
ALTER TABLE statistics_aggregate_15m ADD CONSTRAINT statistics_aggregate_15m_statisticsid_fk FOREIGN KEY (statisticsid) REFERENCES statistics(id) ON DELETE CASCADE;
</statement>

<comment>Add foreign keys to statistics_aggregate_2h</comment>
<statement>
ALTER TABLE statistics_aggregate_2h ADD CONSTRAINT statistics_aggregate_2h_statisticsid_fk FOREIGN KEY (statisticsid) REFERENCES statistics(id) ON DELETE CASCADE;
</statement>

<comment>Add foreign keys to statistics_aggregate_12h</comment>
<statement>
ALTER TABLE statistics_aggregate_12h ADD CONSTRAINT statistics_aggregate_12h_statisticsid_fk FOREIGN KEY (statisticsid) REFERENCES statistics(id) ON DELETE CASCADE;
</statement>

<comment>Add foreign keys to statistics_aggregate_1d</comment>
<statement>
ALTER TABLE statistics_aggregate_1d ADD CONSTRAINT statistics_aggregate_1d_statisticsid_fk FOREIGN KEY (statisticsid) REFERENCES statistics(id) ON DELETE CASCADE;
</statement>

<comment>Add foreign keys to statistics_aggregate_7d</comment>
<statement>
ALTER TABLE statistics_aggregate_7d ADD CONSTRAINT statistics_aggregate_7d_statisticsid_fk FOREIGN KEY (statisticsid) REFERENCES statistics(id) ON DELETE CASCADE;
</statement>

<comment>Add foreign keys to statistics_aggregate_14d</comment>
<statement>
ALTER TABLE statistics_aggregate_14d ADD CONSTRAINT statistics_aggregate_14d_statisticsid_fk FOREIGN KEY (statisticsid) REFERENCES statistics(id) ON DELETE CASCADE;
</statement>

<comment>Add foreign keys to statistics_events</comment>
<statement>
ALTER TABLE statistics_events ADD CONSTRAINT statistics_events_hostid_fk FOREIGN KEY (hostid) REFERENCES host (id) ON DELETE CASCADE;
</statement>

<comment>Add foreign keys to event</comment>
<statement>
ALTER TABLE event ADD CONSTRAINT event_hostid_fk FOREIGN KEY (hostid) REFERENCES host (id) ON DELETE CASCADE;
</statement>
<statement>
ALTER TABLE event ADD CONSTRAINT event_serviceid_fk FOREIGN KEY (serviceid) REFERENCES service (id) ON DELETE CASCADE;
</statement>
<statement>
ALTER TABLE event ADD CONSTRAINT event_service_nameid_fk FOREIGN KEY (service_nameid) REFERENCES name (id) ON DELETE CASCADE;
</statement>

<comment>Add foreign keys to eventnotice</comment>
<statement>
ALTER TABLE eventnotice ADD CONSTRAINT eventnotice_eventid_fk FOREIGN KEY (eventid) REFERENCES event (id) ON DELETE CASCADE;
</statement>

<comment>Add foreign keys to eventfilter</comment>
<statement>
ALTER TABLE eventfilter ADD CONSTRAINT eventfilter_eventid_fk FOREIGN KEY (eventid) REFERENCES event (id) ON DELETE CASCADE;
</statement>
<statement>
ALTER TABLE eventfilter ADD CONSTRAINT eventfilter_userid_fk FOREIGN KEY (userid) REFERENCES users (id) ON DELETE CASCADE;
</statement>

<comment>Add foreign keys to rulerow</comment>
<statement>
ALTER TABLE rulerow ADD CONSTRAINT rulerow_ruleid_fk FOREIGN KEY (ruleid) REFERENCES rule (id) ON DELETE CASCADE;
</statement>

<comment>Add foreign keys to rulerowhost</comment>
<statement>
ALTER TABLE rulerowhost ADD CONSTRAINT rulerowhost_rulerowid_fk FOREIGN KEY (rulerowid) REFERENCES rulerow (id) ON DELETE CASCADE;
</statement>
<statement>
ALTER TABLE rulerowhost ADD CONSTRAINT rulerowhost_hostid_fk FOREIGN KEY (hostid) REFERENCES host (id) ON DELETE CASCADE
</statement>

<comment>Add foreign keys to rulerowhostgroup</comment>
<statement>
ALTER TABLE rulerowhostgroup ADD CONSTRAINT rulerowhostgroup_rulerowid_fk FOREIGN KEY (rulerowid) REFERENCES rulerow (id) ON DELETE CASCADE;
</statement>
<statement>
ALTER TABLE rulerowhostgroup ADD CONSTRAINT rulerowhostgroup_hostgroupid_fk FOREIGN KEY (hostgroupid) REFERENCES hostgroup (id) ON DELETE CASCADE;
</statement>

<comment>Add foreign keys to rulerowservice</comment>
<statement>
ALTER TABLE rulerowservice ADD CONSTRAINT rulerowservice_rulerowid_fk FOREIGN KEY (rulerowid) REFERENCES rulerow (id) ON DELETE CASCADE;
</statement>
<statement>
ALTER TABLE rulerowservice ADD CONSTRAINT rulerowservice_service_nameid_fk FOREIGN KEY (service_nameid) REFERENCES name (id) ON DELETE CASCADE;
</statement>

<comment>Add foreign keys to rulerowservicegroup</comment>
<statement>
ALTER TABLE rulerowservicegroup ADD CONSTRAINT rulerowservicegroup_rulerowid_fk FOREIGN KEY (rulerowid) REFERENCES rulerow (id) ON DELETE CASCADE;
</statement>
<statement>
ALTER TABLE rulerowservicegroup ADD CONSTRAINT rulerowservicegroup_servicegroup_nameid_fk FOREIGN KEY (servicegroup_nameid) REFERENCES name (id) ON DELETE CASCADE;
</statement>

<comment>Add foreign keys to rulerowstate</comment>
<statement>
ALTER TABLE rulerowstate ADD CONSTRAINT rulerowstate_rulerowid_fk FOREIGN KEY (rulerowid) REFERENCES rulerow (id) ON DELETE CASCADE;
</statement>

<comment>Add foreign keys to rulerowevent</comment>
<statement>
ALTER TABLE rulerowevent ADD CONSTRAINT rulerowevent_rulerowid_fk FOREIGN KEY (rulerowid) REFERENCES rulerow (id) ON DELETE CASCADE;
</statement>

<comment>Add foreign keys to actionrow</comment>
<statement>
ALTER TABLE actionrow ADD CONSTRAINT actionrow_ruleid_fk FOREIGN KEY (ruleid) REFERENCES rule (id) ON DELETE CASCADE;
</statement>

<comment>Add foreign keys to actionrowtarget</comment>
<statement>
ALTER TABLE actionrowtarget ADD CONSTRAINT actionrowtarget_actionrowid_fk FOREIGN KEY (actionrowid) REFERENCES actionrow(id) ON DELETE CASCADE;
</statement>

<comment>Add foreign keys to actionrowoption</comment>
<statement>
ALTER TABLE actionrowoption ADD CONSTRAINT actionrowoption_actionrowid_fk FOREIGN KEY (actionrowid) REFERENCES actionrow(id) ON DELETE CASCADE;
</statement>

<comment>Add foreign keys to actionqueue</comment>
<statement>
ALTER TABLE actionqueue ADD CONSTRAINT actionqueue_actionrowid_fk FOREIGN KEY (actionrowid) REFERENCES actionrow(id) ON DELETE CASCADE;
</statement>
<statement>
ALTER TABLE actionqueue ADD CONSTRAINT actionqueue_eventid_fk FOREIGN KEY (eventid) REFERENCES event(id) ON DELETE CASCADE;
</statement>

<comment>Add foreign keys to messagerecipients</comment>
<statement>
ALTER TABLE messagerecipients ADD CONSTRAINT messagerecipients_message_fk FOREIGN KEY (messageid) REFERENCES message (id) ON DELETE CASCADE;
</statement>

<comment>Trigger synchronization of service layout on next start </comment>
<statement>
UPDATE host SET incarnation=0;
</statement>

</statements>

</patch>

