Advertisement
glavmonter

sql

Nov 26th, 2021
838
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. DROP VIEW IF EXISTS "mqtt_history_view";
  2. DROP TABLE IF EXISTS "mqtt_history";
  3. DROP TABLE IF EXISTS "mqtt";
  4. DROP INDEX IF EXISTS idx_mqtt_history_topicid;
  5. DROP INDEX IF EXISTS idx_mqtt_history_ts;
  6.  
  7.  
  8. CREATE TABLE IF NOT EXISTS "mqtt" (
  9.     id serial UNIQUE,
  10.     ts TIMESTAMP WITH TIME ZONE NOT NULL DEFAULT CURRENT_TIMESTAMP,
  11.     topic TEXT NOT NULL,
  12.     value TEXT NOT NULL,
  13.     qos INT NOT NULL,
  14.     retain INT NOT NULL,
  15.     history_enable INT NOT NULL DEFAULT 1,
  16.     history_diffonly INT NOT NULL DEFAULT 0,
  17.     PRIMARY KEY (topic)
  18. );
  19.  
  20. --
  21. CREATE TABLE IF NOT EXISTS mqtt_history (
  22.     id serial,
  23.     ts TIMESTAMP WITH TIME ZONE NOT NULL DEFAULT CURRENT_TIMESTAMP,
  24.     topicid INT NOT NULL,
  25.     value TEXT NULL DEFAULT NULL,
  26.     PRIMARY KEY (id),
  27.     CONSTRAINT FK_mqtt_history_mqtt FOREIGN KEY (topicid) REFERENCES mqtt (id)
  28. );
  29. CREATE INDEX idx_mqtt_history_topicid ON mqtt_history(topicid);
  30. CREATE INDEX idx_mqtt_history_ts ON mqtt_history(ts);
  31.  
  32.  
  33. CREATE OR REPLACE FUNCTION mqtt_before_insert() RETURNS trigger AS
  34.  
  35.  
  36. CREATE TRIGGER mqtt_before_insert BEFORE INSERT ON mqtt FOR EACH ROW EXECUTE PROCEDURE mqtt_before();
  37.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement