trading-platform-database-v2/migrations/2026-02-03_unify_common_functions.sql
Adrian Flores Cortes f64251e459 [TASK-2026-02-03-ANALISIS-DDL-MODELADO] refactor(ddl): FASE-2 Conflicts & Duplications
ST-2.1: Unify timeframe enum
- Created ddl/00-global-types.sql with public.trading_timeframe
- Marked trading.timeframe and market_data.timeframe as DEPRECATED

ST-2.2: Resolve transaction_type conflict
- Documented rename plan: financial.wallet_transaction_type
- Documented rename plan: investment.investment_transaction_type
- Added deprecation comments to both enums

ST-2.3: Unify common functions
- Created ddl/00-global-functions.sql with public.update_updated_at()
- Marked schema-specific functions as DEPRECATED:
  - auth.update_updated_at()
  - education.update_updated_at_column()
  - financial.update_timestamp()
  - feature_flags.update_timestamp()

Migrations created (not executed):
- 2026-02-03_unify_timeframe_enum.sql
- 2026-02-03_rename_transaction_type_enums.sql
- 2026-02-03_unify_common_functions.sql

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
2026-02-03 23:51:08 -06:00

94 lines
4.3 KiB
PL/PgSQL

-- ============================================================================
-- Migration: Unify common functions in public schema
-- Date: 2026-02-03
-- Task: ST-2.3
-- Issue: DUP-003 - update_updated_at() duplicated across schemas
-- ============================================================================
-- Purpose: Create a unified update_updated_at() function in the public schema
-- to replace duplicated functions in auth, education, financial, and
-- feature_flags schemas.
-- ============================================================================
-- ============================================================================
-- STEP 1: Create unified function in public schema
-- ============================================================================
CREATE OR REPLACE FUNCTION public.update_updated_at()
RETURNS TRIGGER AS $$
BEGIN
NEW.updated_at = NOW();
RETURN NEW;
END;
$$ LANGUAGE plpgsql;
COMMENT ON FUNCTION public.update_updated_at() IS
'Unified trigger function for updated_at. Replaces schema-specific versions.
Issue: DUP-003 | Task: ST-2.3';
-- ============================================================================
-- STEP 2: Document deprecated functions
-- ============================================================================
-- The following schema-specific functions are now deprecated:
--
-- | Schema | Function Name | File Location |
-- |---------------|----------------------------|-----------------------------------------|
-- | auth | update_updated_at() | schemas/auth/functions/01-update_updated_at.sql |
-- | education | update_updated_at_column() | schemas/education/functions/01-update_updated_at.sql |
-- | financial | update_timestamp() | schemas/financial/functions/03-triggers.sql |
-- | feature_flags | update_timestamp() | schemas/feature_flags/tables/01-flags.sql |
--
-- These functions are NOT dropped in this migration to avoid breaking
-- existing triggers. Future migrations should:
-- 1. Update triggers to use public.update_updated_at()
-- 2. Drop the deprecated schema-specific functions
-- ============================================================================
-- ============================================================================
-- STEP 3: Example trigger update (DO NOT EXECUTE - FOR REFERENCE ONLY)
-- ============================================================================
-- To migrate a trigger from schema-specific to public function:
--
-- -- For auth.users table:
-- DROP TRIGGER IF EXISTS trigger_update_users_updated_at ON auth.users;
-- CREATE TRIGGER trigger_update_users_updated_at
-- BEFORE UPDATE ON auth.users
-- FOR EACH ROW
-- EXECUTE FUNCTION public.update_updated_at();
--
-- -- For education.courses table:
-- DROP TRIGGER IF EXISTS update_courses_updated_at ON education.courses;
-- CREATE TRIGGER update_courses_updated_at
-- BEFORE UPDATE ON education.courses
-- FOR EACH ROW
-- EXECUTE FUNCTION public.update_updated_at();
--
-- -- For financial.wallets table:
-- DROP TRIGGER IF EXISTS trigger_wallets_updated_at ON financial.wallets;
-- CREATE TRIGGER trigger_wallets_updated_at
-- BEFORE UPDATE ON financial.wallets
-- FOR EACH ROW
-- EXECUTE FUNCTION public.update_updated_at();
-- ============================================================================
-- ============================================================================
-- VERIFICATION QUERY
-- ============================================================================
-- Run this query to verify the function was created:
--
-- SELECT
-- n.nspname AS schema,
-- p.proname AS function_name,
-- d.description AS comment
-- FROM pg_proc p
-- JOIN pg_namespace n ON p.pronamespace = n.oid
-- LEFT JOIN pg_description d ON d.objoid = p.oid
-- WHERE p.proname = 'update_updated_at'
-- ORDER BY n.nspname;
-- ============================================================================
-- ============================================================================
-- ROLLBACK (if needed)
-- ============================================================================
-- DROP FUNCTION IF EXISTS public.update_updated_at();
-- ============================================================================