init.sql 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393
  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. io_multiplier DOUBLE PRECISION NOT NULL DEFAULT 1.0,
  109. phenomenon_id INTEGER NOT NULL
  110. );
  111. ALTER TABLE maplog.sensor OWNER TO senslog;
  112. CREATE SEQUENCE maplog.sensor_sensor_id_seq START WITH 1 INCREMENT BY 1 NO MINVALUE NO MAXVALUE CACHE 1;
  113. ALTER TABLE maplog.sensor_sensor_id_seq OWNER TO senslog;
  114. ALTER SEQUENCE maplog.sensor_sensor_id_seq OWNED BY maplog.sensor.sensor_id;
  115. ALTER TABLE ONLY maplog.sensor ALTER COLUMN sensor_id SET DEFAULT nextval('maplog.sensor_sensor_id_seq'::regclass);
  116. CREATE TABLE maplog.system_user (
  117. id INTEGER NOT NULL PRIMARY KEY,
  118. identity VARCHAR(50) NOT NULL,
  119. name TEXT NOT NULL
  120. );
  121. ALTER TABLE maplog.system_user OWNER TO senslog;
  122. CREATE SEQUENCE maplog.system_user_id_seq START WITH 1 INCREMENT BY 1 NO MINVALUE NO MAXVALUE CACHE 1;
  123. ALTER TABLE maplog.system_user_id_seq OWNER TO senslog;
  124. ALTER SEQUENCE maplog.system_user_id_seq OWNED BY maplog.sensor.sensor_id;
  125. ALTER TABLE ONLY maplog.system_user ALTER COLUMN id SET DEFAULT nextval('maplog.system_user_id_seq'::regclass);
  126. CREATE TABLE maplog.unit (
  127. unit_id BIGINT NOT NULL PRIMARY KEY,
  128. imei CHARACTER VARYING(20) NOT NULL UNIQUE,
  129. name CHARACTER VARYING(100) NOT NULL,
  130. description TEXT,
  131. is_mobile boolean DEFAULT true NOT NULL,
  132. unit_type_id CHARACTER VARYING(2) DEFAULT 'X'::CHARACTER VARYING NOT NULL
  133. );
  134. ALTER TABLE maplog.unit OWNER TO senslog;
  135. CREATE TABLE maplog.unit_to_campaign (
  136. id INTEGER NOT NULL PRIMARY KEY,
  137. campaign_id INTEGER NOT NULL,
  138. unit_id BIGINT NOT NULL,
  139. from_time TIMESTAMP WITH TIME ZONE NOT NULL,
  140. to_time TIMESTAMP WITH TIME ZONE
  141. );
  142. ALTER TABLE maplog.unit_to_campaign OWNER TO senslog;
  143. CREATE SEQUENCE maplog.unit_to_campaign_id_seq AS INTEGER START WITH 1 INCREMENT BY 1 NO MINVALUE NO MAXVALUE CACHE 1;
  144. ALTER TABLE maplog.unit_to_campaign_id_seq OWNER TO senslog;
  145. ALTER SEQUENCE maplog.unit_to_campaign_id_seq OWNED BY maplog.unit_to_campaign.id;
  146. ALTER TABLE ONLY maplog.unit_to_campaign ALTER COLUMN id SET DEFAULT nextval('maplog.unit_to_campaign_id_seq'::regclass);
  147. CREATE TABLE maplog.unit_to_sensor (
  148. sensor_id BIGINT NOT NULL,
  149. unit_id BIGINT NOT NULL,
  150. PRIMARY KEY (unit_id, sensor_id)
  151. );
  152. ALTER TABLE maplog.unit_to_sensor OWNER TO senslog;
  153. CREATE TABLE maplog.unit_type (
  154. unit_type_id CHARACTER VARYING(2) NOT NULL PRIMARY KEY,
  155. name CHARACTER VARYING(20) NOT NULL,
  156. description TEXT
  157. );
  158. ALTER TABLE maplog.unit_type OWNER TO senslog;
  159. CREATE TABLE maplog.user_to_campaign_config (
  160. user_id INTEGER NOT NULL,
  161. campaign_id INTEGER NOT NULL,
  162. entity_id INTEGER NOT NULL,
  163. config JSONB NOT NULL DEFAULT '{}'::JSONB,
  164. PRIMARY KEY (user_id, campaign_id, entity_id)
  165. );
  166. ALTER TABLE maplog.user_to_campaign_config OWNER TO senslog;
  167. CREATE TYPE alert_status AS ENUM ('CREATED', 'INFORMED', 'IN_PROCESS', 'SOLVED', 'DELETED');
  168. CREATE TABLE maplog.alert (
  169. id BIGINT NOT NULL PRIMARY KEY,
  170. time_stamp TIMESTAMP WITH TIME ZONE NOT NULL,
  171. unit_id BIGINT NOT NULL,
  172. message TEXT NOT NULL,
  173. status alert_status NOT NULL,
  174. time_received TIMESTAMP WITH TIME ZONE DEFAULT now() NOT NULL
  175. );
  176. ALTER TABLE maplog.alert OWNER TO senslog;
  177. CREATE TABLE maplog.unit_static_to_mobile (
  178. static_unit_id BIGINT NOT NULL,
  179. mobile_unit_id BIGINT NOT NULL,
  180. the_geom public.geometry NOT NULL,
  181. PRIMARY KEY (static_unit_id, mobile_unit_id)
  182. );
  183. ALTER TABLE maplog.unit_static_to_mobile OWNER TO senslog;
  184. CREATE SEQUENCE maplog.alert_id_seq START WITH 1 INCREMENT BY 1 NO MINVALUE NO MAXVALUE CACHE 1;
  185. ALTER TABLE maplog.alert_id_seq OWNER TO senslog;
  186. ALTER SEQUENCE maplog.alert_id_seq OWNED BY maplog.alert.id;
  187. ALTER TABLE ONLY maplog.alert ALTER COLUMN id SET DEFAULT nextval('maplog.alert_id_seq'::regclass);
  188. CREATE INDEX fki_obss_unitid_fk ON maplog.obs_telemetry USING btree (unit_id);
  189. CREATE INDEX fki_sens_phenom_fk ON maplog.sensor USING btree (phenomenon_id);
  190. CREATE INDEX fki_u2c_campid_fk ON maplog.user_to_campaign_config USING btree (campaign_id);
  191. CREATE INDEX fki_u2c_userid_fk ON maplog.user_to_campaign_config USING btree (user_id);
  192. CREATE INDEX fki_u2c_entityid_fk ON maplog.user_to_campaign_config USING btree (entity_id);
  193. CREATE INDEX fki_un2c_campid ON maplog.unit_to_campaign USING btree (campaign_id);
  194. CREATE INDEX fki_un2c_unitid ON maplog.unit_to_campaign USING btree (unit_id);
  195. CREATE INDEX fki_unit_unittype_fk ON maplog.unit USING btree (unit_type_id);
  196. CREATE INDEX fki_uts_sensorid_fk ON maplog.unit_to_sensor USING btree (sensor_id);
  197. CREATE INDEX fki_uts_unitid_fk ON maplog.unit_to_sensor USING btree (unit_id);
  198. CREATE INDEX fki_event_unitid_fk ON maplog.event USING btree (unit_id);
  199. CREATE INDEX fki_dr2ac_entityid_fk ON maplog.event USING btree (entity_id);
  200. CREATE INDEX fki_dr2ac_actionid_fk ON maplog.event USING btree (action_id);
  201. CREATE INDEX fki_dr2ac_unitid_fk ON maplog.event USING btree (unit_id);
  202. CREATE INDEX fki_alert_unitid_fk ON maplog.alert USING btree (unit_id);
  203. 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;
  204. ALTER TABLE ONLY maplog.sensor ADD CONSTRAINT sens_phenom_fk FOREIGN KEY (phenomenon_id) REFERENCES maplog.phenomenon(id);
  205. 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;
  206. 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;
  207. 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;
  208. 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;
  209. 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;
  210. ALTER TABLE ONLY maplog.unit ADD CONSTRAINT unit_unittype_fk FOREIGN KEY (unit_type_id) REFERENCES maplog.unit_type(unit_type_id);
  211. 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;
  212. 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;
  213. 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;
  214. ALTER TABLE ONLY maplog.event ADD CONSTRAINT event_entityid_fk FOREIGN KEY (entity_id) REFERENCES maplog.entity(id) ON UPDATE CASCADE ON DELETE CASCADE;
  215. 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;
  216. 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;
  217. ALTER TABLE ONLY maplog.unit_static_to_mobile ADD CONSTRAINT static_mobile_st_fk FOREIGN KEY (static_unit_id) REFERENCES maplog.unit(unit_id) ON UPDATE CASCADE ON DELETE CASCADE;
  218. ALTER TABLE ONLY maplog.unit_static_to_mobile ADD CONSTRAINT static_mobile_mb_fk FOREIGN KEY (mobile_unit_id) REFERENCES maplog.unit(unit_id) ON UPDATE CASCADE ON DELETE CASCADE;
  219. REVOKE USAGE ON SCHEMA public FROM PUBLIC;
  220. GRANT ALL ON SCHEMA public TO PUBLIC;
  221. CREATE SCHEMA tracking;
  222. ALTER SCHEMA tracking OWNER TO senslog;
  223. CREATE TYPE order_status AS ENUM ('READY', 'MONITORING', 'DELIVERED');
  224. CREATE TYPE delivery_type AS ENUM ('SERVICE_FOFR', 'VEHICLE_CITROEN', 'VEHICLE_PEUGEOT');
  225. CREATE TABLE tracking.record (
  226. id BIGINT NOT NULL PRIMARY KEY,
  227. unit_id BIGINT NOT NULL,
  228. order_id BIGINT NOT NULL,
  229. tracking_id BIGINT NOT NULL,
  230. status order_status NOT NULL DEFAULT 'READY',
  231. delivery_type delivery_type NOT NULL,
  232. time_tracking_start TIMESTAMP WITH TIME ZONE DEFAULT null,
  233. time_tracking_stop TIMESTAMP WITH TIME ZONE DEFAULT null,
  234. time_received TIMESTAMP WITH TIME ZONE NOT NULL DEFAULT now()
  235. );
  236. ALTER TABLE tracking.record OWNER TO senslog;
  237. CREATE UNIQUE INDEX tracking_record_uniq_open ON tracking.record(unit_id) WHERE (status = 'READY' OR status = 'MONITORING');
  238. CREATE SEQUENCE tracking.record_id_seq START WITH 1 INCREMENT BY 1 NO MINVALUE NO MAXVALUE CACHE 1;
  239. ALTER TABLE tracking.record_id_seq OWNER TO senslog;
  240. ALTER SEQUENCE tracking.record_id_seq OWNED BY tracking.record.id;
  241. ALTER TABLE ONLY tracking.record ALTER COLUMN id SET DEFAULT nextval('tracking.record_id_seq'::regclass);
  242. CREATE TABLE tracking.order_to_event (
  243. record_id BIGINT NOT NULL,
  244. event_id BIGINT NOT NULL,
  245. PRIMARY KEY (record_id, event_id)
  246. );
  247. ALTER TABLE tracking.order_to_event OWNER TO senslog;
  248. ALTER TABLE ONLY tracking.order_to_event ADD CONSTRAINT order_event_rec_fk FOREIGN KEY (record_id) REFERENCES tracking.record(id) ON UPDATE CASCADE ON DELETE CASCADE;
  249. ALTER TABLE ONLY tracking.order_to_event ADD CONSTRAINT order_event_ev_fk FOREIGN KEY (event_id) REFERENCES maplog.event(id) ON UPDATE CASCADE ON DELETE CASCADE;
  250. CREATE TYPE tracking.comparison_operator AS ENUM ('LT', 'LE', 'GE', 'GT', 'NE', 'EQ');
  251. ALTER TYPE tracking.comparison_operator OWNER TO senslog;
  252. CREATE TABLE tracking.violating_conditions (
  253. unit_id BIGINT NOT NULL,
  254. phenomenon_id INTEGER NOT NULL,
  255. comparison_operator tracking.comparison_operator NOT NULL,
  256. value DOUBLE PRECISION NOT NULL,
  257. UNIQUE (unit_id, phenomenon_id)
  258. );
  259. ALTER TABLE tracking.violating_conditions OWNER TO senslog;
  260. ALTER TABLE ONLY tracking.violating_conditions ADD CONSTRAINT v_cnd_unit FOREIGN KEY (unit_id) REFERENCES maplog.unit(unit_id);
  261. ALTER TABLE ONLY tracking.violating_conditions ADD CONSTRAINT v_cnd_ph FOREIGN KEY (phenomenon_id) REFERENCES maplog.phenomenon(id);