Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 25 additions & 7 deletions RelDB/build_tables/schema.sql
Original file line number Diff line number Diff line change
Expand Up @@ -267,9 +267,9 @@ CREATE TABLE IF NOT EXISTS public.sensor_zone_stats (
inserted_at TIMESTAMPTZ NOT NULL DEFAULT now()
);

--- Alerts table
--- Alerts_leaves table

CREATE TABLE IF NOT EXISTS public.alerts (
CREATE TABLE IF NOT EXISTS public.alerts_leaves (
id bigserial PRIMARY KEY,
entity_id text NOT NULL,
rule text NOT NULL,
Expand All @@ -282,12 +282,15 @@ CREATE TABLE IF NOT EXISTS public.alerts (
meta_json jsonb
);

CREATE INDEX IF NOT EXISTS ix_alerts_leaves_entity_rule ON public.alerts_leaves(entity_id, rule);
CREATE INDEX IF NOT EXISTS ix_alerts_leaves_status ON public.alerts_leaves(status);


--- === Soil moisture irrigation tables ===

CREATE TABLE IF NOT EXISTS soil_moisture_events (
id SERIAL PRIMARY KEY,
zone_id TEXT NOT NULL,
device_id TEXT NOT NULL REFERENCES devices(device_id),
ts TIMESTAMPTZ NOT NULL DEFAULT NOW(),
dry_ratio REAL NOT NULL,
decision TEXT NOT NULL,
Expand All @@ -300,7 +303,8 @@ CREATE TABLE IF NOT EXISTS soil_moisture_events (
CREATE UNIQUE INDEX IF NOT EXISTS idx_events_idem ON soil_moisture_events (idempotency_key);

CREATE TABLE IF NOT EXISTS irrigation_schedule (
zone_id TEXT PRIMARY KEY,
device_id TEXT PRIMARY KEY REFERENCES devices(device_id),

next_run_at TIMESTAMPTZ NOT NULL,
duration_min INT NOT NULL,
updated_by TEXT NOT NULL,
Expand All @@ -310,7 +314,7 @@ CREATE TABLE IF NOT EXISTS irrigation_schedule (

CREATE TABLE IF NOT EXISTS irrigation_schedule_audit (
id SERIAL PRIMARY KEY,
zone_id TEXT NOT NULL,
device_id TEXT NOT NULL,
prev_next_run_at TIMESTAMPTZ,
prev_duration_min INT,
next_run_at TIMESTAMPTZ NOT NULL,
Expand All @@ -320,6 +324,21 @@ CREATE TABLE IF NOT EXISTS irrigation_schedule_audit (
updated_at TIMESTAMPTZ NOT NULL DEFAULT NOW()
);

CREATE TABLE irrigation_policies (
device_id TEXT NOT NULL,
prev_state TEXT,
dry_ratio_high REAL,
dry_ratio_low REAL,
min_patches INT,
duration_min INT,
updated_at TIMESTAMP DEFAULT NOW(),
PRIMARY KEY (device_id),
CONSTRAINT fk_device
FOREIGN KEY (device_id) REFERENCES devices(device_id)
ON DELETE CASCADE
);


-- === Task thresholds (enum + table) ===
DO $$
BEGIN
Expand Down Expand Up @@ -412,5 +431,4 @@ CREATE INDEX IF NOT EXISTS ix_event_logs_sensors_start_brin ON event_logs_sens
CREATE INDEX IF NOT EXISTS ix_event_logs_sensors_details_gin ON event_logs_sensors USING GIN (details jsonb_path_ops);


CREATE INDEX IF NOT EXISTS ix_alerts_entity_rule ON public.alerts(entity_id, rule);
CREATE INDEX IF NOT EXISTS ix_alerts_status ON public.alerts(status);

Loading