Files
medical-mall/sql/seed_test2_products.sql

129 lines
4.5 KiB
SQL

-- Seed data for test2@mall.com
-- Adds a shop, products, and SKUs for this user.
DO $$
DECLARE
v_user_email TEXT := 'test2@mall.com';
v_user_id UUID;
v_category_id UUID;
v_product_id UUID;
v_shop_id UUID;
BEGIN
-- 1. Get User ID from auth.users
SELECT id INTO v_user_id FROM auth.users WHERE email = v_user_email LIMIT 1;
IF v_user_id IS NULL THEN
RAISE EXCEPTION 'User % not found in auth.users', v_user_email;
END IF;
-- 2. Ensure user exists in public.ak_users (if table exists and is required for FK)
-- Try to insert if not exists (upsert-like behavior relies on ID match)
-- Assuming ak_users is just a profile table using the same ID
INSERT INTO public.ak_users (id, email, username, nickname, role)
VALUES (v_user_id, v_user_email, 'test2', 'Test User 2', 'merchant')
ON CONFLICT (id) DO NOTHING;
-- 3. Ensure Shop exists
INSERT INTO public.ml_shops (merchant_id, shop_name, status)
VALUES (v_user_id, 'Test Shop 2 Official', 1)
ON CONFLICT (merchant_id) DO UPDATE SET shop_name = 'Test Shop 2 Official'
RETURNING id INTO v_shop_id;
RAISE NOTICE 'Shop ID: %', v_shop_id;
-- 4. Get a Category (Use an existing one or create one)
SELECT id INTO v_category_id FROM public.ml_categories LIMIT 1;
IF v_category_id IS NULL THEN
-- Create a dummy category if none exists
INSERT INTO public.ml_categories (name, level, sort_order)
VALUES ('Electronics', 1, 1)
RETURNING id INTO v_category_id;
END IF;
RAISE NOTICE 'Category ID: %', v_category_id;
-- 5. Insert Product 1: Wireless Headset (No attributes, simple product)
-- Note: product_code must be unique. Using random suffix or timestamp.
INSERT INTO public.ml_products (
merchant_id,
category_id,
product_code,
name,
subtitle,
main_image_url,
base_price,
total_stock,
status,
attributes
)
VALUES (
v_user_id,
v_category_id,
'TEST_P_001_' || floor(random() * 1000)::text,
'Premium Wireless Headset',
'Noise cancelling, 20h battery life',
'https://picsum.photos/400/400?random=101',
199.00,
100,
1, -- On Sale
'{"Brand": "AudioTech", "Model": "WH-1000"}'::jsonb
)
RETURNING id INTO v_product_id;
-- Insert SKUs for Product 1 (Black/White)
INSERT INTO public.ml_product_skus (product_id, sku_code, specifications, price, stock, image_url)
VALUES
(v_product_id, 'SKU_001_BLK_' || floor(random()*1000), '{"Color": "Black"}', 199.00, 50, 'https://picsum.photos/400/400?random=102'),
(v_product_id, 'SKU_001_WHT_' || floor(random()*1000), '{"Color": "White"}', 199.00, 50, 'https://picsum.photos/400/400?random=103');
-- 6. Insert Product 2: Mechanical Keyboard (Complex attributes)
INSERT INTO public.ml_products (
merchant_id, category_id, product_code, name, subtitle, main_image_url, base_price, total_stock, status, attributes
)
VALUES (
v_user_id,
v_category_id,
'TEST_P_002_' || floor(random() * 1000)::text,
'RGB Mechanical Keyboard',
'Blue switches, hot-swappable',
'https://picsum.photos/400/400?random=201',
89.00,
200,
1,
'{"Brand": "KeyMaster", "Material": "Aluminum", "Layout": "75%"}'::jsonb
)
RETURNING id INTO v_product_id;
-- Insert SKUs for Product 2
INSERT INTO public.ml_product_skus (product_id, sku_code, specifications, price, stock)
VALUES
(v_product_id, 'SKU_002_BLU_' || floor(random()*1000), '{"Switch": "Blue", "Color": "Black"}', 89.00, 100),
(v_product_id, 'SKU_002_RED_' || floor(random()*1000), '{"Switch": "Red", "Color": "White"}', 99.00, 100);
-- 7. Insert Product 3: Simple Mouse (Minimal attributes)
INSERT INTO public.ml_products (
merchant_id, category_id, product_code, name, subtitle, main_image_url, base_price, total_stock, status, attributes
)
VALUES (
v_user_id,
v_category_id,
'TEST_P_003_' || floor(random() * 1000)::text,
'Ergonomic Mouse',
'Vertical design for health',
'https://picsum.photos/400/400?random=301',
29.99,
500,
1,
'{"Brand": "ErgoLife"}'::jsonb
)
RETURNING id INTO v_product_id;
-- Insert Default SKU
INSERT INTO public.ml_product_skus (product_id, sku_code, specifications, price, stock)
VALUES
(v_product_id, 'SKU_003_STD_' || floor(random()*1000), '{}', 29.99, 500);
END $$;