-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathschema.sql
More file actions
35 lines (30 loc) · 1.37 KB
/
Copy pathschema.sql
File metadata and controls
35 lines (30 loc) · 1.37 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
-- PostgreSQL Schema Definition for PulseStream
-- Enable UUID extension if not enabled
CREATE EXTENSION IF NOT EXISTS "uuid-ossp";
-- 1. Clients Table (For API Key Authentication)
CREATE TABLE IF NOT EXISTS clients (
id UUID PRIMARY KEY DEFAULT uuid_generate_v4(),
api_key VARCHAR(255) UNIQUE NOT NULL,
name VARCHAR(255) NOT NULL,
active BOOLEAN DEFAULT TRUE,
created_at TIMESTAMP WITH TIME ZONE DEFAULT CURRENT_TIMESTAMP
);
-- 2. Events Table (For Ingested Metrics)
-- The primary key 'id' represents the unique Idempotency-Key sent by the client.
-- This enforces database-level idempotency natively.
CREATE TABLE IF NOT EXISTS events (
id UUID PRIMARY KEY,
device_id UUID NOT NULL,
event_type VARCHAR(50) NOT NULL,
value DOUBLE PRECISION NOT NULL,
timestamp BIGINT NOT NULL,
created_at TIMESTAMP WITH TIME ZONE DEFAULT CURRENT_TIMESTAMP
);
-- Index for querying metrics by device and type
CREATE INDEX IF NOT EXISTS idx_events_device_type ON events (device_id, event_type);
-- Index for querying metrics by timestamp (useful for timeseries aggregation)
CREATE INDEX IF NOT EXISTS idx_events_timestamp ON events (timestamp DESC);
-- Seed a default test client (we can use this key for development)
INSERT INTO clients (api_key, name, active)
VALUES ('ps_live_test_key_abc123xyz', 'Developer Test IoT Device', true)
ON CONFLICT (api_key) DO NOTHING;