DO $$
BEGIN
  IF NOT EXISTS (SELECT 1 FROM pg_type WHERE typname = 'origin_type_enum') THEN
    CREATE TYPE origin_type_enum AS ENUM ('migrated', 'code_inserted');
  END IF;
END $$;

ALTER TYPE origin_type_enum ADD VALUE IF NOT EXISTS 'migrated';
ALTER TYPE origin_type_enum ADD VALUE IF NOT EXISTS 'code_inserted';

ALTER TABLE user_participation_summary
ADD COLUMN IF NOT EXISTS origin_type origin_type_enum NOT NULL DEFAULT 'code_inserted';

COMMENT ON COLUMN user_participation_summary.origin_type IS
  'Tracks whether the participation summary record was inserted via code or migrated manually.';

ALTER TABLE user_registration_map
ADD COLUMN IF NOT EXISTS origin_type origin_type_enum NOT NULL DEFAULT 'code_inserted';

COMMENT ON COLUMN user_registration_map.origin_type IS
  'Tracks whether the registration map record was inserted via code or migrated manually.';

