brand-master/backend/migrations/008_add_username_to_users.sql
dvirlabs 29aa5c2f36
All checks were successful
ci/woodpecker/push/woodpecker Pipeline was successful
Add the option to login with username or phone or email and fix the leave messages
2026-05-08 18:54:26 +03:00

25 lines
1010 B
SQL

-- Migration: Add username field to users table
-- Date: 2026-05-08
-- Description: Add username column and update phone to be unique for flexible login
-- Add username column
ALTER TABLE "user" ADD COLUMN IF NOT EXISTS username VARCHAR(255);
-- Make phone unique for login purposes (drop constraint first if exists)
DO $$
BEGIN
IF EXISTS (SELECT 1 FROM pg_constraint WHERE conname = 'user_phone_key') THEN
ALTER TABLE "user" DROP CONSTRAINT user_phone_key;
END IF;
END $$;
-- Add unique constraint on phone (only for non-null values)
CREATE UNIQUE INDEX IF NOT EXISTS idx_user_phone_unique ON "user"(phone) WHERE phone IS NOT NULL;
-- Create unique index on username (only for non-null values)
CREATE UNIQUE INDEX IF NOT EXISTS idx_user_username ON "user"(username) WHERE username IS NOT NULL;
-- Add comments
COMMENT ON COLUMN "user".username IS 'Unique username for login (alternative to email)';
COMMENT ON COLUMN "user".phone IS 'Phone number (can be used for login when provided)';