init.sql 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324
  1. SET statement_timeout = 0;
  2. SET lock_timeout = 0;
  3. SET idle_in_transaction_session_timeout = 0;
  4. SET client_encoding = 'UTF8';
  5. SET standard_conforming_strings = on;
  6. SELECT pg_catalog.set_config('search_path', '', false);
  7. SET check_function_bodies = false;
  8. SET xmloption = content;
  9. SET client_min_messages = warning;
  10. SET row_security = off;
  11. CREATE ROLE senslog NOSUPERUSER NOCREATEDB NOCREATEROLE INHERIT;
  12. CREATE ROLE maplog_app NOSUPERUSER NOCREATEDB NOCREATEROLE INHERIT LOGIN PASSWORD 'MAPlog';
  13. GRANT senslog TO maplog_app;
  14. CREATE DATABASE maplog WITH TEMPLATE = template0 ENCODING = 'UTF8' LOCALE_PROVIDER = 'libc' LOCALE = 'en_US.UTF-8';
  15. ALTER DATABASE maplog OWNER TO senslog;
  16. \connect maplog
  17. SET statement_timeout = 0;
  18. SET lock_timeout = 0;
  19. SET idle_in_transaction_session_timeout = 0;
  20. SET client_encoding = 'UTF8';
  21. SET standard_conforming_strings = on;
  22. SELECT pg_catalog.set_config('search_path', '', false);
  23. SET check_function_bodies = false;
  24. SET xmloption = content;
  25. SET client_min_messages = warning;
  26. SET row_security = off;
  27. CREATE SCHEMA maplog;
  28. ALTER SCHEMA maplog OWNER TO senslog;
  29. ALTER SCHEMA public OWNER TO senslog;
  30. CREATE EXTENSION IF NOT EXISTS postgis WITH SCHEMA public;
  31. COMMENT ON EXTENSION postgis IS 'PostGIS geometry and geography spatial types and functions';
  32. SET default_tablespace = '';
  33. CREATE TABLE maplog.campaign (
  34. id INTEGER NOT NULL PRIMARY KEY,
  35. name TEXT NOT NULL,
  36. description TEXT NOT NULL,
  37. from_time TIMESTAMP WITH TIME ZONE NOT NULL,
  38. to_time TIMESTAMP WITH TIME ZONE
  39. -- CONSTRAINT campaign_check_time CHECK (from_time < to_time)
  40. );
  41. ALTER TABLE maplog.campaign OWNER TO senslog;
  42. CREATE SEQUENCE maplog.campaign_id_seq AS INTEGER START WITH 1 INCREMENT BY 1 NO MINVALUE NO MAXVALUE CACHE 1;
  43. ALTER TABLE maplog.campaign_id_seq OWNER TO senslog;
  44. ALTER SEQUENCE maplog.campaign_id_seq OWNED BY maplog.campaign.id;
  45. ALTER TABLE ONLY maplog.campaign ALTER COLUMN id SET DEFAULT nextval('maplog.campaign_id_seq'::regclass);
  46. CREATE TABLE maplog.obs_telemetry (
  47. id BIGINT NOT NULL PRIMARY KEY,
  48. time_stamp TIMESTAMP WITH TIME ZONE NOT NULL,
  49. unit_id BIGINT NOT NULL,
  50. observed_values jsonb NOT NULL,
  51. the_geom public.geometry NOT NULL,
  52. speed INTEGER NOT NULL,
  53. time_received TIMESTAMP WITH TIME ZONE DEFAULT now() NOT NULL,
  54. UNIQUE (unit_id, time_stamp) -- can not have multiple measurements on the same unit at the same time
  55. );
  56. ALTER TABLE maplog.obs_telemetry OWNER TO senslog;
  57. CREATE SEQUENCE maplog.obs_telemetry_id_seq START WITH 1 INCREMENT BY 1 NO MINVALUE NO MAXVALUE CACHE 1;
  58. ALTER TABLE maplog.obs_telemetry_id_seq OWNER TO senslog;
  59. ALTER SEQUENCE maplog.obs_telemetry_id_seq OWNED BY maplog.obs_telemetry.id;
  60. ALTER TABLE ONLY maplog.obs_telemetry ALTER COLUMN id SET DEFAULT nextval('maplog.obs_telemetry_id_seq'::regclass);
  61. CREATE TABLE maplog.entity (
  62. id INTEGER NOT NULL PRIMARY KEY,
  63. identity VARCHAR(30) NOT NULL,
  64. name VARCHAR(100) NOT NULL
  65. );
  66. ALTER TABLE maplog.entity OWNER TO senslog;
  67. CREATE SEQUENCE maplog.entity_id_seq START WITH 1 INCREMENT BY 1 NO MINVALUE NO MAXVALUE CACHE 1;
  68. ALTER TABLE maplog.entity_id_seq OWNER TO senslog;
  69. ALTER SEQUENCE maplog.entity_id_seq OWNED BY maplog.entity.id;
  70. ALTER TABLE ONLY maplog.entity ALTER COLUMN id SET DEFAULT nextval('maplog.entity_id_seq'::regclass);
  71. CREATE TABLE maplog.action (
  72. action_id INTEGER NOT NULL PRIMARY KEY,
  73. name VARCHAR(100) NOT NULL
  74. );
  75. ALTER TABLE maplog.action OWNER TO senslog;
  76. CREATE TABLE maplog.event (
  77. id BIGINT NOT NULL PRIMARY KEY,
  78. entity_id INTEGER NOT NULL,
  79. action_id INTEGER NOT NULL,
  80. unit_id BIGINT NOT NULL,
  81. from_time TIMESTAMP WITH TIME ZONE NOT NULL,
  82. to_time TIMESTAMP WITH TIME ZONE,
  83. CONSTRAINT dta_check_time CHECK (from_time < to_time)
  84. );
  85. ALTER TABLE maplog.event OWNER TO senslog;
  86. CREATE SEQUENCE maplog.event_id_seq START WITH 1 INCREMENT BY 1 NO MINVALUE NO MAXVALUE CACHE 1;
  87. ALTER TABLE maplog.event_id_seq OWNER TO senslog;
  88. ALTER SEQUENCE maplog.event_id_seq OWNED BY maplog.event.id;
  89. ALTER TABLE ONLY maplog.event ALTER COLUMN id SET DEFAULT nextval('maplog.event_id_seq'::regclass);
  90. CREATE TABLE maplog.phenomenon (
  91. id INTEGER NOT NULL PRIMARY KEY,
  92. name TEXT NOT NULL,
  93. uom CHARACTER VARYING(30) NOT NULL,
  94. uom_link TEXT
  95. );
  96. ALTER TABLE maplog.phenomenon OWNER TO senslog;
  97. CREATE SEQUENCE maplog.phenomenon_id_seq AS INTEGER START WITH 1 INCREMENT BY 1 NO MINVALUE NO MAXVALUE CACHE 1;
  98. ALTER TABLE maplog.phenomenon_id_seq OWNER TO senslog;
  99. ALTER SEQUENCE maplog.phenomenon_id_seq OWNED BY maplog.phenomenon.id;
  100. ALTER TABLE ONLY maplog.phenomenon ALTER COLUMN id SET DEFAULT nextval('maplog.phenomenon_id_seq'::regclass);
  101. INSERT INTO maplog.phenomenon(id, name, uom, uom_link) VALUES (0, 'GENERAL', 'UOM', null);
  102. CREATE TABLE maplog.sensor (
  103. sensor_id BIGINT NOT NULL PRIMARY KEY,
  104. name CHARACTER VARYING(100) UNIQUE NOT NULL,
  105. type TEXT,
  106. description TEXT,
  107. io_id INTEGER NOT NULL,
  108. phenomenon_id INTEGER NOT NULL
  109. );
  110. ALTER TABLE maplog.sensor OWNER TO senslog;
  111. CREATE SEQUENCE maplog.sensor_sensor_id_seq START WITH 1 INCREMENT BY 1 NO MINVALUE NO MAXVALUE CACHE 1;
  112. ALTER TABLE maplog.sensor_sensor_id_seq OWNER TO senslog;
  113. ALTER SEQUENCE maplog.sensor_sensor_id_seq OWNED BY maplog.sensor.sensor_id;
  114. ALTER TABLE ONLY maplog.sensor ALTER COLUMN sensor_id SET DEFAULT nextval('maplog.sensor_sensor_id_seq'::regclass);
  115. CREATE TABLE maplog.system_user (
  116. id INTEGER NOT NULL PRIMARY KEY,
  117. identity VARCHAR(50) NOT NULL,
  118. name TEXT NOT NULL
  119. );
  120. ALTER TABLE maplog.system_user OWNER TO senslog;
  121. CREATE SEQUENCE maplog.system_user_id_seq START WITH 1 INCREMENT BY 1 NO MINVALUE NO MAXVALUE CACHE 1;
  122. ALTER TABLE maplog.system_user_id_seq OWNER TO senslog;
  123. ALTER SEQUENCE maplog.system_user_id_seq OWNED BY maplog.sensor.sensor_id;
  124. ALTER TABLE ONLY maplog.system_user ALTER COLUMN id SET DEFAULT nextval('maplog.system_user_id_seq'::regclass);
  125. CREATE TABLE maplog.unit (
  126. unit_id BIGINT NOT NULL PRIMARY KEY,
  127. imei CHARACTER VARYING(20) NOT NULL UNIQUE,
  128. name CHARACTER VARYING(100) NOT NULL,
  129. description TEXT,
  130. is_mobile boolean DEFAULT true NOT NULL,
  131. unit_type_id CHARACTER VARYING(2) DEFAULT 'X'::CHARACTER VARYING NOT NULL
  132. );
  133. ALTER TABLE maplog.unit OWNER TO senslog;
  134. CREATE TABLE maplog.unit_to_campaign (
  135. id INTEGER NOT NULL PRIMARY KEY,
  136. campaign_id INTEGER NOT NULL,
  137. unit_id BIGINT NOT NULL,
  138. from_time TIMESTAMP WITH TIME ZONE NOT NULL,
  139. to_time TIMESTAMP WITH TIME ZONE
  140. );
  141. ALTER TABLE maplog.unit_to_campaign OWNER TO senslog;
  142. CREATE SEQUENCE maplog.unit_to_campaign_id_seq AS INTEGER START WITH 1 INCREMENT BY 1 NO MINVALUE NO MAXVALUE CACHE 1;
  143. ALTER TABLE maplog.unit_to_campaign_id_seq OWNER TO senslog;
  144. ALTER SEQUENCE maplog.unit_to_campaign_id_seq OWNED BY maplog.unit_to_campaign.id;
  145. ALTER TABLE ONLY maplog.unit_to_campaign ALTER COLUMN id SET DEFAULT nextval('maplog.unit_to_campaign_id_seq'::regclass);
  146. CREATE TABLE maplog.unit_to_sensor (
  147. sensor_id BIGINT NOT NULL,
  148. unit_id BIGINT NOT NULL,
  149. PRIMARY KEY (unit_id, sensor_id)
  150. );
  151. ALTER TABLE maplog.unit_to_sensor OWNER TO senslog;
  152. CREATE TABLE maplog.unit_type (
  153. unit_type_id CHARACTER VARYING(2) NOT NULL PRIMARY KEY,
  154. name CHARACTER VARYING(20) NOT NULL,
  155. description TEXT
  156. );
  157. ALTER TABLE maplog.unit_type OWNER TO senslog;
  158. CREATE TABLE maplog.user_to_campaign_config (
  159. user_id INTEGER NOT NULL,
  160. campaign_id INTEGER NOT NULL,
  161. entity_id INTEGER NOT NULL,
  162. config JSONB NOT NULL DEFAULT '{}'::JSONB,
  163. PRIMARY KEY (user_id, campaign_id, entity_id)
  164. );
  165. ALTER TABLE maplog.user_to_campaign_config OWNER TO senslog;
  166. CREATE TYPE alert_status AS ENUM ('CREATED', 'INFORMED', 'IN_PROCESS', 'SOLVED', 'DELETED');
  167. CREATE TABLE maplog.alert (
  168. id BIGINT NOT NULL PRIMARY KEY,
  169. time_stamp TIMESTAMP WITH TIME ZONE NOT NULL,
  170. unit_id BIGINT NOT NULL,
  171. message TEXT NOT NULL,
  172. status alert_status NOT NULL,
  173. time_received TIMESTAMP WITH TIME ZONE DEFAULT now() NOT NULL
  174. );
  175. ALTER TABLE maplog.alert OWNER TO senslog;
  176. CREATE SEQUENCE maplog.alert_id_seq START WITH 1 INCREMENT BY 1 NO MINVALUE NO MAXVALUE CACHE 1;
  177. ALTER TABLE maplog.alert_id_seq OWNER TO senslog;
  178. ALTER SEQUENCE maplog.alert_id_seq OWNED BY maplog.alert.id;
  179. ALTER TABLE ONLY maplog.alert ALTER COLUMN id SET DEFAULT nextval('maplog.alert_id_seq'::regclass);
  180. CREATE INDEX fki_obss_unitid_fk ON maplog.obs_telemetry USING btree (unit_id);
  181. CREATE INDEX fki_sens_phenom_fk ON maplog.sensor USING btree (phenomenon_id);
  182. CREATE INDEX fki_u2c_campid_fk ON maplog.user_to_campaign_config USING btree (campaign_id);
  183. CREATE INDEX fki_u2c_userid_fk ON maplog.user_to_campaign_config USING btree (user_id);
  184. CREATE INDEX fki_u2c_entityid_fk ON maplog.user_to_campaign_config USING btree (entity_id);
  185. CREATE INDEX fki_un2c_campid ON maplog.unit_to_campaign USING btree (campaign_id);
  186. CREATE INDEX fki_un2c_unitid ON maplog.unit_to_campaign USING btree (unit_id);
  187. CREATE INDEX fki_unit_unittype_fk ON maplog.unit USING btree (unit_type_id);
  188. CREATE INDEX fki_uts_sensorid_fk ON maplog.unit_to_sensor USING btree (sensor_id);
  189. CREATE INDEX fki_uts_unitid_fk ON maplog.unit_to_sensor USING btree (unit_id);
  190. CREATE INDEX fki_event_unitid_fk ON maplog.event USING btree (unit_id);
  191. CREATE INDEX fki_dr2ac_entityid_fk ON maplog.event USING btree (entity_id);
  192. CREATE INDEX fki_dr2ac_actionid_fk ON maplog.event USING btree (action_id);
  193. CREATE INDEX fki_dr2ac_unitid_fk ON maplog.event USING btree (unit_id);
  194. CREATE INDEX fki_alert_unitid_fk ON maplog.alert USING btree (unit_id);
  195. ALTER TABLE ONLY maplog.obs_telemetry ADD CONSTRAINT obss_unitid_fk FOREIGN KEY (unit_id) REFERENCES maplog.unit(unit_id) ON UPDATE CASCADE ON DELETE CASCADE;
  196. ALTER TABLE ONLY maplog.sensor ADD CONSTRAINT sens_phenom_fk FOREIGN KEY (phenomenon_id) REFERENCES maplog.phenomenon(id);
  197. ALTER TABLE ONLY maplog.user_to_campaign_config ADD CONSTRAINT u2c_campid FOREIGN KEY (campaign_id) REFERENCES maplog.campaign(id) ON UPDATE CASCADE ON DELETE CASCADE;
  198. ALTER TABLE ONLY maplog.user_to_campaign_config ADD CONSTRAINT u2c_userid_fk FOREIGN KEY (user_id) REFERENCES maplog.system_user(id) ON UPDATE CASCADE ON DELETE CASCADE;
  199. ALTER TABLE ONLY maplog.user_to_campaign_config ADD CONSTRAINT u2c_entityid_fk FOREIGN KEY (entity_id) REFERENCES maplog.entity(id) ON UPDATE CASCADE ON DELETE CASCADE;
  200. ALTER TABLE ONLY maplog.unit_to_campaign ADD CONSTRAINT un2c_campid FOREIGN KEY (campaign_id) REFERENCES maplog.campaign(id) ON UPDATE CASCADE ON DELETE CASCADE;
  201. ALTER TABLE ONLY maplog.unit_to_campaign ADD CONSTRAINT un2c_unitid FOREIGN KEY (unit_id) REFERENCES maplog.unit(unit_id) ON UPDATE CASCADE ON DELETE CASCADE;
  202. ALTER TABLE ONLY maplog.unit ADD CONSTRAINT unit_unittype_fk FOREIGN KEY (unit_type_id) REFERENCES maplog.unit_type(unit_type_id);
  203. ALTER TABLE ONLY maplog.unit_to_sensor ADD CONSTRAINT uts_sensorid_fk FOREIGN KEY (sensor_id) REFERENCES maplog.sensor(sensor_id) ON UPDATE CASCADE ON DELETE CASCADE;
  204. ALTER TABLE ONLY maplog.unit_to_sensor ADD CONSTRAINT uts_unitid_fk FOREIGN KEY (unit_id) REFERENCES maplog.unit(unit_id) ON UPDATE CASCADE ON DELETE CASCADE;
  205. ALTER TABLE ONLY maplog.event ADD CONSTRAINT event_unitid_fk FOREIGN KEY (unit_id) REFERENCES maplog.unit(unit_id) ON UPDATE CASCADE ON DELETE CASCADE;
  206. ALTER TABLE ONLY maplog.event ADD CONSTRAINT event_entityid_fk FOREIGN KEY (entity_id) REFERENCES maplog.entity(id) ON UPDATE CASCADE ON DELETE CASCADE;
  207. ALTER TABLE ONLY maplog.event ADD CONSTRAINT event_actionid_fk FOREIGN KEY (action_id) REFERENCES maplog.action(action_id) ON UPDATE CASCADE ON DELETE CASCADE;
  208. ALTER TABLE ONLY maplog.alert ADD CONSTRAINT alert_unitid_fk FOREIGN KEY (unit_id) REFERENCES maplog.unit(unit_id) ON UPDATE CASCADE ON DELETE CASCADE;
  209. REVOKE USAGE ON SCHEMA public FROM PUBLIC;
  210. GRANT ALL ON SCHEMA public TO PUBLIC;