tsapi-seriesdb-shared-sql/sql/06_triggers.sql
peter eed06559c0 Canonical TSAPI series DB schema v1.0.0
Single source of truth for the timeseries PostgreSQL schema shared by the
Python (TSAPI_SERIESDB_PG_PY) and C++ (TSAPI_POSTGRES_CPP) libraries. SQL
seeded byte-for-byte from TSAPI_SERIESDB_PG_PY/tsapi_pgdb/sql on 2026-09-02.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
2026-09-02 17:27:26 +01:00

37 lines
1 KiB
PL/PgSQL

-- Triggers and event triggers
-- Generated: 2025-10-15T12:24:56.582392
-- Event triggers
-- Event trigger: prevent_series_drops
CREATE OR REPLACE FUNCTION meta.prevent_direct_drop()
RETURNS event_trigger
LANGUAGE plpgsql
AS $function$
DECLARE
obj record;
BEGIN
-- Get the current context
IF current_setting('application.dropping_series', true) = 'true' THEN
-- Allow the drop if it's coming from our procedures
RETURN;
END IF;
FOR obj IN SELECT * FROM pg_event_trigger_dropped_objects()
WHERE object_type = 'table'
LOOP
IF EXISTS (
SELECT 1 FROM meta.series
WHERE table_name = obj.object_name
AND db_schema = obj.schema_name
) THEN
RAISE EXCEPTION 'Table %.% is registered in series and can only be dropped using TimeseriesDB.drop_series()',
obj.schema_name, obj.object_name;
END IF;
END LOOP;
END;
$function$;
CREATE EVENT TRIGGER prevent_series_drops
ON sql_drop
EXECUTE FUNCTION meta.prevent_direct_drop();