2026-06-06 23:51:30 -04:00
|
|
|
-- +goose Up
|
|
|
|
|
-- +goose StatementBegin
|
2026-05-22 20:26:11 -04:00
|
|
|
ALTER TABLE user_profiles
|
|
|
|
|
ADD COLUMN is_primary BOOLEAN NOT NULL DEFAULT false;
|
|
|
|
|
|
|
|
|
|
WITH ranked AS (
|
|
|
|
|
SELECT id,
|
|
|
|
|
ROW_NUMBER() OVER (
|
|
|
|
|
PARTITION BY user_id
|
|
|
|
|
ORDER BY created_at ASC, id ASC
|
|
|
|
|
) AS rn
|
|
|
|
|
FROM user_profiles
|
|
|
|
|
)
|
|
|
|
|
UPDATE user_profiles
|
|
|
|
|
SET is_primary = true
|
|
|
|
|
FROM ranked
|
|
|
|
|
WHERE user_profiles.id = ranked.id
|
|
|
|
|
AND ranked.rn = 1;
|
|
|
|
|
|
|
|
|
|
CREATE UNIQUE INDEX user_profiles_primary_per_user
|
|
|
|
|
ON user_profiles (user_id)
|
|
|
|
|
WHERE is_primary;
|
2026-06-06 23:51:30 -04:00
|
|
|
-- +goose StatementEnd
|
|
|
|
|
|
|
|
|
|
-- +goose Down
|
|
|
|
|
-- +goose StatementBegin
|
|
|
|
|
DROP INDEX IF EXISTS user_profiles_primary_per_user;
|
|
|
|
|
|
|
|
|
|
ALTER TABLE user_profiles
|
|
|
|
|
DROP COLUMN IF EXISTS is_primary;
|
|
|
|
|
-- +goose StatementEnd
|