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

<!-- Differential patch for M/Monit database schema version 12 to 13 -->

<patch>

<!-- sqlite -->

<statements type="sqlite">

<statement>
PRAGMA foreign_keys=OFF;
</statement>
<statement>
CREATE TEMPORARY TABLE host_backup (
  id INTEGER PRIMARY KEY,
  created_at INTEGER,
  updated_at INTEGER,
  incarnation INTEGER,
  status CHAR(1) NOT NULL,
  nameid INTEGER NOT NULL,
  keepname INTEGER DEFAULT 0,
  monitid VARCHAR(255) UNIQUE NOT NULL,
  ipaddrin VARCHAR(255) NOT NULL,
  ipaddrout VARCHAR(255),
  portin INTEGER,
  portout INTEGER DEFAULT -1,
  uname VARCHAR(255),
  password VARCHAR(255),
  sslin INTEGER,
  sslout INTEGER DEFAULT -1,
  description TEXT,
  poll INTEGER DEFAULT 0,
  startdelay INTEGER DEFAULT 0,
  controlfilenameid INTEGER NOT NULL,
  statusmodified INTEGER,
  servicemodified INTEGER DEFAULT 0,
  serviceskew INTEGER DEFAULT -1,
  serviceup INTEGER DEFAULT 0,
  servicedown INTEGER DEFAULT 0,
  serviceunmonitorauto INTEGER,
  serviceunmonitormanual INTEGER,
  version VARCHAR(20),
  platformname VARCHAR(255),
  platformrelease VARCHAR(255),
  platformversion VARCHAR(255),
  platformmachine VARCHAR(255),
  platformcpu INTEGER DEFAULT 0,
  platformmemory INTEGER DEFAULT 0,
  platformswap INTEGER DEFAULT 0,
  statusheartbeat INTEGER DEFAULT 1
);
</statement>
<statement>
INSERT INTO host_backup (id, created_at, updated_at, incarnation, status, nameid, keepname, monitid, ipaddrin, ipaddrout, portin, portout, uname, password, sslin, sslout, description, poll, startdelay, controlfilenameid, statusmodified, servicemodified, serviceskew, serviceup, servicedown, serviceunmonitorauto, serviceunmonitormanual, version, platformname, platformrelease, platformversion, platformmachine, platformcpu, platformmemory, platformswap, statusheartbeat) SELECT id, created_at, updated_at, incarnation, status, nameid, keepname, monitid, ipaddrin, ipaddrout, portin, portout, uname, password, sslin, sslout, description, poll, startdelay, controlfilenameid, statusmodified, servicemodified, serviceskew, serviceup, servicedown, serviceunmonitorauto, serviceunmonitormanual, version, platformname, platformrelease, platformversion, platformmachine, platformcpu, platformmemory, platformswap, statusheartbeat FROM host;
</statement>
<statement>
DROP TABLE host;
</statement>
<statement>
CREATE TABLE host (
  id INTEGER PRIMARY KEY,
  created_at INTEGER,
  updated_at INTEGER,
  incarnation INTEGER,
  status CHAR(1) NOT NULL,
  nameid INTEGER NOT NULL,
  keepname INTEGER DEFAULT 0,
  monitid VARCHAR(255) UNIQUE NOT NULL,
  ipaddrin VARCHAR(255) NOT NULL,
  ipaddrout VARCHAR(255),
  portin INTEGER,
  portout INTEGER DEFAULT -1,
  uname VARCHAR(255),
  password VARCHAR(255),
  sslin INTEGER,
  sslout INTEGER DEFAULT -1,
  description TEXT,
  poll INTEGER DEFAULT 0,
  startdelay INTEGER DEFAULT 0,
  controlfilenameid INTEGER NOT NULL,
  statusmodified INTEGER,
  servicemodified INTEGER DEFAULT 0,
  serviceskew INTEGER DEFAULT -1,
  serviceup INTEGER DEFAULT 0,
  servicedown INTEGER DEFAULT 0,
  serviceunmonitorauto INTEGER,
  serviceunmonitormanual INTEGER,
  version VARCHAR(20),
  platformname VARCHAR(255),
  platformrelease VARCHAR(255),
  platformversion VARCHAR(255),
  platformmachine VARCHAR(255),
  platformcpu INTEGER DEFAULT 0,
  platformmemory INTEGER DEFAULT 0,
  platformswap INTEGER DEFAULT 0,
  statusheartbeat INTEGER DEFAULT 1,
  CONSTRAINT host_nameid_fk FOREIGN KEY(nameid) REFERENCES name(id) ON DELETE CASCADE,
  CONSTRAINT host_controlfilenameid_fk FOREIGN KEY(controlfilenameid) REFERENCES name(id) ON DELETE CASCADE
);
</statement>
<statement>
INSERT INTO host SELECT * FROM host_backup;
</statement>
<statement>
DROP TABLE host_backup;
</statement>
<statement>
CREATE TEMPORARY TABLE service_backup (
  id INTEGER PRIMARY KEY,
  created_at INTEGER,
  updated_at INTEGER,
  nameid INTEGER NOT NULL,
  hostid INTEGER NOT NULL,
  type INTEGER NOT NULL,
  status INTEGER NOT NULL,
  statushint INTEGER NOT NULL,
  monitoringstate INTEGER NOT NULL,
  monitoringmode INTEGER NOT NULL,
  onreboot INTEGER NOT NULL,
  statusmodified INTEGER DEFAULT 0,
  everytype INTEGER DEFAULT 0,
  everycyclenumber INTEGER DEFAULT 0,
  everycyclecounter INTEGER DEFAULT 0,
  everycron VARCHAR(255)
);
</statement>
<statement>
INSERT INTO service_backup (id, created_at, updated_at, nameid, hostid, type, status, statushint, monitoringstate, monitoringmode, onreboot, statusmodified, everytype, everycyclenumber, everycyclecounter, everycron) SELECT id, created_at, updated_at, nameid, hostid, type, status, statushint, monitoringstate, monitoringmode, 0, statusmodified, everytype, everycyclenumber, everycyclecounter, everycron FROM service;
</statement>
<statement>
DROP TABLE service;
</statement>
<statement>
CREATE TABLE service (
  id INTEGER PRIMARY KEY,
  created_at INTEGER,
  updated_at INTEGER,
  nameid INTEGER NOT NULL,
  hostid INTEGER NOT NULL,
  type INTEGER NOT NULL,
  status INTEGER NOT NULL,
  statushint INTEGER NOT NULL,
  monitoringstate INTEGER NOT NULL,
  monitoringmode INTEGER NOT NULL,
  onreboot INTEGER NOT NULL,
  statusmodified INTEGER DEFAULT 0,
  everytype INTEGER DEFAULT 0,
  everycyclenumber INTEGER DEFAULT 0,
  everycyclecounter INTEGER DEFAULT 0,
  everycron VARCHAR(255),
  CONSTRAINT service_hostid_fk FOREIGN KEY(hostid) REFERENCES host(id) ON DELETE CASCADE,
  CONSTRAINT service_nameid_fk FOREIGN KEY(nameid) REFERENCES name(id) ON DELETE CASCADE
);
</statement>
<statement>
INSERT INTO service SELECT * FROM service_backup;
</statement>
<statement>
DROP TABLE service_backup;
</statement>
<statement>
UPDATE service SET onreboot=2 WHERE monitoringmode=2;
</statement>
<statement>
CREATE INDEX service_hostid_index ON service(hostid);
</statement>
<statement>
UPDATE statistics SET datatype=64 WHERE type IN (15, 16, 17);
</statement>
<statement>
UPDATE messageformat SET contenttype=1, sender=(SELECT sender FROM messageformat mf2 WHERE id=1) WHERE id=3;
</statement>
<statement>
VACUUM;
</statement>

