Merge PR #122: feat: 用户自定义属性系统 + Wechat 字段迁移
This commit is contained in:
48
backend/migrations/018_user_attributes.sql
Normal file
48
backend/migrations/018_user_attributes.sql
Normal file
@@ -0,0 +1,48 @@
|
||||
-- Add user attribute definitions and values tables for custom user attributes.
|
||||
|
||||
-- User Attribute Definitions table (with soft delete support)
|
||||
CREATE TABLE IF NOT EXISTS user_attribute_definitions (
|
||||
id BIGSERIAL PRIMARY KEY,
|
||||
key VARCHAR(100) NOT NULL,
|
||||
name VARCHAR(255) NOT NULL,
|
||||
description TEXT DEFAULT '',
|
||||
type VARCHAR(20) NOT NULL,
|
||||
options JSONB DEFAULT '[]'::jsonb,
|
||||
required BOOLEAN NOT NULL DEFAULT FALSE,
|
||||
validation JSONB DEFAULT '{}'::jsonb,
|
||||
placeholder VARCHAR(255) DEFAULT '',
|
||||
display_order INT NOT NULL DEFAULT 0,
|
||||
enabled BOOLEAN NOT NULL DEFAULT TRUE,
|
||||
created_at TIMESTAMPTZ NOT NULL DEFAULT NOW(),
|
||||
updated_at TIMESTAMPTZ NOT NULL DEFAULT NOW(),
|
||||
deleted_at TIMESTAMPTZ
|
||||
);
|
||||
|
||||
-- Partial unique index for key (only for non-deleted records)
|
||||
-- Allows reusing keys after soft delete
|
||||
CREATE UNIQUE INDEX IF NOT EXISTS idx_user_attribute_definitions_key_unique
|
||||
ON user_attribute_definitions(key) WHERE deleted_at IS NULL;
|
||||
|
||||
CREATE INDEX IF NOT EXISTS idx_user_attribute_definitions_enabled
|
||||
ON user_attribute_definitions(enabled);
|
||||
CREATE INDEX IF NOT EXISTS idx_user_attribute_definitions_display_order
|
||||
ON user_attribute_definitions(display_order);
|
||||
CREATE INDEX IF NOT EXISTS idx_user_attribute_definitions_deleted_at
|
||||
ON user_attribute_definitions(deleted_at);
|
||||
|
||||
-- User Attribute Values table (hard delete only, no deleted_at)
|
||||
CREATE TABLE IF NOT EXISTS user_attribute_values (
|
||||
id BIGSERIAL PRIMARY KEY,
|
||||
user_id BIGINT NOT NULL REFERENCES users(id) ON DELETE CASCADE,
|
||||
attribute_id BIGINT NOT NULL REFERENCES user_attribute_definitions(id) ON DELETE CASCADE,
|
||||
value TEXT DEFAULT '',
|
||||
created_at TIMESTAMPTZ NOT NULL DEFAULT NOW(),
|
||||
updated_at TIMESTAMPTZ NOT NULL DEFAULT NOW(),
|
||||
|
||||
UNIQUE(user_id, attribute_id)
|
||||
);
|
||||
|
||||
CREATE INDEX IF NOT EXISTS idx_user_attribute_values_user_id
|
||||
ON user_attribute_values(user_id);
|
||||
CREATE INDEX IF NOT EXISTS idx_user_attribute_values_attribute_id
|
||||
ON user_attribute_values(attribute_id);
|
||||
83
backend/migrations/019_migrate_wechat_to_attributes.sql
Normal file
83
backend/migrations/019_migrate_wechat_to_attributes.sql
Normal file
@@ -0,0 +1,83 @@
|
||||
-- Migration: Move wechat field from users table to user_attribute_values
|
||||
-- This migration:
|
||||
-- 1. Creates a "wechat" attribute definition
|
||||
-- 2. Migrates existing wechat data to user_attribute_values
|
||||
-- 3. Does NOT drop the wechat column (for rollback safety, can be done in a later migration)
|
||||
|
||||
-- +goose Up
|
||||
-- +goose StatementBegin
|
||||
|
||||
-- Step 1: Insert wechat attribute definition if not exists
|
||||
INSERT INTO user_attribute_definitions (key, name, description, type, options, required, validation, placeholder, display_order, enabled, created_at, updated_at)
|
||||
SELECT 'wechat', '微信', '用户微信号', 'text', '[]'::jsonb, false, '{}'::jsonb, '请输入微信号', 0, true, NOW(), NOW()
|
||||
WHERE NOT EXISTS (
|
||||
SELECT 1 FROM user_attribute_definitions WHERE key = 'wechat' AND deleted_at IS NULL
|
||||
);
|
||||
|
||||
-- Step 2: Migrate existing wechat values to user_attribute_values
|
||||
-- Only migrate non-empty values
|
||||
INSERT INTO user_attribute_values (user_id, attribute_id, value, created_at, updated_at)
|
||||
SELECT
|
||||
u.id,
|
||||
(SELECT id FROM user_attribute_definitions WHERE key = 'wechat' AND deleted_at IS NULL LIMIT 1),
|
||||
u.wechat,
|
||||
NOW(),
|
||||
NOW()
|
||||
FROM users u
|
||||
WHERE u.wechat IS NOT NULL
|
||||
AND u.wechat != ''
|
||||
AND u.deleted_at IS NULL
|
||||
AND NOT EXISTS (
|
||||
SELECT 1 FROM user_attribute_values uav
|
||||
WHERE uav.user_id = u.id
|
||||
AND uav.attribute_id = (SELECT id FROM user_attribute_definitions WHERE key = 'wechat' AND deleted_at IS NULL LIMIT 1)
|
||||
);
|
||||
|
||||
-- Step 3: Update display_order to ensure wechat appears first
|
||||
UPDATE user_attribute_definitions
|
||||
SET display_order = -1
|
||||
WHERE key = 'wechat' AND deleted_at IS NULL;
|
||||
|
||||
-- Reorder all attributes starting from 0
|
||||
WITH ordered AS (
|
||||
SELECT id, ROW_NUMBER() OVER (ORDER BY display_order, id) - 1 as new_order
|
||||
FROM user_attribute_definitions
|
||||
WHERE deleted_at IS NULL
|
||||
)
|
||||
UPDATE user_attribute_definitions
|
||||
SET display_order = ordered.new_order
|
||||
FROM ordered
|
||||
WHERE user_attribute_definitions.id = ordered.id;
|
||||
|
||||
-- Step 4: Drop the redundant wechat column from users table
|
||||
ALTER TABLE users DROP COLUMN IF EXISTS wechat;
|
||||
|
||||
-- +goose StatementEnd
|
||||
|
||||
-- +goose Down
|
||||
-- +goose StatementBegin
|
||||
|
||||
-- Restore wechat column
|
||||
ALTER TABLE users ADD COLUMN IF NOT EXISTS wechat VARCHAR(100) DEFAULT '';
|
||||
|
||||
-- Copy attribute values back to users.wechat column
|
||||
UPDATE users u
|
||||
SET wechat = uav.value
|
||||
FROM user_attribute_values uav
|
||||
JOIN user_attribute_definitions uad ON uav.attribute_id = uad.id
|
||||
WHERE uav.user_id = u.id
|
||||
AND uad.key = 'wechat'
|
||||
AND uad.deleted_at IS NULL;
|
||||
|
||||
-- Delete migrated attribute values
|
||||
DELETE FROM user_attribute_values
|
||||
WHERE attribute_id IN (
|
||||
SELECT id FROM user_attribute_definitions WHERE key = 'wechat' AND deleted_at IS NULL
|
||||
);
|
||||
|
||||
-- Soft-delete the wechat attribute definition
|
||||
UPDATE user_attribute_definitions
|
||||
SET deleted_at = NOW()
|
||||
WHERE key = 'wechat' AND deleted_at IS NULL;
|
||||
|
||||
-- +goose StatementEnd
|
||||
Reference in New Issue
Block a user