nats-app/app/database/001_sensors.up.sql

46 lines
1.3 KiB
SQL

create type sensor_type as enum (
'temperature',
'humidity',
'carbon_dioxide',
'pressure',
'proximity',
'light'
);
create table sensors
(
sensor_id varchar(255) primary key,
sensor_type sensor_type not null,
sampling_interval int not null default 3600,
threshold_above float not null default 100,
threshold_below float not null default 0,
created_at timestamp not null default now(),
updated_at timestamp not null default now()
);
create index idx_sensors_sensor_id on sensors (sensor_id);
create table registry
(
sensor_id varchar(255) not null references sensors (sensor_id),
value float not null,
created_at timestamp not null default now()
)
with (
timescaledb.hypertable,
timescaledb.partition_column = 'created_at',
timescaledb.segmentby = 'sensor_id'
);
insert into sensors (sensor_id, sensor_type, sampling_interval, threshold_above, threshold_below)
values
('temp-001', 'temperature', 10, 100, 0),
('hum-001', 'humidity', 15, 80, 20),
('co2-001', 'carbon_dioxide', 20, 1000, 400),
('pres-001', 'pressure', 30, 1050, 950),
('prox-001', 'proximity', 5, 200, 0),
('light-001', 'light', 10, 10000, 0);