</statements>


<!-- mysql -->

<statements type="mysql">

<statement>
ALTER TABLE host DROP COLUMN eventscount;
</statement>
<statement>
ALTER TABLE service DROP COLUMN eventscount;
</statement>
<statement>
ALTER TABLE service ADD COLUMN onreboot INTEGER NOT NULL AFTER monitoringmode;
</statement>
<statement>
UPDATE service SET onreboot=0;
</statement>
<statement>
UPDATE service SET onreboot=2 WHERE monitoringmode=2;
</statement>
<statement>
UPDATE statistics SET datatype=64 WHERE type IN (15, 16, 17);
</statement>
<statement>
UPDATE messageformat mf1, messageformat mf2 SET mf1.contenttype=1, mf1.sender=mf2.sender WHERE mf1.id=3 and mf2.id=1;
</statement>

</statements>


<!-- postgresql -->

<statements type="postgresql">

<statement>
ALTER TABLE host DROP COLUMN eventscount;
</statement>
<statement>
ALTER TABLE service DROP COLUMN eventscount;
</statement>
<statement>
ALTER TABLE service ADD COLUMN onreboot INTEGER;
</statement>
<statement>
UPDATE service SET onreboot=0;
</statement>
<statement>
UPDATE service SET onreboot=2 WHERE monitoringmode=2;
</statement>
<statement>
ALTER TABLE service ALTER COLUMN onreboot SET NOT NULL;
</statement>
<statement>
UPDATE statistics SET datatype=64 WHERE type IN (15, 16, 17);
</statement>
<statement>
UPDATE messageformat SET contenttype=1, sender=(SELECT sender FROM messageformat mf2 WHERE id=1) WHERE id=3;
</statement>

</statements>

</patch>

