From 307346744b72f3efedcc26f87a88880c78d879b7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Arwid=20Thornstr=C3=B6m?= Date: Thu, 18 Dec 2025 13:32:33 +0100 Subject: [PATCH] Added new tabel interiors_roomtypes (#521) * migrations for the roomtypes * rename files * updated types * Added new tabel interiors_roomtypes * remove old migrations * fixed bug * correct numbers --- migrations/000.601.sql | 20 ++++++++++++++++++++ migrations/000.602.sql | 40 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 60 insertions(+) create mode 100644 migrations/000.601.sql create mode 100644 migrations/000.602.sql diff --git a/migrations/000.601.sql b/migrations/000.601.sql new file mode 100644 index 0000000..28ce2a3 --- /dev/null +++ b/migrations/000.601.sql @@ -0,0 +1,20 @@ +-- Create a table called interiors_roomtypes +-- This table should link the id in interiors table with a id from roomtypes table +CREATE TABLE interiors_roomtypes ( + id SERIAL PRIMARY KEY, + interior_id INTEGER NOT NULL, + roomtype_id INTEGER NOT NULL, + + -- Foreign key constraints with CASCADE DELETE + CONSTRAINT fk_interiors_roomtypes_interiors + FOREIGN KEY (interior_id) REFERENCES interiors(id) ON DELETE CASCADE, + CONSTRAINT fk_interiors_roomtypes_roomtype + FOREIGN KEY (roomtype_id) REFERENCES roomtypes(id) ON DELETE CASCADE, + + -- Ensure unique combination of room and roomtype (prevent duplicates) + UNIQUE(interior_id, roomtype_id) +); + +-- Create indexes for better query performance +CREATE INDEX idx_interiors_roomtypes_interior_id ON interiors_roomtypes(interior_id); +CREATE INDEX idx_interiors_roomtypes_roomtype_id ON interiors_roomtypes(roomtype_id); diff --git a/migrations/000.602.sql b/migrations/000.602.sql new file mode 100644 index 0000000..e650bcd --- /dev/null +++ b/migrations/000.602.sql @@ -0,0 +1,40 @@ +-- Add interiors id to v_interior_image_urls + +CREATE OR REPLACE VIEW v_interior_image_urls AS +SELECT + interiors.id interior_id, + "product-printproducts".productid, + interiors.print_id, + "product-printproducts".groupid AS product_group, + rooms.id AS room_id, + rooms.name AS room_name, + interiors.position, + room_types.name AS room_type, + CASE + WHEN interiors.room_id IS null THEN + '/interior-images/' || interiors.id || '.jpg' + ELSE + '/interiors/' || "product-printproducts".productid || '/' || rooms.s3_suffix + END as uri, + array_agg(DISTINCT roomtypes.name) FILTER (WHERE roomtypes.admin_specific = false) AS room_types +FROM interiors +JOIN "product-printproducts" ON "product-printproducts".printid = interiors.print_id +JOIN "product-groups" ON "product-groups".groupid = "product-printproducts".groupid +LEFT JOIN rooms ON interiors.room_id = rooms.id +LEFT JOIN room_types ON room_types.id = rooms.room_type_id +LEFT JOIN room_roomtypes ON room_roomtypes.room_id = rooms.id +LEFT JOIN interiors_roomtypes ON interiors_roomtypes.interior_id = interiors.id +LEFT JOIN roomtypes ON roomtypes.id = CASE + WHEN interiors.room_id IS NULL THEN interiors_roomtypes.roomtype_id + ELSE room_roomtypes.roomtype_id + END +GROUP BY + interiors.id, + "product-printproducts".productid, + interiors.print_id, + "product-printproducts".groupid, + rooms.id, + rooms.name, + interiors.position, + room_types.name, + rooms.s3_suffix;