diff --git a/migrations/000.605.sql b/migrations/000.605.sql new file mode 100644 index 0000000..329b6e2 --- /dev/null +++ b/migrations/000.605.sql @@ -0,0 +1,13 @@ +-- Migration: 000.604.sql +-- Description: Create table for tracking swapped products with cascade delete behavior +CREATE TABLE swapped_products ( + id INT GENERATED ALWAYS AS IDENTITY PRIMARY KEY, + original_product_id INTEGER NOT NULL REFERENCES "product-products"(productid), + new_product_id INTEGER NOT NULL REFERENCES "product-products"(productid), + swapped_at TIMESTAMP WITH TIME ZONE NOT NULL DEFAULT CURRENT_TIMESTAMP, + + -- Ensure unique combination of original and swapped product (prevent duplicates) + UNIQUE(original_product_id), + UNIQUE(new_product_id), + CHECK (original_product_id IS DISTINCT FROM new_product_id) +);