Initial commit of akmon project

This commit is contained in:
2026-01-20 08:04:15 +08:00
commit 77a2bab985
1309 changed files with 343305 additions and 0 deletions

View File

@@ -0,0 +1,231 @@
# 商城数据库部署与测试完整指南
## 📋 部署前检查清单
### 1. 环境要求
- PostgreSQL 13+ 或 Supabase 项目
- 具有数据库创建权限的账户
- 已安装必要扩展的权限
### 2. 必要扩展
```sql
-- 在执行任何脚本前,确保这些扩展已安装
CREATE EXTENSION IF NOT EXISTS "uuid-ossp";
CREATE EXTENSION IF NOT EXISTS "pgcrypto";
```
### 3. 现有表检查
如果您的项目中已有 `ak_users` 表,请确保:
- `auth_id` 字段类型为 `uuid`(不是 `text`
- 表结构包含必要的字段:`id`, `username`, `email`, `phone`, `auth_id`, `avatar_url`, `gender`, `created_at`
## 🚀 部署步骤
### 步骤 1: 验证环境
```bash
# 执行验证脚本
psql -d your_database -f validation_test.sql
```
### 步骤 2: 创建完整数据库结构
```bash
# 执行主数据库脚本
psql -d your_database -f complete_mall_database.sql
```
### 步骤 3: 插入模拟数据
```bash
# 执行模拟数据脚本
psql -d your_database -f mock_data_insert.sql
```
### 步骤 4: 验证部署结果
```bash
# 再次执行验证脚本确认
psql -d your_database -f validation_test.sql
```
## 🔧 Supabase 部署
### 在 Supabase Dashboard 中部署
1. **登录 Supabase Dashboard**
- 打开 [supabase.com](https://supabase.com)
- 选择您的项目
2. **SQL Editor 部署**
```sql
-- 1. 首先安装扩展
CREATE EXTENSION IF NOT EXISTS "uuid-ossp";
CREATE EXTENSION IF NOT EXISTS "pgcrypto";
-- 2. 复制粘贴 complete_mall_database.sql 内容并执行
-- 3. 复制粘贴 mock_data_insert.sql 内容并执行
```
3. **验证 RLS 策略**
- 在 Authentication > Policies 中查看策略
- 确认所有 `ml_*` 表都有相应的 RLS 策略
## 📊 部署验证
### 数据完整性检查
```sql
-- 检查所有主要表的数据量
SELECT
'ak_users' as table_name, COUNT(*) as record_count
FROM public.ak_users
UNION ALL
SELECT 'ml_user_profiles', COUNT(*) FROM public.ml_user_profiles
UNION ALL
SELECT 'ml_merchants', COUNT(*) FROM public.ml_merchants
UNION ALL
SELECT 'ml_categories', COUNT(*) FROM public.ml_categories
UNION ALL
SELECT 'ml_products', COUNT(*) FROM public.ml_products
UNION ALL
SELECT 'ml_orders', COUNT(*) FROM public.ml_orders
UNION ALL
SELECT 'ml_reviews', COUNT(*) FROM public.ml_reviews
ORDER BY table_name;
```
### 权限验证
```sql
-- 检查 RLS 是否正确启用
SELECT
schemaname,
tablename,
rowsecurity
FROM pg_tables
WHERE tablename LIKE 'ml_%'
ORDER BY tablename;
```
### 功能测试
```sql
-- 测试用户认证相关查询
SELECT
u.username,
up.real_name,
up.gender
FROM public.ak_users u
LEFT JOIN public.ml_user_profiles up ON u.id = up.user_id
WHERE u.username IN ('customer1', 'merchant1')
LIMIT 5;
-- 测试商品数据
SELECT
p.name,
p.price,
c.name as category,
m.name as merchant
FROM public.ml_products p
JOIN public.ml_categories c ON p.category_id = c.id
JOIN public.ml_merchants m ON p.merchant_id = m.id
LIMIT 5;
```
## ⚠️ 常见问题解决
### 问题 1: UUID 扩展未安装
```
ERROR: function uuid_generate_v4() does not exist
```
**解决方案:**
```sql
CREATE EXTENSION IF NOT EXISTS "uuid-ossp";
```
### 问题 2: auth_id 类型不匹配
```
ERROR: column "auth_id" is of type uuid but expression is of type text
```
**解决方案:**
确保 `ak_users` 表中 `auth_id` 字段类型为 `uuid`
```sql
-- 检查当前类型
SELECT column_name, data_type
FROM information_schema.columns
WHERE table_name = 'ak_users' AND column_name = 'auth_id';
-- 如果是 text 类型,需要转换
ALTER TABLE public.ak_users
ALTER COLUMN auth_id TYPE uuid
USING auth_id::uuid;
```
### 问题 3: RLS 策略创建失败
```
ERROR: policy "xxx" for table "yyy" already exists
```
**解决方案:**
```sql
-- 删除现有策略后重新创建
DROP POLICY IF EXISTS policy_name ON table_name;
```
### 问题 4: 权限不足
```
ERROR: permission denied for relation ak_users
```
**解决方案:**
确保当前用户具有足够权限,或在 Supabase 中使用 Service Role Key。
## 📈 性能优化建议
### 1. 索引检查
```sql
-- 查看重要表的索引
SELECT
tablename,
indexname,
indexdef
FROM pg_indexes
WHERE tablename LIKE 'ml_%'
ORDER BY tablename, indexname;
```
### 2. 查询优化
- 商品列表查询使用 `ml_products_search_idx` 索引
- 订单查询使用 `ml_orders_user_status_idx` 索引
- 用户行为分析使用 `ml_user_behavior_user_time_idx` 索引
### 3. 监控要点
- 订单表增长速度
- 用户行为日志大小
- 图片存储用量
## 🔄 数据维护
### 定期清理
```sql
-- 清理过期的购物车项目30天前
DELETE FROM public.ml_shopping_cart
WHERE created_at < NOW() - INTERVAL '30 days';
-- 清理过期的优惠券
UPDATE public.ml_coupons
SET status = 'expired'
WHERE end_date < NOW() AND status = 'active';
```
### 备份建议
- 每日备份核心业务表:`ml_orders`, `ml_order_items`, `ml_products`
- 每周全量备份
- 重要操作前手动备份
## 📞 技术支持
如果在部署过程中遇到问题,请检查:
1. PostgreSQL 版本兼容性
2. 扩展安装权限
3. 表结构完整性
4. RLS 策略语法
部署成功后,您的商城数据库将包含:
- ✅ 18 个核心业务表
- ✅ 完整的 RLS 安全策略
- ✅ 优化的索引结构
- ✅ 丰富的模拟测试数据
- ✅ 业务触发器和函数