trading-platform-database-v2/migrations/2026-02-03_unify_timeframe_enum.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

75 lines
2.9 KiB
SQL

-- ============================================================================
-- Migration: Unify timeframe enum across schemas
-- Date: 2026-02-03
-- Task: ST-2.1
-- Issue: DUP-001 - timeframe enum duplicated in trading and market_data
-- ============================================================================
--
-- SUMMARY:
-- This migration creates a unified timeframe enum in the public schema.
-- The enum is a superset of all values from:
-- - trading.timeframe: '1m', '5m', '15m', '30m', '1h', '4h', '1d', '1w', '1M'
-- - market_data.timeframe: '1m', '5m', '15m', '30m', '1h', '4h', '1d', '1w'
--
-- DIFFERENCE IDENTIFIED:
-- - trading.timeframe has '1M' (1 month)
-- - market_data.timeframe does NOT have '1M'
-- - Unified type includes ALL values (superset)
--
-- ============================================================================
-- Step 1: Create unified enum in public schema (superset of all values)
DO $$
BEGIN
IF NOT EXISTS (
SELECT 1 FROM pg_type
WHERE typname = 'trading_timeframe'
AND typnamespace = (SELECT oid FROM pg_namespace WHERE nspname = 'public')
) THEN
CREATE TYPE public.trading_timeframe AS ENUM (
'1m', '5m', '15m', '30m', '1h', '4h', '1d', '1w', '1M'
);
END IF;
END $$;
COMMENT ON TYPE public.trading_timeframe IS 'Unified timeframe enum for all trading-related tables. Replaces trading.timeframe and market_data.timeframe';
-- ============================================================================
-- Step 2: Migration plan for existing tables (NOT EXECUTED - requires planning)
-- ============================================================================
--
-- AFFECTED TABLES (to be migrated in future):
--
-- Schema: trading
-- - trading.trading_signals (column: timeframe)
-- - trading.bot_configurations (column: timeframes - array)
-- - Any other tables using trading.timeframe
--
-- Schema: market_data
-- - market_data.ohlcv_data (column: timeframe)
-- - market_data.symbols (column: supported_timeframes - array)
-- - Any other tables using market_data.timeframe
--
-- MIGRATION STEPS FOR EACH TABLE:
--
-- a) Add new column with public.trading_timeframe type:
-- ALTER TABLE schema.table ADD COLUMN timeframe_new public.trading_timeframe;
--
-- b) Copy data from old column:
-- UPDATE schema.table SET timeframe_new = timeframe::text::public.trading_timeframe;
--
-- c) Drop old column and rename new:
-- ALTER TABLE schema.table DROP COLUMN timeframe;
-- ALTER TABLE schema.table RENAME COLUMN timeframe_new TO timeframe;
--
-- d) After all tables migrated, drop schema-specific enum:
-- DROP TYPE trading.timeframe;
-- DROP TYPE market_data.timeframe;
--
-- WARNING: This is a breaking change that requires:
-- 1. Backend code updates to use new type
-- 2. Application downtime or blue-green deployment
-- 3. Careful coordination with all services
--
-- ============================================================================