-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmigration.sql
More file actions
36 lines (31 loc) · 1.16 KB
/
migration.sql
File metadata and controls
36 lines (31 loc) · 1.16 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
-- 数据库迁移脚本:将 image_url 迁移到新的 images 结构
-- 1. 添加新字段
ALTER TABLE products
ADD COLUMN IF NOT EXISTS images JSONB DEFAULT '[]'::jsonb,
ADD COLUMN IF NOT EXISTS primary_image_id VARCHAR(255);
-- 2. 迁移现有的 image_url 数据到 images 字段
UPDATE products
SET images = CASE
WHEN image_url IS NOT NULL AND image_url != '' THEN
jsonb_build_array(
jsonb_build_object(
'id', substr(md5(random()::text), 1, 8),
'url', image_url,
'cloudflare_id', null,
'is_primary', true,
'alt', name || ' 图片',
'created_at', created_at
)
)
ELSE '[]'::jsonb
END
WHERE images = '[]'::jsonb OR images IS NULL;
-- 3. 设置主图ID
UPDATE products
SET primary_image_id = (images->0->>'id')
WHERE jsonb_array_length(images) > 0 AND primary_image_id IS NULL;
-- 4. 删除旧的 image_url 字段(可选,建议先备份)
-- ALTER TABLE products DROP COLUMN IF EXISTS image_url;
-- 5. 添加索引以提高查询性能
CREATE INDEX IF NOT EXISTS idx_products_images ON products USING GIN (images);
CREATE INDEX IF NOT EXISTS idx_products_primary_image ON products (primary_image_id);