#!/bin/bash set -euo pipefail SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" # shellcheck source=prod-load-env.sh source "$SCRIPT_DIR/prod-load-env.sh" psql "$DATABASE_URL" <<'SQL' -- 补齐 profiles 列(兼容旧库 display_name) ALTER TABLE profiles ADD COLUMN IF NOT EXISTS username TEXT; ALTER TABLE profiles ADD COLUMN IF NOT EXISTS full_name TEXT; ALTER TABLE profiles ADD COLUMN IF NOT EXISTS department TEXT; ALTER TABLE profiles ADD COLUMN IF NOT EXISTS status INTEGER DEFAULT 1; ALTER TABLE profiles ADD COLUMN IF NOT EXISTS branch_code TEXT; ALTER TABLE profiles ADD COLUMN IF NOT EXISTS created_at TIMESTAMPTZ DEFAULT NOW(); UPDATE profiles SET full_name = COALESCE(full_name, display_name), username = COALESCE(username, split_part((SELECT email FROM auth.users u WHERE u.id = profiles.id), '@', 1)), status = COALESCE(status, 1) WHERE username IS NULL OR full_name IS NULL; -- superadmin 资料 + 角色 INSERT INTO profiles (id, display_name, username, full_name, status) VALUES ( 'a0000000-0000-0000-0000-000000000001', '超级管理员', 'superadmin', '超级管理员', 1 ) ON CONFLICT (id) DO UPDATE SET display_name = EXCLUDED.display_name, username = EXCLUDED.username, full_name = EXCLUDED.full_name, status = 1; INSERT INTO user_roles (user_id, role) VALUES ('a0000000-0000-0000-0000-000000000001', 'super_admin') ON CONFLICT (user_id, role) DO NOTHING; SELECT u.email, p.username, p.full_name, p.display_name, ur.role FROM auth.users u LEFT JOIN profiles p ON p.id = u.id LEFT JOIN user_roles ur ON ur.user_id = u.id WHERE u.email = 'superadmin@meoo.local'; SQL