019_migrate_wechat_to_attributes.sql 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. -- Migration: Move wechat field from users table to user_attribute_values
  2. -- This migration:
  3. -- 1. Creates a "wechat" attribute definition
  4. -- 2. Migrates existing wechat data to user_attribute_values
  5. -- 3. Does NOT drop the wechat column (for rollback safety, can be done in a later migration)
  6. -- +goose Up
  7. -- +goose StatementBegin
  8. -- Step 1: Insert wechat attribute definition if not exists
  9. INSERT INTO user_attribute_definitions (key, name, description, type, options, required, validation, placeholder, display_order, enabled, created_at, updated_at)
  10. SELECT 'wechat', '微信', '用户微信号', 'text', '[]'::jsonb, false, '{}'::jsonb, '请输入微信号', 0, true, NOW(), NOW()
  11. WHERE NOT EXISTS (
  12. SELECT 1 FROM user_attribute_definitions WHERE key = 'wechat' AND deleted_at IS NULL
  13. );
  14. -- Step 2: Migrate existing wechat values to user_attribute_values
  15. -- Only migrate non-empty values
  16. INSERT INTO user_attribute_values (user_id, attribute_id, value, created_at, updated_at)
  17. SELECT
  18. u.id,
  19. (SELECT id FROM user_attribute_definitions WHERE key = 'wechat' AND deleted_at IS NULL LIMIT 1),
  20. u.wechat,
  21. NOW(),
  22. NOW()
  23. FROM users u
  24. WHERE u.wechat IS NOT NULL
  25. AND u.wechat != ''
  26. AND u.deleted_at IS NULL
  27. AND NOT EXISTS (
  28. SELECT 1 FROM user_attribute_values uav
  29. WHERE uav.user_id = u.id
  30. AND uav.attribute_id = (SELECT id FROM user_attribute_definitions WHERE key = 'wechat' AND deleted_at IS NULL LIMIT 1)
  31. );
  32. -- Step 3: Update display_order to ensure wechat appears first
  33. UPDATE user_attribute_definitions
  34. SET display_order = -1
  35. WHERE key = 'wechat' AND deleted_at IS NULL;
  36. -- Reorder all attributes starting from 0
  37. WITH ordered AS (
  38. SELECT id, ROW_NUMBER() OVER (ORDER BY display_order, id) - 1 as new_order
  39. FROM user_attribute_definitions
  40. WHERE deleted_at IS NULL
  41. )
  42. UPDATE user_attribute_definitions
  43. SET display_order = ordered.new_order
  44. FROM ordered
  45. WHERE user_attribute_definitions.id = ordered.id;
  46. -- Step 4: Drop the redundant wechat column from users table
  47. ALTER TABLE users DROP COLUMN IF EXISTS wechat;
  48. -- +goose StatementEnd
  49. -- +goose Down
  50. -- +goose StatementBegin
  51. -- Restore wechat column
  52. ALTER TABLE users ADD COLUMN IF NOT EXISTS wechat VARCHAR(100) DEFAULT '';
  53. -- Copy attribute values back to users.wechat column
  54. UPDATE users u
  55. SET wechat = uav.value
  56. FROM user_attribute_values uav
  57. JOIN user_attribute_definitions uad ON uav.attribute_id = uad.id
  58. WHERE uav.user_id = u.id
  59. AND uad.key = 'wechat'
  60. AND uad.deleted_at IS NULL;
  61. -- Delete migrated attribute values
  62. DELETE FROM user_attribute_values
  63. WHERE attribute_id IN (
  64. SELECT id FROM user_attribute_definitions WHERE key = 'wechat' AND deleted_at IS NULL
  65. );
  66. -- Soft-delete the wechat attribute definition
  67. UPDATE user_attribute_definitions
  68. SET deleted_at = NOW()
  69. WHERE key = 'wechat' AND deleted_at IS NULL;
  70. -- +goose StatementEnd