014_create_ai_picture_table.sql 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. -- 迁移文件: 014_create_ai_picture_table.sql
  2. -- 描述: 创建AI生图记录表
  3. -- 需求: 5.1, 5.2
  4. -- ============================================
  5. -- 正向迁移: 创建表和索引
  6. -- ============================================
  7. -- 创建ai_picture表
  8. CREATE TABLE IF NOT EXISTS ai_picture (
  9. id SERIAL PRIMARY KEY,
  10. model_id VARCHAR(100) NOT NULL,
  11. model_name VARCHAR(100) NOT NULL,
  12. user_id VARCHAR(50) NOT NULL,
  13. input_type VARCHAR(20) NOT NULL,
  14. input_data TEXT NOT NULL,
  15. image_count INTEGER NOT NULL DEFAULT 1,
  16. output_images JSONB NOT NULL,
  17. cost DECIMAL(10, 4) NOT NULL DEFAULT 0,
  18. status VARCHAR(20) NOT NULL DEFAULT 'success',
  19. created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
  20. );
  21. -- 添加表注释
  22. COMMENT ON TABLE ai_picture IS 'AI生图记录表';
  23. COMMENT ON COLUMN ai_picture.id IS '主键ID';
  24. COMMENT ON COLUMN ai_picture.model_id IS '模型ID';
  25. COMMENT ON COLUMN ai_picture.model_name IS '模型名称';
  26. COMMENT ON COLUMN ai_picture.user_id IS '用户ID';
  27. COMMENT ON COLUMN ai_picture.input_type IS '输入类型: text/image';
  28. COMMENT ON COLUMN ai_picture.input_data IS '输入数据:文本或OSS地址';
  29. COMMENT ON COLUMN ai_picture.image_count IS '生成图片数量';
  30. COMMENT ON COLUMN ai_picture.output_images IS '输出图片OSS地址数组';
  31. COMMENT ON COLUMN ai_picture.cost IS '费用(元)';
  32. COMMENT ON COLUMN ai_picture.status IS '状态';
  33. COMMENT ON COLUMN ai_picture.created_at IS '创建时间';
  34. -- 创建索引(优化查询性能)
  35. CREATE INDEX IF NOT EXISTS idx_ai_picture_user_id ON ai_picture(user_id);
  36. CREATE INDEX IF NOT EXISTS idx_ai_picture_created_at ON ai_picture(created_at DESC);
  37. -- ============================================
  38. -- 回滚迁移: 删除表和索引
  39. -- ============================================
  40. -- DROP INDEX IF EXISTS idx_ai_picture_created_at;
  41. -- DROP INDEX IF EXISTS idx_ai_picture_user_id;
  42. -- DROP TABLE IF EXISTS ai_picture;