Files
orangecat/supabase/sql/updated_at_trigger.sql
Your Name 38cc4669d8 chore: comprehensive codebase cleanup and organization
- Move 100+ files from root to proper directories
  - Migration scripts → scripts/db/
  - SQL files → supabase/sql/archive/
  - Test/debug scripts → scripts/test/
  - Documentation → docs/archive/ (organized by category)
  - Shell scripts → scripts/ (organized by category)

- Fix test organization
  - Remove nested tests/unit/tests/unit/ structure
  - Consolidate test files to proper locations

- Update documentation references
  - Update migration script paths in workflow docs
  - Update scripts/README.md with moved file notes

- Delete placeholder files
  - Remove empty placeholder.ipynb

- Result: Root directory now contains only essential config files
- Result: Professional, organized codebase structure
- Result: All files in proper locations per project standards

See docs/CLEANUP_COMPLETE.md for full details.
2025-12-21 22:57:54 +01:00

33 lines
981 B
PL/PgSQL

-- Updated_at trigger function and triggers for core tables
-- Safe to run multiple times; existence is checked per trigger
CREATE OR REPLACE FUNCTION public.set_updated_at()
RETURNS TRIGGER AS $$
BEGIN
NEW.updated_at = now();
RETURN NEW;
END;
$$ LANGUAGE plpgsql;
-- Helper to create trigger if not exists
DO $$
DECLARE
r RECORD;
BEGIN
FOR r IN SELECT unnest(ARRAY[
'projects', 'user_products', 'user_services', 'assets', 'loans', 'loan_offers', 'loan_payments', 'messages'
]) AS tbl LOOP
IF NOT EXISTS (
SELECT 1 FROM pg_trigger t
JOIN pg_class c ON c.oid = t.tgrelid
JOIN pg_namespace n ON n.oid = c.relnamespace
WHERE t.tgname = format('trg_set_updated_at_%s', r.tbl)
AND n.nspname = 'public'
AND c.relname = r.tbl
) THEN
EXECUTE format('CREATE TRIGGER trg_set_updated_at_%s BEFORE UPDATE ON public.%I FOR EACH ROW EXECUTE PROCEDURE public.set_updated_at();', r.tbl, r.tbl);
END IF;
END LOOP;
END$$;