-- 001_extensions.sql
-- Registers the TimescaleDB extension on the directus database.
--
-- What this file does:
--   Enables TimescaleDB so that migration 002 can call create_hypertable().
--   CASCADE is included so any required dependency extensions install
--   transparently; for TimescaleDB on timescaledb-ha there are none, but
--   the clause is harmless and future-proof.
--
-- PostGIS is intentionally NOT registered here.
--   Decision: PostGIS lands in a separate migration when Phase 2
--   (geofences, SLZs, waypoints) is implemented. The timescaledb-ha image
--   ships the PostGIS binaries, so the binary is present; it just is not
--   registered on this database yet. The boot-time "PostGIS isn't installed"
--   warning from the processor service is benign and expected during Phase 1.
--
-- Idempotency: IF NOT EXISTS makes this a no-op if timescaledb is already
--   registered. Running this file twice produces no error.

CREATE EXTENSION IF NOT EXISTS timescaledb CASCADE;

-- Assertion: verify the extension is actually present after the statement
-- above. If the CREATE EXTENSION silently failed for any reason (e.g. binary
-- missing from the image), this block halts boot with an actionable message
-- rather than letting migration 002 fail with a less-obvious error.
DO $$ BEGIN
  IF NOT EXISTS (
    SELECT 1 FROM pg_extension WHERE extname = 'timescaledb'
  ) THEN
    RAISE EXCEPTION 'timescaledb extension was not created — check that the timescaledb-ha image is being used and the binary is present';
  END IF;
END $$;
