018_user_attributes.sql 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. -- Add user attribute definitions and values tables for custom user attributes.
  2. -- User Attribute Definitions table (with soft delete support)
  3. CREATE TABLE IF NOT EXISTS user_attribute_definitions (
  4. id BIGSERIAL PRIMARY KEY,
  5. key VARCHAR(100) NOT NULL,
  6. name VARCHAR(255) NOT NULL,
  7. description TEXT DEFAULT '',
  8. type VARCHAR(20) NOT NULL,
  9. options JSONB DEFAULT '[]'::jsonb,
  10. required BOOLEAN NOT NULL DEFAULT FALSE,
  11. validation JSONB DEFAULT '{}'::jsonb,
  12. placeholder VARCHAR(255) DEFAULT '',
  13. display_order INT NOT NULL DEFAULT 0,
  14. enabled BOOLEAN NOT NULL DEFAULT TRUE,
  15. created_at TIMESTAMPTZ NOT NULL DEFAULT NOW(),
  16. updated_at TIMESTAMPTZ NOT NULL DEFAULT NOW(),
  17. deleted_at TIMESTAMPTZ
  18. );
  19. -- Partial unique index for key (only for non-deleted records)
  20. -- Allows reusing keys after soft delete
  21. CREATE UNIQUE INDEX IF NOT EXISTS idx_user_attribute_definitions_key_unique
  22. ON user_attribute_definitions(key) WHERE deleted_at IS NULL;
  23. CREATE INDEX IF NOT EXISTS idx_user_attribute_definitions_enabled
  24. ON user_attribute_definitions(enabled);
  25. CREATE INDEX IF NOT EXISTS idx_user_attribute_definitions_display_order
  26. ON user_attribute_definitions(display_order);
  27. CREATE INDEX IF NOT EXISTS idx_user_attribute_definitions_deleted_at
  28. ON user_attribute_definitions(deleted_at);
  29. -- User Attribute Values table (hard delete only, no deleted_at)
  30. CREATE TABLE IF NOT EXISTS user_attribute_values (
  31. id BIGSERIAL PRIMARY KEY,
  32. user_id BIGINT NOT NULL REFERENCES users(id) ON DELETE CASCADE,
  33. attribute_id BIGINT NOT NULL REFERENCES user_attribute_definitions(id) ON DELETE CASCADE,
  34. value TEXT DEFAULT '',
  35. created_at TIMESTAMPTZ NOT NULL DEFAULT NOW(),
  36. updated_at TIMESTAMPTZ NOT NULL DEFAULT NOW(),
  37. UNIQUE(user_id, attribute_id)
  38. );
  39. CREATE INDEX IF NOT EXISTS idx_user_attribute_values_user_id
  40. ON user_attribute_values(user_id);
  41. CREATE INDEX IF NOT EXISTS idx_user_attribute_values_attribute_id
  42. ON user_attribute_values(attribute_id);