Files
backend/database/migrations/063_website_static_images.sql
T

84 lines
2.8 KiB
SQL

-- Meshkee CMS — website static image slots (single or list, optional link per image)
CREATE TABLE website_image_slots (
id BIGINT GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY,
business_id BIGINT NOT NULL,
slot_key VARCHAR(100) NOT NULL,
kind VARCHAR(20) NOT NULL,
label VARCHAR(255) NOT NULL,
aspect_ratio VARCHAR(20),
recommended_width INTEGER,
sort_order INTEGER NOT NULL DEFAULT 0,
created_at TIMESTAMPTZ NOT NULL DEFAULT NOW(),
updated_at TIMESTAMPTZ NOT NULL DEFAULT NOW(),
CONSTRAINT website_image_slots_business_id_fkey
FOREIGN KEY (business_id) REFERENCES businesses (id) ON DELETE CASCADE,
CONSTRAINT website_image_slots_kind_check
CHECK (kind IN ('single', 'list')),
CONSTRAINT website_image_slots_aspect_ratio_check
CHECK (aspect_ratio IS NULL OR aspect_ratio ~ '^[0-9]{1,3}:[0-9]{1,3}$'),
CONSTRAINT website_image_slots_recommended_width_check
CHECK (recommended_width IS NULL OR recommended_width > 0)
);
CREATE UNIQUE INDEX website_image_slots_business_id_slot_key_unique
ON website_image_slots (business_id, slot_key);
CREATE INDEX idx_website_image_slots_business_id
ON website_image_slots (business_id);
CREATE TRIGGER website_image_slots_set_updated_at
BEFORE UPDATE ON website_image_slots
FOR EACH ROW
EXECUTE FUNCTION set_updated_at();
CREATE TABLE website_image_slot_items (
id BIGINT GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY,
slot_id BIGINT NOT NULL,
image_media_id BIGINT NOT NULL,
title VARCHAR(255),
link_url VARCHAR(2048),
sort_order INTEGER NOT NULL DEFAULT 0,
created_at TIMESTAMPTZ NOT NULL DEFAULT NOW(),
updated_at TIMESTAMPTZ NOT NULL DEFAULT NOW(),
CONSTRAINT website_image_slot_items_slot_id_fkey
FOREIGN KEY (slot_id) REFERENCES website_image_slots (id) ON DELETE CASCADE,
CONSTRAINT website_image_slot_items_image_media_id_fkey
FOREIGN KEY (image_media_id) REFERENCES media (id) ON DELETE RESTRICT
);
CREATE INDEX idx_website_image_slot_items_slot_id
ON website_image_slot_items (slot_id);
CREATE INDEX idx_website_image_slot_items_image_media_id
ON website_image_slot_items (image_media_id);
CREATE TRIGGER website_image_slot_items_set_updated_at
BEFORE UPDATE ON website_image_slot_items
FOR EACH ROW
EXECUTE FUNCTION set_updated_at();
-- First test slot for live sanihome.ir (no-op if that domain is not in this database)
INSERT INTO website_image_slots (
business_id,
slot_key,
kind,
label,
aspect_ratio,
recommended_width,
sort_order
)
SELECT DISTINCT ON (d.business_id)
d.business_id,
'home-hero',
'list',
'Home hero',
'16:9',
1440,
0
FROM domains d
WHERE lower(regexp_replace(d.host, '^www\.', '')) = 'sanihome.ir'
ON CONFLICT (business_id, slot_key) DO NOTHING;