-- Chattyness Updated At Triggers
-- PostgreSQL 18
--
-- Apply updated_at triggers to all tables with that column
-- Load via: psql -f schema/triggers/001_updated_at.sql

\set ON_ERROR_STOP on

BEGIN;

-- =============================================================================
-- Server Schema Triggers
-- =============================================================================

CREATE TRIGGER trg_server_config_updated_at
    BEFORE UPDATE ON server.config
    FOR EACH ROW EXECUTE FUNCTION public.update_updated_at_column();

CREATE TRIGGER trg_server_staff_updated_at
    BEFORE UPDATE ON server.staff
    FOR EACH ROW EXECUTE FUNCTION public.update_updated_at_column();

CREATE TRIGGER trg_server_props_updated_at
    BEFORE UPDATE ON server.props
    FOR EACH ROW EXECUTE FUNCTION public.update_updated_at_column();

CREATE TRIGGER trg_server_audio_updated_at
    BEFORE UPDATE ON server.audio
    FOR EACH ROW EXECUTE FUNCTION public.update_updated_at_column();

CREATE TRIGGER trg_server_bans_updated_at
    BEFORE UPDATE ON server.bans
    FOR EACH ROW EXECUTE FUNCTION public.update_updated_at_column();

CREATE TRIGGER trg_server_mutes_updated_at
    BEFORE UPDATE ON server.mutes
    FOR EACH ROW EXECUTE FUNCTION public.update_updated_at_column();

CREATE TRIGGER trg_server_content_filters_updated_at
    BEFORE UPDATE ON server.content_filters
    FOR EACH ROW EXECUTE FUNCTION public.update_updated_at_column();

-- =============================================================================
-- Auth Schema Triggers
-- =============================================================================

CREATE TRIGGER trg_auth_users_updated_at
    BEFORE UPDATE ON auth.users
    FOR EACH ROW EXECUTE FUNCTION public.update_updated_at_column();

CREATE TRIGGER trg_auth_inventory_updated_at
    BEFORE UPDATE ON auth.inventory
    FOR EACH ROW EXECUTE FUNCTION public.update_updated_at_column();

CREATE TRIGGER trg_auth_avatars_updated_at
    BEFORE UPDATE ON auth.avatars
    FOR EACH ROW EXECUTE FUNCTION public.update_updated_at_column();

CREATE TRIGGER trg_auth_active_avatars_updated_at
    BEFORE UPDATE ON auth.active_avatars
    FOR EACH ROW EXECUTE FUNCTION public.update_updated_at_column();

-- =============================================================================
-- Realm Schema Triggers
-- =============================================================================

CREATE TRIGGER trg_realm_realms_updated_at
    BEFORE UPDATE ON realm.realms
    FOR EACH ROW EXECUTE FUNCTION public.update_updated_at_column();

CREATE TRIGGER trg_realm_scenes_updated_at
    BEFORE UPDATE ON realm.scenes
    FOR EACH ROW EXECUTE FUNCTION public.update_updated_at_column();

CREATE TRIGGER trg_realm_memberships_updated_at
    BEFORE UPDATE ON realm.memberships
    FOR EACH ROW EXECUTE FUNCTION public.update_updated_at_column();

CREATE TRIGGER trg_realm_realm_scripts_updated_at
    BEFORE UPDATE ON realm.realm_scripts
    FOR EACH ROW EXECUTE FUNCTION public.update_updated_at_column();

CREATE TRIGGER trg_realm_props_updated_at
    BEFORE UPDATE ON realm.props
    FOR EACH ROW EXECUTE FUNCTION public.update_updated_at_column();

CREATE TRIGGER trg_realm_bans_updated_at
    BEFORE UPDATE ON realm.bans
    FOR EACH ROW EXECUTE FUNCTION public.update_updated_at_column();

CREATE TRIGGER trg_realm_mutes_updated_at
    BEFORE UPDATE ON realm.mutes
    FOR EACH ROW EXECUTE FUNCTION public.update_updated_at_column();

CREATE TRIGGER trg_realm_content_filters_updated_at
    BEFORE UPDATE ON realm.content_filters
    FOR EACH ROW EXECUTE FUNCTION public.update_updated_at_column();

CREATE TRIGGER trg_realm_reports_updated_at
    BEFORE UPDATE ON realm.reports
    FOR EACH ROW EXECUTE FUNCTION public.update_updated_at_column();

-- =============================================================================
-- Scene Schema Triggers
-- =============================================================================

CREATE TRIGGER trg_scene_instances_updated_at
    BEFORE UPDATE ON scene.instances
    FOR EACH ROW EXECUTE FUNCTION public.update_updated_at_column();

CREATE TRIGGER trg_scene_spots_updated_at
    BEFORE UPDATE ON scene.spots
    FOR EACH ROW EXECUTE FUNCTION public.update_updated_at_column();

CREATE TRIGGER trg_scene_scripts_updated_at
    BEFORE UPDATE ON scene.scripts
    FOR EACH ROW EXECUTE FUNCTION public.update_updated_at_column();

CREATE TRIGGER trg_scene_decorations_updated_at
    BEFORE UPDATE ON scene.decorations
    FOR EACH ROW EXECUTE FUNCTION public.update_updated_at_column();

COMMIT;
