jokull/supabase/migrations/20240409190001_initial.sql

33 lines
932 B
SQL

-- extensions
CREATE EXTENSION IF NOT EXISTS "plv8" SCHEMA pg_catalog;
CREATE EXTENSION IF NOT EXISTS "pg_cron" SCHEMA pg_catalog;
CREATE EXTENSION IF NOT EXISTS "pg_net" SCHEMA extensions;
CREATE EXTENSION IF NOT EXISTS timescaledb CASCADE;
CREATE TABLE IF NOT EXISTS raw_bus_positions (
id BIGINT GENERATED ALWAYS AS IDENTITY,
measured_at TIMESTAMP WITH TIME ZONE NOT NULL DEFAULT NOW(),
response_status INT NULL,
bus_id text,
trip_id text,
route_number text,
headsign text,
tag text,
direction int,
lat decimal,
lon decimal
);
-- TimescaleDB Hypertable creation
SELECT create_hypertable('raw_bus_positions', 'measured_at', chunk_time_interval => INTERVAL '10 minutes');
CREATE INDEX bus_position_measures ON raw_bus_positions (bus_id, measured_at DESC);
SELECT
cron.schedule(
'drop-old-chunks',
'*/10 * * * *',
$$
SELECT drop_chunks('raw_bus_positions', INTERVAL '24 hours');
$$
);