nxtgauge-backend-rust/crates/db/migrations/20260415010003_add_user_role_profile_id.up.sql
Tracewebstudio Dev 03376b9567 feat: Add database redesign documentation and Phase 1-2 migrations
- Add schema_audit.md documenting current schema issues
- Add target_schema.md with complete target schema design
- Add old_to_new_mapping.md with table mapping
- Add migration_plan.md with phased migration strategy
- Add Phase 1 migrations (core infrastructure):
  - user_sessions table
  - users missing columns
  - departments updates
  - designations updates
  - employees updates
- Add Phase 2 migrations (profile domain - CRITICAL):
  - create user_role_profiles root table
  - backfill user_role_profiles from existing profiles
  - add user_role_profile_id to extension tables
  - remove forbidden external portfolio links
- Add user_role_profile Rust model
- Update photographer model to use user_role_profile_id
2026-04-12 23:21:11 +02:00

85 lines
4.8 KiB
SQL

-- Phase 2.3: Add user_role_profile_id to extension tables
-- Migration: 20260415010003
-- This links existing extension tables to the new user_role_profiles root
-- photographer_profiles
ALTER TABLE photographer_profiles ADD COLUMN IF NOT EXISTS user_role_profile_id UUID;
UPDATE photographer_profiles p SET user_role_profile_id = urp.id
FROM user_role_profiles urp
WHERE p.user_id = urp.user_id AND urp.role_key = 'photographer';
ALTER TABLE photographer_profiles ALTER COLUMN user_role_profile_id SET NOT NULL;
-- tutor_profiles
ALTER TABLE tutor_profiles ADD COLUMN IF NOT EXISTS user_role_profile_id UUID;
UPDATE tutor_profiles p SET user_role_profile_id = urp.id
FROM user_role_profiles urp
WHERE p.user_id = urp.user_id AND urp.role_key = 'tutor';
ALTER TABLE tutor_profiles ALTER COLUMN user_role_profile_id SET NOT NULL;
-- makeup_artist_profiles
ALTER TABLE makeup_artist_profiles ADD COLUMN IF NOT EXISTS user_role_profile_id UUID;
UPDATE makeup_artist_profiles p SET user_role_profile_id = urp.id
FROM user_role_profiles urp
WHERE p.user_id = urp.user_id AND urp.role_key = 'makeup_artist';
ALTER TABLE makeup_artist_profiles ALTER COLUMN user_role_profile_id SET NOT NULL;
-- developer_profiles
ALTER TABLE developer_profiles ADD COLUMN IF NOT EXISTS user_role_profile_id UUID;
UPDATE developer_profiles p SET user_role_profile_id = urp.id
FROM user_role_profiles urp
WHERE p.user_id = urp.user_id AND urp.role_key = 'developer';
ALTER TABLE developer_profiles ALTER COLUMN user_role_profile_id SET NOT NULL;
-- video_editor_profiles
ALTER TABLE video_editor_profiles ADD COLUMN IF NOT EXISTS user_role_profile_id UUID;
UPDATE video_editor_profiles p SET user_role_profile_id = urp.id
FROM user_role_profiles urp
WHERE p.user_id = urp.user_id AND urp.role_key = 'video_editor';
ALTER TABLE video_editor_profiles ALTER COLUMN user_role_profile_id SET NOT NULL;
-- graphic_designer_profiles
ALTER TABLE graphic_designer_profiles ADD COLUMN IF NOT EXISTS user_role_profile_id UUID;
UPDATE graphic_designer_profiles p SET user_role_profile_id = urp.id
FROM user_role_profiles urp
WHERE p.user_id = urp.user_id AND urp.role_key = 'graphic_designer';
ALTER TABLE graphic_designer_profiles ALTER COLUMN user_role_profile_id SET NOT NULL;
-- social_media_manager_profiles
ALTER TABLE social_media_manager_profiles ADD COLUMN IF NOT EXISTS user_role_profile_id UUID;
UPDATE social_media_manager_profiles p SET user_role_profile_id = urp.id
FROM user_role_profiles urp
WHERE p.user_id = urp.user_id AND urp.role_key = 'social_media_manager';
ALTER TABLE social_media_manager_profiles ALTER COLUMN user_role_profile_id SET NOT NULL;
-- fitness_trainer_profiles
ALTER TABLE fitness_trainer_profiles ADD COLUMN IF NOT EXISTS user_role_profile_id UUID;
UPDATE fitness_trainer_profiles p SET user_role_profile_id = urp.id
FROM user_role_profiles urp
WHERE p.user_id = urp.user_id AND urp.role_key = 'fitness_trainer';
ALTER TABLE fitness_trainer_profiles ALTER COLUMN user_role_profile_id SET NOT NULL;
-- catering_service_profiles
ALTER TABLE catering_service_profiles ADD COLUMN IF NOT EXISTS user_role_profile_id UUID;
UPDATE catering_service_profiles p SET user_role_profile_id = urp.id
FROM user_role_profiles urp
WHERE p.user_id = urp.user_id AND urp.role_key = 'catering_service';
ALTER TABLE catering_service_profiles ALTER COLUMN user_role_profile_id SET NOT NULL;
-- ugc_content_creator_profiles
ALTER TABLE ugc_content_creator_profiles ADD COLUMN IF NOT EXISTS user_role_profile_id UUID;
UPDATE ugc_content_creator_profiles p SET user_role_profile_id = urp.id
FROM user_role_profiles urp
WHERE p.user_id = urp.user_id AND urp.role_key = 'ugc_content_creator';
ALTER TABLE ugc_content_creator_profiles ALTER COLUMN user_role_profile_id SET NOT NULL;
-- Create indexes
CREATE INDEX IF NOT EXISTS idx_photographer_profiles_user_role ON photographer_profiles(user_role_profile_id);
CREATE INDEX IF NOT EXISTS idx_tutor_profiles_user_role ON tutor_profiles(user_role_profile_id);
CREATE INDEX IF NOT EXISTS idx_makeup_artist_profiles_user_role ON makeup_artist_profiles(user_role_profile_id);
CREATE INDEX IF NOT EXISTS idx_developer_profiles_user_role ON developer_profiles(user_role_profile_id);
CREATE INDEX IF NOT EXISTS idx_video_editor_profiles_user_role ON video_editor_profiles(user_role_profile_id);
CREATE INDEX IF NOT EXISTS idx_graphic_designer_profiles_user_role ON graphic_designer_profiles(user_role_profile_id);
CREATE INDEX IF NOT EXISTS idx_social_media_manager_profiles_user_role ON social_media_manager_profiles(user_role_profile_id);
CREATE INDEX IF NOT EXISTS idx_fitness_trainer_profiles_user_role ON fitness_trainer_profiles(user_role_profile_id);
CREATE INDEX IF NOT EXISTS idx_catering_service_profiles_user_role ON catering_service_profiles(user_role_profile_id);
CREATE INDEX IF NOT EXISTS idx_ugc_content_creator_profiles_user_role ON ugc_content_creator_profiles(user_role_profile_id);