init.sql 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323
  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 NOT NULL,
  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. );
  55. ALTER TABLE maplog.obs_telemetry OWNER TO senslog;
  56. CREATE SEQUENCE maplog.obs_telemetry_id_seq START WITH 1 INCREMENT BY 1 NO MINVALUE NO MAXVALUE CACHE 1;
  57. ALTER TABLE maplog.obs_telemetry_id_seq OWNER TO senslog;
  58. ALTER SEQUENCE maplog.obs_telemetry_id_seq OWNED BY maplog.obs_telemetry.id;
  59. ALTER TABLE ONLY maplog.obs_telemetry ALTER COLUMN id SET DEFAULT nextval('maplog.obs_telemetry_id_seq'::regclass);
  60. CREATE TABLE maplog.entity (
  61. id INTEGER NOT NULL PRIMARY KEY,
  62. identity VARCHAR(30) NOT NULL,
  63. name VARCHAR(100) NOT NULL
  64. );
  65. ALTER TABLE maplog.entity OWNER TO senslog;
  66. CREATE SEQUENCE maplog.entity_id_seq START WITH 1 INCREMENT BY 1 NO MINVALUE NO MAXVALUE CACHE 1;
  67. ALTER TABLE maplog.entity_id_seq OWNER TO senslog;
  68. ALTER SEQUENCE maplog.entity_id_seq OWNED BY maplog.entity.id;
  69. ALTER TABLE ONLY maplog.entity ALTER COLUMN id SET DEFAULT nextval('maplog.entity_id_seq'::regclass);
  70. CREATE TABLE maplog.action (
  71. action_id INTEGER NOT NULL PRIMARY KEY,
  72. name VARCHAR(100) NOT NULL
  73. );
  74. ALTER TABLE maplog.action OWNER TO senslog;
  75. CREATE TABLE maplog.event (
  76. id BIGINT NOT NULL PRIMARY KEY,
  77. entity_id INTEGER NOT NULL,
  78. action_id INTEGER NOT NULL,
  79. unit_id BIGINT NOT NULL,
  80. from_time TIMESTAMP WITH TIME ZONE NOT NULL,
  81. to_time TIMESTAMP WITH TIME ZONE,
  82. CONSTRAINT dta_check_time CHECK (from_time < to_time)
  83. );
  84. ALTER TABLE maplog.event OWNER TO senslog;
  85. CREATE SEQUENCE maplog.event_id_seq START WITH 1 INCREMENT BY 1 NO MINVALUE NO MAXVALUE CACHE 1;
  86. ALTER TABLE maplog.event_id_seq OWNER TO senslog;
  87. ALTER SEQUENCE maplog.event_id_seq OWNED BY maplog.event.id;
  88. ALTER TABLE ONLY maplog.event ALTER COLUMN id SET DEFAULT nextval('maplog.event_id_seq'::regclass);
  89. CREATE TABLE maplog.phenomenon (
  90. id INTEGER NOT NULL PRIMARY KEY,
  91. name TEXT NOT NULL,
  92. uom CHARACTER VARYING(30) NOT NULL,
  93. uom_link TEXT
  94. );
  95. ALTER TABLE maplog.phenomenon OWNER TO senslog;
  96. CREATE SEQUENCE maplog.phenomenon_id_seq AS INTEGER START WITH 1 INCREMENT BY 1 NO MINVALUE NO MAXVALUE CACHE 1;
  97. ALTER TABLE maplog.phenomenon_id_seq OWNER TO senslog;
  98. ALTER SEQUENCE maplog.phenomenon_id_seq OWNED BY maplog.phenomenon.id;
  99. ALTER TABLE ONLY maplog.phenomenon ALTER COLUMN id SET DEFAULT nextval('maplog.phenomenon_id_seq'::regclass);
  100. INSERT INTO maplog.phenomenon(id, name, uom, uom_link) VALUES (0, 'GENERAL', 'UOM', null);
  101. CREATE TABLE maplog.sensor (
  102. sensor_id BIGINT NOT NULL PRIMARY KEY,
  103. name CHARACTER VARYING(100) UNIQUE NOT NULL,
  104. type TEXT,
  105. description TEXT,
  106. io_id INTEGER NOT NULL,
  107. phenomenon_id INTEGER NOT NULL
  108. );
  109. ALTER TABLE maplog.sensor OWNER TO senslog;
  110. CREATE SEQUENCE maplog.sensor_sensor_id_seq START WITH 1 INCREMENT BY 1 NO MINVALUE NO MAXVALUE CACHE 1;
  111. ALTER TABLE maplog.sensor_sensor_id_seq OWNER TO senslog;
  112. ALTER SEQUENCE maplog.sensor_sensor_id_seq OWNED BY maplog.sensor.sensor_id;
  113. ALTER TABLE ONLY maplog.sensor ALTER COLUMN sensor_id SET DEFAULT nextval('maplog.sensor_sensor_id_seq'::regclass);
  114. CREATE TABLE maplog.system_user (
  115. id INTEGER NOT NULL PRIMARY KEY,
  116. identity VARCHAR(50) NOT NULL,
  117. name TEXT NOT NULL
  118. );
  119. ALTER TABLE maplog.system_user OWNER TO senslog;
  120. CREATE SEQUENCE maplog.system_user_id_seq START WITH 1 INCREMENT BY 1 NO MINVALUE NO MAXVALUE CACHE 1;
  121. ALTER TABLE maplog.system_user_id_seq OWNER TO senslog;
  122. ALTER SEQUENCE maplog.system_user_id_seq OWNED BY maplog.sensor.sensor_id;
  123. ALTER TABLE ONLY maplog.system_user ALTER COLUMN id SET DEFAULT nextval('maplog.system_user_id_seq'::regclass);
  124. CREATE TABLE maplog.unit (
  125. unit_id BIGINT NOT NULL PRIMARY KEY,
  126. imei CHARACTER VARYING(20) NOT NULL UNIQUE,
  127. name CHARACTER VARYING(100) NOT NULL,
  128. description TEXT,
  129. is_mobile boolean DEFAULT true NOT NULL,
  130. unit_type_id CHARACTER VARYING(2) DEFAULT 'X'::CHARACTER VARYING NOT NULL
  131. );
  132. ALTER TABLE maplog.unit OWNER TO senslog;
  133. CREATE TABLE maplog.unit_to_campaign (
  134. id INTEGER NOT NULL PRIMARY KEY,
  135. campaign_id INTEGER NOT NULL,
  136. unit_id BIGINT NOT NULL,
  137. from_time TIMESTAMP WITH TIME ZONE NOT NULL,
  138. to_time TIMESTAMP WITH TIME ZONE NOT NULL
  139. );
  140. ALTER TABLE maplog.unit_to_campaign OWNER TO senslog;
  141. CREATE SEQUENCE maplog.unit_to_campaign_id_seq AS INTEGER START WITH 1 INCREMENT BY 1 NO MINVALUE NO MAXVALUE CACHE 1;
  142. ALTER TABLE maplog.unit_to_campaign_id_seq OWNER TO senslog;
  143. ALTER SEQUENCE maplog.unit_to_campaign_id_seq OWNED BY maplog.unit_to_campaign.id;
  144. ALTER TABLE ONLY maplog.unit_to_campaign ALTER COLUMN id SET DEFAULT nextval('maplog.unit_to_campaign_id_seq'::regclass);
  145. CREATE TABLE maplog.unit_to_sensor (
  146. sensor_id BIGINT NOT NULL,
  147. unit_id BIGINT NOT NULL,
  148. PRIMARY KEY (unit_id, sensor_id)
  149. );
  150. ALTER TABLE maplog.unit_to_sensor OWNER TO senslog;
  151. CREATE TABLE maplog.unit_type (
  152. unit_type_id CHARACTER VARYING(2) NOT NULL PRIMARY KEY,
  153. name CHARACTER VARYING(20) NOT NULL,
  154. description TEXT
  155. );
  156. ALTER TABLE maplog.unit_type OWNER TO senslog;
  157. CREATE TABLE maplog.user_to_campaign_config (
  158. user_id INTEGER NOT NULL,
  159. campaign_id INTEGER NOT NULL,
  160. entity_id INTEGER NOT NULL,
  161. config JSONB NOT NULL DEFAULT '{}'::JSONB,
  162. PRIMARY KEY (user_id, campaign_id, entity_id)
  163. );
  164. ALTER TABLE maplog.user_to_campaign_config OWNER TO senslog;
  165. CREATE TYPE alert_status AS ENUM ('CREATED', 'INFORMED', 'IN_PROCESS', 'SOLVED', 'DELETED');
  166. CREATE TABLE maplog.alert (
  167. id BIGINT NOT NULL PRIMARY KEY,
  168. time_stamp TIMESTAMP WITH TIME ZONE NOT NULL,
  169. unit_id BIGINT NOT NULL,
  170. message TEXT NOT NULL,
  171. status alert_status NOT NULL,
  172. time_received TIMESTAMP WITH TIME ZONE DEFAULT now() NOT NULL
  173. );
  174. ALTER TABLE maplog.alert OWNER TO senslog;
  175. CREATE SEQUENCE maplog.alert_id_seq START WITH 1 INCREMENT BY 1 NO MINVALUE NO MAXVALUE CACHE 1;
  176. ALTER TABLE maplog.alert_id_seq OWNER TO senslog;
  177. ALTER SEQUENCE maplog.alert_id_seq OWNED BY maplog.alert.id;
  178. ALTER TABLE ONLY maplog.alert ALTER COLUMN id SET DEFAULT nextval('maplog.alert_id_seq'::regclass);
  179. CREATE INDEX fki_obss_unitid_fk ON maplog.obs_telemetry USING btree (unit_id);
  180. CREATE INDEX fki_sens_phenom_fk ON maplog.sensor USING btree (phenomenon_id);
  181. CREATE INDEX fki_u2c_campid_fk ON maplog.user_to_campaign_config USING btree (campaign_id);
  182. CREATE INDEX fki_u2c_userid_fk ON maplog.user_to_campaign_config USING btree (user_id);
  183. CREATE INDEX fki_u2c_entityid_fk ON maplog.user_to_campaign_config USING btree (entity_id);
  184. CREATE INDEX fki_un2c_campid ON maplog.unit_to_campaign USING btree (campaign_id);
  185. CREATE INDEX fki_un2c_unitid ON maplog.unit_to_campaign USING btree (unit_id);
  186. CREATE INDEX fki_unit_unittype_fk ON maplog.unit USING btree (unit_type_id);
  187. CREATE INDEX fki_uts_sensorid_fk ON maplog.unit_to_sensor USING btree (sensor_id);
  188. CREATE INDEX fki_uts_unitid_fk ON maplog.unit_to_sensor USING btree (unit_id);
  189. CREATE INDEX fki_event_unitid_fk ON maplog.event USING btree (unit_id);
  190. CREATE INDEX fki_dr2ac_entityid_fk ON maplog.event USING btree (entity_id);
  191. CREATE INDEX fki_dr2ac_actionid_fk ON maplog.event USING btree (action_id);
  192. CREATE INDEX fki_dr2ac_unitid_fk ON maplog.event USING btree (unit_id);
  193. CREATE INDEX fki_alert_unitid_fk ON maplog.alert USING btree (unit_id);
  194. 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;
  195. ALTER TABLE ONLY maplog.sensor ADD CONSTRAINT sens_phenom_fk FOREIGN KEY (phenomenon_id) REFERENCES maplog.phenomenon(id);
  196. 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;
  197. 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;
  198. 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;
  199. 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;
  200. 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;
  201. ALTER TABLE ONLY maplog.unit ADD CONSTRAINT unit_unittype_fk FOREIGN KEY (unit_type_id) REFERENCES maplog.unit_type(unit_type_id);
  202. 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;
  203. 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;
  204. 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;
  205. ALTER TABLE ONLY maplog.event ADD CONSTRAINT event_entityid_fk FOREIGN KEY (entity_id) REFERENCES maplog.entity(id) ON UPDATE CASCADE ON DELETE CASCADE;
  206. 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;
  207. 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;
  208. REVOKE USAGE ON SCHEMA public FROM PUBLIC;
  209. GRANT ALL ON SCHEMA public TO PUBLIC;